Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from models import text_model, TEXT_FRIENDLY_MAP, image_model
|
| 3 |
+
|
| 4 |
+
# -----------------------------
|
| 5 |
+
# Helpers
|
| 6 |
+
# -----------------------------
|
| 7 |
+
def _friendly_text_label(raw_label: str) -> str:
|
| 8 |
+
# Normalize raw label from the text model to our friendly label
|
| 9 |
+
raw = (raw_label or "").strip().upper()
|
| 10 |
+
return TEXT_FRIENDLY_MAP.get(raw_label, TEXT_FRIENDLY_MAP.get(raw, raw_label))
|
| 11 |
+
|
| 12 |
+
def _to_label_dict(items, friendly=False, is_text=False):
|
| 13 |
+
"""
|
| 14 |
+
Convert pipeline outputs (list of dicts) to a {label: score} dict for gr.Label.
|
| 15 |
+
|
| 16 |
+
items: e.g. [{"label": "POSITIVE", "score": 0.98}, ...]
|
| 17 |
+
friendly: if True and is_text=True, map to friendly 'Real / Credible' or 'Fake / Not Credible'
|
| 18 |
+
"""
|
| 19 |
+
out = {}
|
| 20 |
+
for it in items:
|
| 21 |
+
lab = it.get("label", "")
|
| 22 |
+
if friendly and is_text:
|
| 23 |
+
lab = _friendly_text_label(lab)
|
| 24 |
+
out[lab] = float(it.get("score", 0.0))
|
| 25 |
+
return out
|
| 26 |
+
|
| 27 |
+
# -----------------------------
|
| 28 |
+
# Inference functions
|
| 29 |
+
# -----------------------------
|
| 30 |
+
def predict_text(text: str):
|
| 31 |
+
if not text or not text.strip():
|
| 32 |
+
return "Please paste some text first.", {"Waiting...": 1.0}
|
| 33 |
+
|
| 34 |
+
# Get top-2 to show a nice confidence breakdown
|
| 35 |
+
preds = text_model(text, top_k=2)
|
| 36 |
+
# When top_k is used, transformers returns a list of dicts directly
|
| 37 |
+
# Normalize to dict for gr.Label component
|
| 38 |
+
label_dict = _to_label_dict(preds, friendly=True, is_text=True)
|
| 39 |
+
|
| 40 |
+
# Pick top result (highest score)
|
| 41 |
+
top_label, top_score = max(label_dict.items(), key=lambda kv: kv[1])
|
| 42 |
+
summary = f"{top_label} ({round(top_score * 100, 2)}%)"
|
| 43 |
+
return summary, label_dict
|
| 44 |
+
|
| 45 |
+
def predict_image(image):
|
| 46 |
+
if image is None:
|
| 47 |
+
return "Please upload an image first.", {"Waiting...": 1.0}
|
| 48 |
+
|
| 49 |
+
# Get top-5 predictions for a nicer breakdown
|
| 50 |
+
preds = image_model(image, top_k=5)
|
| 51 |
+
label_dict = _to_label_dict(preds, friendly=False, is_text=False)
|
| 52 |
+
|
| 53 |
+
top_label, top_score = max(label_dict.items(), key=lambda kv: kv[1])
|
| 54 |
+
summary = f"{top_label} ({round(top_score * 100, 2)}%)"
|
| 55 |
+
return summary, label_dict
|
| 56 |
+
|
| 57 |
+
# -----------------------------
|
| 58 |
+
# UI
|
| 59 |
+
# -----------------------------
|
| 60 |
+
with gr.Blocks(title="AI Detection Hub") as demo:
|
| 61 |
+
gr.Markdown(
|
| 62 |
+
"""
|
| 63 |
+
# 🔎 AI Detection Hub
|
| 64 |
+
Analyze **text** (credibility demo) and **images** (generic classifier) with a clean, simple interface.
|
| 65 |
+
|
| 66 |
+
> ✅ Uses models that are known to load on Spaces without errors.
|
| 67 |
+
> 🧩 Later, you can swap in your own fine-tuned fake-news and deepfake detectors.
|
| 68 |
+
"""
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
with gr.Tab("📝 Text Analysis"):
|
| 72 |
+
gr.Markdown("Paste a claim or snippet. The model returns a **credibility-style** score (demo using sentiment).")
|
| 73 |
+
with gr.Row():
|
| 74 |
+
text_input = gr.Textbox(
|
| 75 |
+
label="Enter text",
|
| 76 |
+
placeholder="Paste news text or a claim here...",
|
| 77 |
+
lines=6
|
| 78 |
+
)
|
| 79 |
+
with gr.Row():
|
| 80 |
+
text_btn = gr.Button("Analyze", variant="primary")
|
| 81 |
+
text_clear = gr.Button("Clear")
|
| 82 |
+
with gr.Row():
|
| 83 |
+
text_summary = gr.Textbox(label="Result", interactive=False)
|
| 84 |
+
text_probs = gr.Label(label="Confidence")
|
| 85 |
+
|
| 86 |
+
examples = gr.Examples(
|
| 87 |
+
label="Try examples",
|
| 88 |
+
examples=[
|
| 89 |
+
["Breaking: Local council approves new park renovation after community vote."],
|
| 90 |
+
["Celebrity endorses miracle cure that doctors say is unproven and dangerous."],
|
| 91 |
+
],
|
| 92 |
+
inputs=[text_input],
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
text_btn.click(predict_text, inputs=text_input, outputs=[text_summary, text_probs])
|
| 96 |
+
text_clear.click(lambda: ("", "", {}), outputs=[text_input, text_summary, text_probs])
|
| 97 |
+
|
| 98 |
+
with gr.Tab("🖼️ Image Analysis"):
|
| 99 |
+
gr.Markdown("Upload an image. The model returns **top predicted classes** (generic classifier demo).")
|
| 100 |
+
with gr.Row():
|
| 101 |
+
image_input = gr.Image(type="pil", label="Upload image")
|
| 102 |
+
with gr.Row():
|
| 103 |
+
image_btn = gr.Button("Analyze", variant="primary")
|
| 104 |
+
image_clear = gr.Button("Clear")
|
| 105 |
+
with gr.Row():
|
| 106 |
+
image_summary = gr.Textbox(label="Result", interactive=False)
|
| 107 |
+
image_probs = gr.Label(label="Top-5 Confidence")
|
| 108 |
+
|
| 109 |
+
image_btn.click(predict_image, inputs=image_input, outputs=[image_summary, image_probs])
|
| 110 |
+
image_clear.click(lambda: (None, "", {}), outputs=[image_input, image_summary, image_probs])
|
| 111 |
+
|
| 112 |
+
# Required entry point
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
demo.launch()
|