AnonymousPaperSubmission123 commited on
Commit
6563642
·
verified ·
1 Parent(s): 4bc18dc

Update src/app.py

Browse files
Files changed (1) hide show
  1. src/app.py +198 -112
src/app.py CHANGED
@@ -6,6 +6,8 @@ from pathlib import Path
6
  # -----------------
7
  APP_DIR = Path(__file__).parent.resolve()
8
 
 
 
9
  # -----------------
10
  # Fix Streamlit Permission Issues
11
  # -----------------
@@ -34,13 +36,75 @@ import streamlit as st
34
  from FineTune.model import ComputeStat
35
  import time
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  # -----------------
38
  # Page Configuration
39
  # -----------------
40
  st.set_page_config(
41
- page_title="AdaDetectGPT",
42
- page_icon="🔍",
43
- layout="wide"
44
  )
45
 
46
  # -----------------
@@ -113,7 +177,7 @@ from feedback import FeedbackManager
113
  # Initialize Feedback Manager with HF dataset
114
  # 请将 'your-username/your-dataset-name' 替换为您的实际 HF 数据集仓库 ID
115
  # 确保在环境变量中设置了 HF_TOKEN 以访问私有数据集
116
- FEEDBACK_DATASET_ID = os.environ.get('FEEDBACK_DATASET_ID', 'mamba413/user-feedback')
117
  feedback_manager = FeedbackManager(
118
  dataset_repo_id=FEEDBACK_DATASET_ID,
119
  hf_token=os.environ.get('HF_TOKEN'),
@@ -166,17 +230,15 @@ if 'feedback_given' not in st.session_state:
166
  # -----------------
167
  # Streamlit Layout
168
  # -----------------
169
- _, col0, _ = st.columns((1, 5, 1))
170
- with col0:
171
- st.markdown(
172
- "<h1 style='text-align: center; color: #0072C3;'>AdaDetectGPT: Adaptive LLM's Texts Detection</h1>",
173
- unsafe_allow_html=True,
174
- )
175
-
176
  st.markdown(
177
- """Pasted the text to be detected below and click the 'Detect' button to get the p-value. Use a better option may improve detection."""
 
178
  )
179
 
 
 
 
 
180
  # Display model loading status
181
  if not model_loaded:
182
  st.error(f"❌ Failed to load model: {error_message}")
@@ -186,40 +248,59 @@ if not model_loaded:
186
  # Main Interface
187
  # -----------------
188
  # --- Two columns: Input text & button | Result displays ---
189
- col1, col2 = st.columns((1, 1))
190
-
191
- with col1:
192
- text_input = st.text_area(
193
- label="",
194
- placeholder="Paste your text to be detected here",
195
- help="Typically, providing text with a longer content would get a more reliable result.",
196
- height=200,
197
- )
198
-
199
- detect_clicked = st.button("Detect", type="primary", use_container_width=True)
200
 
201
- selected_domain = st.selectbox(
202
- label="⚙️ Domain (Optional)",
203
- options=DOMAINS,
204
- index=0, # Default to General
205
- help="💡 **Tip:** Select the domain that best matches your text for improving detection accuracy. Default is 'General' that means consider all domains."
206
- )
207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  with col2:
209
  statistics_ph = st.empty()
210
  statistics_ph.text_input(
211
- label="Statistics",
212
  value="",
 
213
  disabled=True,
214
- help="Statistics will appear here after clicking the Detect button.",
215
  )
216
-
 
217
  pvalue_ph = st.empty()
218
  pvalue_ph.text_input(
219
  label="p-value",
220
  value="",
 
 
 
 
 
 
 
 
 
 
221
  disabled=True,
222
- help="p-value will appear here after clicking the Detect button.",
223
  )
224
 
225
  # -----------------
@@ -267,83 +348,88 @@ if detect_clicked:
267
  }
268
 
269
  # Update score displays
270
- with col2:
271
- statistics_ph.text_input(
272
- label="Statistics",
273
- value=f"{crit:.6f}",
274
- disabled=True,
275
- help="Detection statistics will appear here after clicking Detect.",
276
- )
277
-
278
- pvalue_ph.text_input(
279
- label="p-value",
280
- value=f"{p_value:.6f}",
281
- disabled=True,
282
- help="p-value will appear here after clicking Detect.",
283
- )
284
-
285
- st.info(
286
- """
287
- **📊 p-value:**
288
- - **Lower p-value** (closer to 0) indicates text is **more likely AI-generated**
289
- - **Higher p-value** (closer to 1) indicates text is **more likely human-written**
290
- - Generally, p-value < 0.05 suggests the text may be LLM-generated
291
- """,
292
- icon="💡"
293
- )
294
-
295
- # ========== 🆕 Feedback buttons (moved here for better UX) ==========
296
- st.markdown("**📝 Result Feedback**: Does this detection result meet your expectations?")
297
-
298
- current_text = text_input
299
- current_domain = selected_domain
300
- current_statistics = crit
301
- current_pvalue = p_value
302
- feedback_col1, feedback_col2 = st.columns(2)
303
-
304
- with feedback_col1:
305
- if st.button("✅ Expected", use_container_width=True, type="secondary", key=f"expected_btn_{hash(text_input[:50])}"):
306
- try:
307
- success, message = feedback_manager.save_feedback(
308
- current_text,
309
- current_domain,
310
- current_statistics,
311
- current_pvalue,
312
- 'expected'
313
- )
314
- if success:
315
- st.success("✅ Thank you for your feedback!")
316
- st.caption(f"💾 {message}")
317
- else:
318
- st.error(f"Failed to save feedback: {message}")
319
- except Exception as e:
320
- st.error(f"Failed to save feedback: {str(e)}")
321
- import traceback
322
- st.code(traceback.format_exc())
323
-
324
- with feedback_col2:
325
- if st.button(" Unexpected", use_container_width=True, type="secondary", key=f"unexpected_btn_{hash(text_input[:50])}"):
326
- try:
327
- success, message = feedback_manager.save_feedback(
328
- current_text,
329
- current_domain,
330
- current_statistics,
331
- current_pvalue,
332
- 'unexpected'
333
- )
334
- if success:
335
- st.warning("❌ Feedback recorded! This will help us improve.")
336
- st.caption(f"💾 {message}")
337
- else:
338
- st.error(f"Failed to save feedback: {message}")
339
- except Exception as e:
340
- st.error(f"Failed to save feedback: {str(e)}")
341
- import traceback
342
- st.code(traceback.format_exc())
343
-
344
- if st.session_state.feedback_given:
345
- st.success(" Feedback submitted successfully!")
346
- # ============================================
 
 
 
 
 
347
 
348
  # Show detailed results
349
  with result_placeholder:
@@ -388,14 +474,14 @@ st.markdown(
388
  background-color: white;
389
  color: gray;
390
  text-align: center;
391
- padding: 10px;
392
  border-top: 1px solid #e0e0e0;
393
  z-index: 999;
394
  }
395
 
396
  /* Add padding to main content to prevent overlap with fixed footer */
397
  .main .block-container {
398
- padding-bottom: 60px;
399
  }
400
  </style>
401
  <div class='footer'>
 
6
  # -----------------
7
  APP_DIR = Path(__file__).parent.resolve()
8
 
9
+ account_name = 'mamba413'
10
+
11
  # -----------------
12
  # Fix Streamlit Permission Issues
13
  # -----------------
 
36
  from FineTune.model import ComputeStat
37
  import time
38
 
39
+ st.markdown(
40
+ """
41
+ <style>
42
+ /* Text area & text input */
43
+ textarea, input[type="text"] {
44
+ background-color: #f8fafc !important;
45
+ border: 1px solid #e5e7eb !important;
46
+ color: #111827 !important;
47
+ }
48
+
49
+ textarea::placeholder {
50
+ color: #9ca3af !important;
51
+ }
52
+
53
+ /* Selectbox */
54
+ div[data-testid="stSelectbox"] > div {
55
+ background-color: #f8fafc !important;
56
+ border: 1px solid #e5e7eb !important;
57
+ }
58
+ </style>
59
+ """,
60
+ unsafe_allow_html=True
61
+ )
62
+
63
+ st.markdown(
64
+ """
65
+ <style>
66
+ /* Detect button */
67
+ div.stButton > button[kind="primary"] {
68
+ background-color: #fdae6b;
69
+ border: white;
70
+ color: black;
71
+ font-weight: 600;
72
+ height: 4.3rem;
73
+
74
+ font-size: 1.1rem;
75
+
76
+ display: flex;
77
+ align-items: center;
78
+ justify-content: center;
79
+ gap: 0.55rem;
80
+ }
81
+
82
+ /* Icon inside Detect button */
83
+ div.stButton > button[kind="primary"] span {
84
+ font-size: 1.25rem;
85
+ line-height: 1;
86
+ }
87
+
88
+ div.stButton > button[kind="primary"]:hover {
89
+ background-color: #fd8d3c;
90
+ border-color: white;
91
+ }
92
+
93
+ div.stButton > button[kind="primary"]:active {
94
+ background-color: #fd8d3c;
95
+ border-color: white;
96
+ }
97
+ </style>
98
+ """,
99
+ unsafe_allow_html=True
100
+ )
101
+
102
  # -----------------
103
  # Page Configuration
104
  # -----------------
105
  st.set_page_config(
106
+ page_title="DetectGPTPro",
107
+ page_icon="🕵️",
 
108
  )
109
 
110
  # -----------------
 
177
  # Initialize Feedback Manager with HF dataset
178
  # 请将 'your-username/your-dataset-name' 替换为您的实际 HF 数据集仓库 ID
179
  # 确保在环境变量中设置了 HF_TOKEN 以访问私有数据集
180
+ FEEDBACK_DATASET_ID = os.environ.get('FEEDBACK_DATASET_ID', f'{account_name}/user-feedback')
181
  feedback_manager = FeedbackManager(
182
  dataset_repo_id=FEEDBACK_DATASET_ID,
183
  hf_token=os.environ.get('HF_TOKEN'),
 
230
  # -----------------
231
  # Streamlit Layout
232
  # -----------------
 
 
 
 
 
 
 
233
  st.markdown(
234
+ "<h1 style='text-align: center;'> 🕵️ Adaptive LLM-texts Detector</h1>",
235
+ unsafe_allow_html=True,
236
  )
237
 
238
+ # st.markdown(
239
+ # """Pasted the text to be detected below and click the 'Detect' button to get the p-value. Use a better option may improve detection."""
240
+ # )
241
+
242
  # Display model loading status
243
  if not model_loaded:
244
  st.error(f"❌ Failed to load model: {error_message}")
 
248
  # Main Interface
249
  # -----------------
250
  # --- Two columns: Input text & button | Result displays ---
251
+ text_input = st.text_area(
252
+ label="",
253
+ placeholder="Paste your text to be detected here. Typically, providing text with a longer content would get a more reliable result.",
254
+ height=240,
255
+ )
 
 
 
 
 
 
256
 
257
+ subcol11, subcol12, subcol13 = st.columns((1, 1, 1))
 
 
 
 
 
258
 
259
+ selected_domain = subcol11.selectbox(
260
+ label="💡 Domain that matches your text",
261
+ options=DOMAINS,
262
+ index=0, # Default to General
263
+ # label_visibility="collapsed",
264
+ # label_visibility="hidden",
265
+ )
266
+
267
+ detect_clicked = subcol12.button("🔍 Detect", type="primary", use_container_width=True)
268
+
269
+ selected_level = subcol13.slider(
270
+ label="Significance (α)",
271
+ min_value=0.01,
272
+ max_value=0.2,
273
+ value=0.05,
274
+ step=0.005,
275
+ # label_visibility="collapsed",
276
+ )
277
+
278
+ col2, col3, col4 = st.columns((1, 1, 2))
279
  with col2:
280
  statistics_ph = st.empty()
281
  statistics_ph.text_input(
282
+ label="Statistic",
283
  value="",
284
+ placeholder="",
285
  disabled=True,
 
286
  )
287
+
288
+ with col3:
289
  pvalue_ph = st.empty()
290
  pvalue_ph.text_input(
291
  label="p-value",
292
  value="",
293
+ placeholder="",
294
+ disabled=True,
295
+ )
296
+
297
+ with col4:
298
+ conclusion_ph = st.empty()
299
+ conclusion_ph.text_input(
300
+ label="Conclusion",
301
+ value="",
302
+ placeholder="",
303
  disabled=True,
 
304
  )
305
 
306
  # -----------------
 
348
  }
349
 
350
  # Update score displays
351
+ statistics_ph.text_input(
352
+ label="Statistics",
353
+ value=f"{crit:.6f}",
354
+ disabled=True,
355
+ help="Detection statistics will appear here after clicking Detect.",
356
+ )
357
+
358
+ pvalue_ph.text_input(
359
+ label="p-value",
360
+ value=f"{p_value:.6f}",
361
+ disabled=True,
362
+ help="p-value will appear here after clicking Detect.",
363
+ )
364
+
365
+ conclusion_ph.text_input(
366
+ label="Conclusion",
367
+ value="Reject H0: Text is likely LLM-generated." if p_value < selected_level else "Fail to Reject H0: Text is likely human-written.",
368
+ disabled=True,
369
+ help="Conclusion will appear here after clicking Detect.",
370
+ )
371
+
372
+ st.info(
373
+ """
374
+ **📊 p-value:**
375
+ - **Lower p-value** (closer to 0) indicates text is **more likely AI-generated**
376
+ - **Higher p-value** (closer to 1) indicates text is **more likely human-written**
377
+ """,
378
+ icon="💡"
379
+ )
380
+
381
+ # ========== 🆕 Feedback buttons (moved here for better UX) ==========
382
+ st.markdown("**📝 Result Feedback**: Does this detection result meet your expectations?")
383
+
384
+ current_text = text_input
385
+ current_domain = selected_domain
386
+ current_statistics = crit
387
+ current_pvalue = p_value
388
+ feedback_col1, feedback_col2 = st.columns(2)
389
+
390
+ with feedback_col1:
391
+ if st.button("✅ Expected", use_container_width=True, type="secondary", key=f"expected_btn_{hash(text_input[:50])}"):
392
+ try:
393
+ success, message = feedback_manager.save_feedback(
394
+ current_text,
395
+ current_domain,
396
+ current_statistics,
397
+ current_pvalue,
398
+ 'expected'
399
+ )
400
+ if success:
401
+ st.success(" Thank you for your feedback!")
402
+ st.caption(f"💾 {message}")
403
+ else:
404
+ st.error(f"Failed to save feedback: {message}")
405
+ except Exception as e:
406
+ st.error(f"Failed to save feedback: {str(e)}")
407
+ import traceback
408
+ st.code(traceback.format_exc())
409
+
410
+ with feedback_col2:
411
+ if st.button("❌ Unexpected", use_container_width=True, type="secondary", key=f"unexpected_btn_{hash(text_input[:50])}"):
412
+ try:
413
+ success, message = feedback_manager.save_feedback(
414
+ current_text,
415
+ current_domain,
416
+ current_statistics,
417
+ current_pvalue,
418
+ 'unexpected'
419
+ )
420
+ if success:
421
+ st.warning(" Feedback recorded! This will help us improve.")
422
+ st.caption(f"💾 {message}")
423
+ else:
424
+ st.error(f"Failed to save feedback: {message}")
425
+ except Exception as e:
426
+ st.error(f"Failed to save feedback: {str(e)}")
427
+ import traceback
428
+ st.code(traceback.format_exc())
429
+
430
+ if st.session_state.feedback_given:
431
+ st.success("✅ Feedback submitted successfully!")
432
+ # ============================================
433
 
434
  # Show detailed results
435
  with result_placeholder:
 
474
  background-color: white;
475
  color: gray;
476
  text-align: center;
477
+ padding: 1px;
478
  border-top: 1px solid #e0e0e0;
479
  z-index: 999;
480
  }
481
 
482
  /* Add padding to main content to prevent overlap with fixed footer */
483
  .main .block-container {
484
+ padding-bottom: 1px;
485
  }
486
  </style>
487
  <div class='footer'>