Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,123 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from ai_text_detector_valid_final import detect_text
|
| 3 |
-
|
| 4 |
-
def analyze_text(user_text: str):
|
| 5 |
-
"""
|
| 6 |
-
Wrapper around your detection function.
|
| 7 |
-
We assume detect_text returns one of:
|
| 8 |
-
- a dict of {model_name: score, ...}
|
| 9 |
-
- a dict with mixed info
|
| 10 |
-
- a single label/string
|
| 11 |
-
"""
|
| 12 |
-
# Protect against empty input
|
| 13 |
-
if not user_text or not user_text.strip():
|
| 14 |
-
return {"error": "No text provided."}
|
| 15 |
-
|
| 16 |
-
try:
|
| 17 |
-
return detect_text(user_text)
|
| 18 |
-
except Exception as e:
|
| 19 |
-
# Graceful fallback if your detection code raises
|
| 20 |
-
return {"error": f"Detection failed: {e}"}
|
| 21 |
-
|
| 22 |
-
def run_analysis(user_text: str):
|
| 23 |
-
raw = analyze_text(user_text)
|
| 24 |
-
|
| 25 |
-
# Normalize outputs into:
|
| 26 |
-
# final_result: dict for the "Final Results" JSON
|
| 27 |
-
# detailed: dict for the "All Model Scores" JSON
|
| 28 |
-
final_result = {}
|
| 29 |
-
detailed = {}
|
| 30 |
-
|
| 31 |
-
# If detect_text returned an error or string, pass it through
|
| 32 |
-
if isinstance(raw, str):
|
| 33 |
-
final_result = {"Final Prediction": raw}
|
| 34 |
-
detailed = {"result": raw}
|
| 35 |
-
return final_result, detailed
|
| 36 |
-
|
| 37 |
-
if isinstance(raw, dict):
|
| 38 |
-
# If there's an explicit 'final' key, use it
|
| 39 |
-
if "final" in raw or "final_prediction" in raw or "prediction" in raw:
|
| 40 |
-
# Normalize keys to a friendly output
|
| 41 |
-
final_key = raw.get("final") or raw.get("final_prediction") or raw.get("prediction")
|
| 42 |
-
final_result = {"Final Prediction": final_key}
|
| 43 |
-
detailed = raw
|
| 44 |
-
return final_result, detailed
|
| 45 |
-
|
| 46 |
-
# Extract numeric model scores if present (floats/ints in [0,1] or 0-100)
|
| 47 |
-
numeric_scores = {}
|
| 48 |
-
non_numeric = {}
|
| 49 |
-
for k, v in raw.items():
|
| 50 |
-
if isinstance(v, (int, float)):
|
| 51 |
-
numeric_scores[k] = float(v)
|
| 52 |
-
else:
|
| 53 |
-
non_numeric[k] = v
|
| 54 |
-
|
| 55 |
-
if numeric_scores:
|
| 56 |
-
# normalize if scores look like 0-100 -> convert to 0-1
|
| 57 |
-
vals = list(numeric_scores.values())
|
| 58 |
-
max_val = max(vals)
|
| 59 |
-
if max_val > 1.01: # probably 0-100 scale
|
| 60 |
-
numeric_scores = {k: v / 100.0 for k, v in numeric_scores.items()}
|
| 61 |
-
|
| 62 |
-
avg_prob = sum(numeric_scores.values()) / len(numeric_scores)
|
| 63 |
-
label = "Likely AI" if avg_prob >= 0.5 else "Likely Human"
|
| 64 |
-
|
| 65 |
-
final_result = {
|
| 66 |
-
"Final Prediction": label,
|
| 67 |
-
"Average Probability (0-1)": round(avg_prob, 4),
|
| 68 |
-
"Model Count": len(numeric_scores)
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
-
detailed = {
|
| 72 |
-
"model_scores": {k: round(v, 4) for k, v in numeric_scores.items()}
|
| 73 |
-
}
|
| 74 |
-
if non_numeric:
|
| 75 |
-
detailed["other_info"] = non_numeric
|
| 76 |
-
|
| 77 |
-
return final_result, detailed
|
| 78 |
-
|
| 79 |
-
# If no numeric scores, but we got a structured dict, just show it
|
| 80 |
-
final_result = {"Final Prediction (raw)": "See detailed results"}
|
| 81 |
-
detailed = raw
|
| 82 |
-
return final_result, detailed
|
| 83 |
-
|
| 84 |
-
# Fallback: unknown return type
|
| 85 |
-
return {"Final Prediction": str(raw)}, {"raw": raw}
|
| 86 |
|
|
|
|
|
|
|
| 87 |
|
| 88 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 89 |
gr.Markdown(
|
| 90 |
"""
|
| 91 |
# 🔍 AI vs Human Text Detector
|
| 92 |
-
Paste any text below and our system will analyze it using
|
| 93 |
-
and
|
| 94 |
"""
|
| 95 |
)
|
| 96 |
|
| 97 |
-
with gr.Row():
|
| 98 |
-
with gr.Column(scale=2):
|
| 99 |
-
user_input = gr.Textbox(
|
| 100 |
-
label="✍️ Enter Text",
|
| 101 |
-
placeholder="Paste text here...",
|
| 102 |
-
lines=10
|
| 103 |
-
)
|
| 104 |
-
analyze_btn = gr.Button("🚀 Run Detection", variant="primary")
|
| 105 |
-
|
| 106 |
-
with gr.Column(scale=1):
|
| 107 |
final_output = gr.JSON(label="📊 Final Results")
|
| 108 |
|
| 109 |
with gr.Accordion("🔬 Detailed Model Results", open=False):
|
| 110 |
-
model_output = gr.JSON(label="All Model Scores
|
| 111 |
|
| 112 |
-
def
|
| 113 |
-
|
| 114 |
-
return
|
| 115 |
|
| 116 |
analyze_btn.click(
|
| 117 |
-
fn=
|
| 118 |
inputs=user_input,
|
| 119 |
outputs=[final_output, model_output]
|
| 120 |
)
|
| 121 |
|
| 122 |
-
|
| 123 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from ai_text_detector_valid_final import detect_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
def analyze_text(user_text):
|
| 5 |
+
return detect_text(user_text)
|
| 6 |
|
| 7 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 8 |
gr.Markdown(
|
| 9 |
"""
|
| 10 |
# 🔍 AI vs Human Text Detector
|
| 11 |
+
Paste any text below and our system will analyze it using **3 different models**
|
| 12 |
+
and generate a final average probability.
|
| 13 |
"""
|
| 14 |
)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
final_output = gr.JSON(label="📊 Final Results")
|
| 17 |
|
| 18 |
with gr.Accordion("🔬 Detailed Model Results", open=False):
|
| 19 |
+
model_output = gr.JSON(label="All Model Scores")
|
| 20 |
|
| 21 |
+
def run_analysis(user_text):
|
| 22 |
+
results = analyze_text(user_text)
|
| 23 |
+
return results, results # send to both JSON outputs
|
| 24 |
|
| 25 |
analyze_btn.click(
|
| 26 |
+
fn=run_analysis,
|
| 27 |
inputs=user_input,
|
| 28 |
outputs=[final_output, model_output]
|
| 29 |
)
|
| 30 |
|
| 31 |
+
|
| 32 |
demo.launch()
|