Update src/app.py
Browse files- 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="
|
| 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', '
|
| 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 |
-
"
|
|
|
|
| 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 |
-
|
| 190 |
-
|
| 191 |
-
with
|
| 192 |
-
|
| 193 |
-
|
| 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 |
-
|
| 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="
|
| 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 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
st.
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
st.
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 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:
|
| 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:
|
| 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'>
|