mihalykiss commited on
Commit
4b15114
Β·
verified Β·
1 Parent(s): 2657d2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +294 -197
app.py CHANGED
@@ -3,213 +3,310 @@ import gradio as gr
3
  title = "AI Text Detector"
4
 
5
  description = """
6
- <div class="hero">
7
- <h1>πŸ”Ž AI Text Detector</h1>
8
- <p>
9
- Built on the <b>ModernBERT</b> ensemble (3 variants, soft-voting).<br>
10
- <span class="point">βœ… Human verification</span> &nbsp;|&nbsp;
11
- <span class="point">πŸ” Detects 40+ AI models</span> &nbsp;|&nbsp;
12
- <span class="point">πŸ“ˆ Best on longer passages</span>
13
- </p>
14
- <p class="paper">
15
- Read the full paper&nbsp;β†’&nbsp;
16
- <a href="https://aclanthology.org/2025.genaidetect-1.15/" target="_blank">LINK</a>
17
- </p>
18
  </div>
19
  """
 
20
 
21
- bottom_text = "**Developed by SzegedAI - Mihaly Kiss**"
22
-
23
- # ---------- UI ----------
24
- iface = gr.Blocks(
25
- theme=gr.themes.Default(), # keep defaults; we'll override in CSS
26
- css="""
27
- /* ---------- Global tokens ---------- */
28
- :root{
29
- --primary:#203A6B; /* midnight-indigo */
30
- --accent :#14B8A6; /* teal-mint */
31
- --bg :#F8FAFC; /* light-gray */
32
- --bg-card:rgba(255,255,255,0.55);
33
- --radius :16px;
34
- --shadow :0 6px 24px rgba(0,0,0,.08);
35
- --transition:180ms cubic-bezier(.4,0,.2,1);
36
- --font:'Inter',system-ui,-apple-system,'Segoe UI',sans-serif;
37
- }
38
-
39
- @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap');
40
-
41
- html,body{
42
- font-family:var(--font);
43
- background: var(--bg);
44
- color:var(--primary);
45
- height:100%;
46
- margin:0;
47
- }
48
-
49
- /* ---------- Main container ---------- */
50
- .gradio-container{
51
- display:grid;
52
- place-items:center;
53
- padding:5vmin;
54
- min-height:100dvh;
55
- animation:fade-slide-in .6s ease-out both;
56
- }
57
-
58
- .container{
59
- background:var(--bg-card);
60
- backdrop-filter:blur(12px) saturate(120%);
61
- border:1px solid rgba(255,255,255,.35);
62
- border-radius:var(--radius);
63
- box-shadow:var(--shadow);
64
- padding:clamp(1.5rem,4vw,3rem);
65
- width:min(680px,90vw);
66
- display:grid;
67
- gap:2rem;
68
- }
69
-
70
- /* ---------- Hero / heading ---------- */
71
- .hero{
72
- text-align:center;
73
- line-height:1.6;
74
- }
75
- .hero h1{
76
- margin:0 0 .5rem;
77
- font-size:clamp(1.8rem,4vw,2.4rem);
78
- font-weight:700;
79
- }
80
- .point{
81
- font-weight:600;
82
- }
83
- .paper a{
84
- color:var(--accent);
85
- text-decoration:none;
86
- font-weight:600;
87
- }
88
-
89
- /* ---------- Inputs & button ---------- */
90
- #text_input_box{
91
- border:2px solid rgba(32,58,107,.25);
92
- border-radius:var(--radius);
93
- padding:1rem 1.25rem;
94
- font-size:1rem;
95
- width:100%;
96
- transition:border-color var(--transition), box-shadow var(--transition);
97
- }
98
- #text_input_box:focus{
99
- outline:none;
100
- border-color:var(--accent);
101
- box-shadow:0 0 0 4px rgba(20,184,166,.18);
102
- }
103
-
104
- .analyze-btn{
105
- display:inline-flex;
106
- align-items:center;
107
- justify-content:center;
108
- gap:.5rem;
109
- font-weight:600;
110
- font-size:1rem;
111
- padding:.9rem 1.6rem;
112
- background:var(--primary);
113
- color:#fff;
114
- border:none;
115
- border-radius:var(--radius);
116
- cursor:pointer;
117
- transition:transform var(--transition), box-shadow var(--transition);
118
- }
119
- .analyze-btn:hover{
120
- transform:translateY(-2px);
121
- box-shadow:0 8px 24px rgba(0,0,0,.12);
122
- }
123
- .analyze-btn:active{
124
- transform:translateY(1px) scale(.98);
125
- }
126
-
127
- /* spinner placeholder */
128
- .spinner{
129
- width:1rem;height:1rem;
130
- border:3px solid rgba(255,255,255,.4);
131
- border-top-color:#fff;
132
- border-radius:50%;
133
- animation:spin 1s linear infinite;
134
- }
135
-
136
- @keyframes spin{to{transform:rotate(360deg);}}
137
 
138
- /* ---------- Output box ---------- */
139
- #result_output_box{
140
- text-align:center;
141
- font-size:1.15rem;
142
- padding:1rem 1.5rem;
143
- border-radius:var(--radius);
144
- border:2px solid transparent;
145
- min-height:3.5rem;
146
- word-wrap:break-word;
147
- }
148
- .highlight-human{color:var(--accent);}
149
- .highlight-ai{color:#EF4444;} /* red-500 */
150
-
151
- /* ---------- Tabs ---------- */
152
- .gradio-container .tabs{
153
- margin-top:1rem!important;
154
- }
155
- .gradio-container .tab{
156
- border:none!important;
157
- }
158
- .gradio-container .tabitem{
159
- padding:.35rem .75rem!important;
160
- border-radius:var(--radius) var(--radius) 0 0!important;
161
- }
162
-
163
- /* ---------- Utilities & media ---------- */
164
- @media(max-width:768px){
165
- .container{padding:1.25rem;}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  }
167
 
168
- @keyframes fade-slide-in{
169
- from{opacity:0;transform:translateY(12px);}
170
- to{opacity:1;transform:none;}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  }
172
  """
173
- )
174
 
175
- def classify_text():
176
- pass
177
 
178
- AI_texts = [""]
179
- Human_texts = [""]
180
  with iface:
181
- with gr.Column(elem_classes="container"):
182
- # Hero section
183
- gr.Markdown(description)
184
-
185
- # Input
186
- text_input = gr.Textbox(
187
- label="Paste or type your text...",
188
- placeholder="Start writing here…",
189
- elem_id="text_input_box",
190
- lines=6
191
- )
192
-
193
- # Analyze button (triggers classify_text)
194
- analyze_btn = gr.Button("Analyze", elem_classes="analyze-btn")
195
-
196
- # Output
197
- result_output = gr.Markdown("", elem_id="result_output_box")
198
-
199
- # Example tabs
200
- with gr.Tab("AI examples"):
201
- gr.Examples(examples=AI_texts, inputs=text_input)
202
- with gr.Tab("Human examples"):
203
- gr.Examples(examples=Human_texts, inputs=text_input)
204
-
205
- gr.Markdown(bottom_text, elem_id="bottom_text", elem_classes="footer")
206
-
207
- # Wire the interaction
208
- analyze_btn.click(
209
- fn=classify_text,
210
- inputs=text_input,
211
- outputs=result_output,
212
- show_progress="minimal" # toggles spinner internally
213
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
 
215
- iface.launch(share=True)
 
3
  title = "AI Text Detector"
4
 
5
  description = """
6
+ <div class="app-description">
7
+ <p>This tool utilizes the <b>ModernBERT</b> model to discern whether a given text is human-authored or AI-generated. It employs a soft voting ensemble of <b>three</b> models, amalgamating their outputs to enhance detection accuracy.</p>
8
+ <ul class="features-list">
9
+ <li><span class="icon">βœ…</span> <strong>Human Verification:</strong> Clearly identifies human-written content.</li>
10
+ <li><span class="icon">πŸ”</span> <strong>Model Detection:</strong> Capable of identifying content from over 40 AI models.</li>
11
+ <li><span class="icon">πŸ“ˆ</span> <strong>Accuracy:</strong> Performs optimally with more extensive text inputs.</li>
12
+ <li><span class="icon">πŸ“„</span> <strong>Read more:</strong> Our methodology is detailed in our research paper:
13
+ <a href="https://aclanthology.org/2025.genaidetect-1.15/" target="_blank" class="learn-more-link"><b>LINK</b></a>.
14
+ </li>
15
+ </ul>
16
+ <p class="instruction-text">Paste your text into the field below to analyze its origin.</p>
 
17
  </div>
18
  """
19
+ bottom_text = "<p class='footer-text'>Developed by <strong>SzegedAI</strong></p>"
20
 
21
+ # Placeholder for the actual classification function
22
+ def classify_text(text):
23
+ if not text.strip():
24
+ return "<div style='text-align: center; color: #7f8c8d;'>Please enter some text to analyze.</div>"
25
+ # Simulate model output
26
+ if "bert" in text.lower() or "sentence" in text.lower() or "token" in text.lower():
27
+ return "<div class='highlight-human'>Likely Human-Written</div><p style='font-size:0.9em; color: #7f8c8d; margin-top: 5px;'>Based on linguistic patterns.</p>"
28
+ elif len(text) > 50 : # Simple heuristic for demo
29
+ return "<div class='highlight-ai'>Likely AI-Generated</div><p style='font-size:0.9em; color: #7f8c8d; margin-top: 5px;'>Based on predictive analysis.</p>"
30
+ else:
31
+ return "<div class='highlight-ai'>Potentially AI-Generated</div><p style='font-size:0.9em; color: #7f8c8d; margin-top: 5px;'>Analysis suggests non-human origin, but more text may improve accuracy.</p>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ AI_texts = [
34
+ "Camels are remarkable desert animals known for their unique adaptations to harsh, arid environments. Native to the Middle East, North Africa, and parts of Asia, camels have been essential to human life for centuries, serving as a mode of transportation, a source of food, and even a symbol of endurance and survival. There are two primary species of camels: the dromedary camel, which has a single hump and is commonly found in the Middle East and North Africa, and the Bactrian camel, which has two humps and is native to Central Asia. Their humps store fat, not water, as commonly believed, allowing them to survive long periods without food by metabolizing the stored fat for energy. Camels are highly adapted to desert life. They can go for weeks without water, and when they do drink, they can consume up to 40 gallons in one sitting. Their thick eyelashes, sealable nostrils, and wide, padded feet protect them from sand and help them walk easily on loose desert terrain.",
35
+ ]
36
+
37
+ Human_texts = [
38
+ "To make BERT handle a variety of down-stream tasks, our input representation is able to unambiguously represent both a single sentence and a pair of sentences (e.g., h Question, Answeri) in one token sequence. Throughout this work, a β€œsentence” can be an arbitrary span of contiguous text, rather than an actual linguistic sentence. A β€œsequence” refers to the input token sequence to BERT, which may be a single sentence or two sentences packed together. We use WordPiece embeddings (Wu et al., 2016) with a 30,000 token vocabulary. The first token of every sequence is always a special classification token ([CLS]). The final hidden state corresponding to this token is used as the aggregate sequence representation for classification tasks. Sentence pairs are packed together into a single sequence."
39
+ ]
40
+
41
+ modern_css = """
42
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
43
+
44
+ :root {
45
+ --primary-bg: #F8F9FA; /* Light Gray for page body */
46
+ --app-bg: #FFFFFF; /* White for app container */
47
+ --text-primary: #2C3E50; /* Dark Slate Blue */
48
+ --text-secondary: #7F8C8D; /* Cool Gray */
49
+ --accent-color: #1ABC9C; /* Teal */
50
+ --accent-color-darker: #16A085; /* Darker Teal for hover */
51
+ --border-color: #E0E0E0; /* Light Gray for borders */
52
+ --input-bg: #FFFFFF; /* White for input fields */
53
+ --input-focus-border: var(--accent-color);
54
+ --human-color: #2ECC71; /* Green */
55
+ --human-bg: rgba(46, 204, 113, 0.1);
56
+ --ai-color: #E74C3C; /* Red */
57
+ --ai-bg: rgba(231, 76, 60, 0.1);
58
+ --shadow-color: rgba(44, 62, 80, 0.1); /* Subtle shadow */
59
+ --container-max-width: 700px;
60
+ --border-radius-md: 8px;
61
+ --border-radius-lg: 12px;
62
+ }
63
+
64
+ body {
65
+ font-family: 'Inter', sans-serif;
66
+ background: linear-gradient(135deg, #f5f7fa 0%, #eef2f7 100%); /* Subtle gradient background */
67
+ color: var(--text-primary);
68
+ margin: 0;
69
+ padding: 20px;
70
+ display: flex;
71
+ justify-content: center;
72
+ align-items: flex-start; /* Align to top for scroll */
73
+ min-height: 100vh;
74
+ box-sizing: border-box;
75
+ overflow-y: auto;
76
+ }
77
+
78
+ .gradio-container {
79
+ background-color: var(--app-bg);
80
+ border-radius: var(--border-radius-lg);
81
+ padding: clamp(20px, 5vw, 40px); /* Responsive padding */
82
+ box-shadow: 0 8px 25px var(--shadow-color);
83
+ max-width: var(--container-max-width);
84
+ width: 100%;
85
+ margin: 20px auto; /* Centering with margin for scroll */
86
+ border: none; /* Remove default Gradio border */
87
+ }
88
+
89
+ /* Hide default Gradio Form styling that might interfere */
90
+ .form.svelte-633qhp, .block.svelte-11xb1hd {
91
+ background: none !important;
92
+ border: none !important;
93
+ box-shadow: none !important;
94
+ padding: 0 !important; /* Reset padding if it causes issues */
95
+ }
96
+
97
+ h1 { /* Targets the main title */
98
+ color: var(--text-primary);
99
+ font-size: clamp(24px, 5vw, 32px); /* Responsive font size */
100
+ font-weight: 700;
101
+ text-align: center;
102
+ margin-bottom: 25px;
103
+ letter-spacing: -0.5px;
104
+ }
105
+
106
+ .app-description p {
107
+ color: var(--text-secondary);
108
+ font-size: clamp(14px, 2.5vw, 16px);
109
+ line-height: 1.7;
110
+ margin-bottom: 15px;
111
+ }
112
+
113
+ .app-description .instruction-text {
114
+ font-weight: 500;
115
+ color: var(--text-primary);
116
+ margin-top: 20px;
117
+ text-align: center;
118
+ }
119
+
120
+ .features-list {
121
+ list-style: none;
122
+ padding-left: 0;
123
+ margin: 20px 0;
124
+ }
125
+
126
+ .features-list li {
127
+ display: flex;
128
+ align-items: center;
129
+ font-size: clamp(14px, 2.5vw, 16px);
130
+ color: var(--text-secondary);
131
+ margin-bottom: 12px;
132
+ line-height: 1.6;
133
+ }
134
+
135
+ .features-list .icon {
136
+ margin-right: 12px;
137
+ font-size: 1.2em;
138
+ color: var(--accent-color);
139
+ }
140
+
141
+ .learn-more-link, .learn-more-link b {
142
+ color: var(--accent-color) !important;
143
+ text-decoration: none;
144
+ font-weight: 600;
145
+ }
146
+ .learn-more-link:hover, .learn-more-link:hover b {
147
+ color: var(--accent-color-darker) !important;
148
+ text-decoration: underline;
149
+ }
150
+
151
+ #text_input_box textarea { /* Targeting the textarea inside the Gradio Textbox component */
152
+ background-color: var(--input-bg);
153
+ border: 1px solid var(--border-color);
154
+ border-radius: var(--border-radius-md);
155
+ font-size: clamp(15px, 2.5vw, 16px);
156
+ padding: 15px;
157
+ width: 100%;
158
+ box-sizing: border-box;
159
+ color: var(--text-primary);
160
+ transition: border-color 0.3s ease, box-shadow 0.3s ease;
161
+ min-height: 120px; /* Ensure enough space for typing */
162
+ box-shadow: 0 2px 4px rgba(0,0,0,0.05);
163
  }
164
 
165
+ #text_input_box textarea::placeholder {
166
+ color: #B0BEC5; /* Lighter placeholder text */
167
+ }
168
+
169
+ #text_input_box textarea:focus {
170
+ border-color: var(--input-focus-border);
171
+ box-shadow: 0 0 0 3px rgba(26, 188, 156, 0.2);
172
+ outline: none;
173
+ }
174
+
175
+ #result_output_box { /* Style the Markdown block for results */
176
+ background-color: var(--input-bg);
177
+ border: 1px solid var(--border-color);
178
+ border-radius: var(--border-radius-md);
179
+ padding: 20px;
180
+ margin-top: 25px;
181
+ width: 100%;
182
+ box-sizing: border-box;
183
+ text-align: center;
184
+ font-size: clamp(16px, 3vw, 18px);
185
+ box-shadow: 0 4px 8px rgba(0,0,0,0.05);
186
+ }
187
+
188
+ .highlight-human, .highlight-ai {
189
+ font-weight: 600;
190
+ padding: 8px 15px;
191
+ border-radius: var(--border-radius-md);
192
+ display: inline-block; /* So padding applies correctly */
193
+ font-size: 1.1em;
194
+ margin-bottom: 5px; /* Space for sub-text if any */
195
+ }
196
+
197
+ .highlight-human {
198
+ color: var(--human-color);
199
+ background-color: var(--human-bg);
200
+ border: 1px solid var(--human-color);
201
+ }
202
+
203
+ .highlight-ai {
204
+ color: var(--ai-color);
205
+ background-color: var(--ai-bg);
206
+ border: 1px solid var(--ai-color);
207
+ }
208
+
209
+ /* Styling for Gradio Tabs and Examples */
210
+ .tabs > div:first-child button { /* Tab buttons */
211
+ background-color: transparent !important;
212
+ color: var(--text-secondary) !important;
213
+ border: none !important;
214
+ border-bottom: 2px solid transparent !important;
215
+ border-radius: 0 !important;
216
+ padding: 10px 15px !important;
217
+ font-weight: 500 !important;
218
+ transition: color 0.3s ease, border-bottom-color 0.3s ease !important;
219
+ }
220
+
221
+ .tabs > div:first-child button.selected { /* Selected tab */
222
+ color: var(--accent-color) !important;
223
+ border-bottom-color: var(--accent-color) !important;
224
+ font-weight: 600 !important;
225
+ }
226
+
227
+ .gr-examples { /* Container for examples */
228
+ padding: 15px !important;
229
+ border: 1px solid var(--border-color) !important;
230
+ border-radius: var(--border-radius-md) !important;
231
+ background-color: #fdfdfd !important;
232
+ }
233
+ .gr-sample-textbox { /* Example textboxes */
234
+ border: 1px solid var(--border-color) !important;
235
+ border-radius: var(--border-radius-md) !important;
236
+ font-size: 14px !important;
237
+ }
238
+
239
+ .footer-text, #bottom_text {
240
+ text-align: center;
241
+ margin-top: 40px;
242
+ font-size: clamp(13px, 2vw, 14px);
243
+ color: var(--text-secondary);
244
+ }
245
+ #bottom_text p { /* Ensure p tag inside Markdown inherits */
246
+ margin: 0;
247
+ }
248
+
249
+ /* Responsive adjustments */
250
+ @media (max-width: 768px) {
251
+ body {
252
+ padding: 10px;
253
+ align-items: flex-start; /* Keep at top for mobile */
254
+ }
255
+ .gradio-container {
256
+ padding: 20px;
257
+ margin: 10px; /* Reduce margin on mobile */
258
+ }
259
+ h1 {
260
+ font-size: 24px;
261
+ }
262
+ .app-description p, .features-list li {
263
+ font-size: 14px;
264
+ }
265
+ #text_input_box textarea {
266
+ font-size: 15px;
267
+ min-height: 100px;
268
+ }
269
+ #result_output_box {
270
+ font-size: 16px;
271
+ padding: 15px;
272
+ }
273
  }
274
  """
 
275
 
276
+ iface = gr.Blocks(css=modern_css, theme=gr.themes.Base(font=[gr.themes.GoogleFont("Inter"), "sans-serif"]))
 
277
 
 
 
278
  with iface:
279
+ gr.Markdown(f"<h1>{title}</h1>") # Use h1 directly for title
280
+ gr.Markdown(description) # HTML is now within the description string
281
+
282
+ text_input = gr.Textbox(
283
+ label="", # Label is visually less important now due to structure
284
+ placeholder="Type or paste your content here...",
285
+ elem_id="text_input_box",
286
+ lines=6 # Adjusted lines
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
  )
288
+ result_output = gr.HTML(elem_id="result_output_box") # Use HTML for richer output formatting
289
+
290
+ text_input.change(classify_text, inputs=text_input, outputs=result_output)
291
+
292
+ with gr.Row(): # Keep examples side-by-side if space allows, stack on mobile by default
293
+ with gr.Column(scale=1):
294
+ with gr.Accordion("AI Text Examples", open=False):
295
+ gr.Examples(
296
+ examples=AI_texts,
297
+ inputs=text_input,
298
+ label="Click an example to load it",
299
+ elem_classes="gr-examples"
300
+ )
301
+ with gr.Column(scale=1):
302
+ with gr.Accordion("Human Text Examples", open=False):
303
+ gr.Examples(
304
+ examples=Human_texts,
305
+ inputs=text_input,
306
+ label="Click an example to load it",
307
+ elem_classes="gr-examples"
308
+ )
309
+
310
+ gr.Markdown(bottom_text, elem_id="bottom_text")
311
 
312
+ iface.launch(share=False) # Set share=True if you need a public link