File size: 3,262 Bytes
9c8cbad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import gradio as gr
from transformers import pipeline
import os

REPO   = os.getenv("HF_REPO_ID", "Backened/sarcasm-model")
LABELS = {"LABEL_0": "Not Sarcastic", "LABEL_1": "Sarcastic"}
CUES   = [
    "oh great","oh wow","oh sure","just what","so helpful","love how",
    "clearly","obviously","yeah right","only took","amazing","wonderful",
    "fantastic","totally","absolutely","of course","sure","as if",
]

clf = pipeline("text-classification", model=REPO, device=-1)

def predict(text: str, threshold: float):
    if not text.strip():
        return "—", 0.0, "—", "—"

    out   = clf(text[:512])[0]
    label = LABELS.get(out["label"], out["label"])
    conf  = round(float(out["score"]), 4)

    if conf < threshold:
        display = f"Uncertain  (confidence {conf:.0%} < threshold {threshold:.0%})"
    else:
        display = label

    signals = [f'"{c}"' for c in CUES if c in text.lower()]
    sig_txt = "Detected: " + ", ".join(signals[:4]) if signals else "No common sarcasm cues"

    bar = f"{conf:.0%} confident it is {label.lower()}"
    return display, conf, sig_txt, bar


examples = [
    ["Oh great, another Monday. Just what I needed.", 0.65],
    ["I really enjoyed this product, works perfectly!", 0.65],
    ["Sure, because that always works out so well.", 0.65],
    ["Thank you for the quick response, very helpful!", 0.65],
    ["Oh wow, my flight got cancelled again. Loving this airline.", 0.65],
    ["The team did an excellent job, really proud of everyone.", 0.65],
]

with gr.Blocks(title="Sarcasm Detector", theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        # Sarcasm Detector
        **Fine-tuned DistilBERT** trained on 120k+ samples across Reddit, Twitter & news headlines.
        F1 score: **0.872** · Accuracy: **85.3%**

        > Try the examples below or paste your own text.
        """
    )

    with gr.Row():
        with gr.Column(scale=3):
            text_in = gr.Textbox(
                label="Input text",
                placeholder="Type something sarcastic (or not)...",
                lines=3,
            )
            threshold = gr.Slider(
                minimum=0.5, maximum=0.95, value=0.65, step=0.05,
                label="Confidence threshold — below this returns 'Uncertain'",
            )
            btn = gr.Button("Detect", variant="primary")

        with gr.Column(scale=2):
            label_out  = gr.Textbox(label="Prediction")
            conf_out   = gr.Number(label="Confidence score")
            signal_out = gr.Textbox(label="Sarcasm signals found")
            bar_out    = gr.Textbox(label="Summary")

    gr.Examples(examples=examples, inputs=[text_in, threshold])

    btn.click(
        fn=predict,
        inputs=[text_in, threshold],
        outputs=[label_out, conf_out, signal_out, bar_out],
    )
    text_in.submit(
        fn=predict,
        inputs=[text_in, threshold],
        outputs=[label_out, conf_out, signal_out, bar_out],
    )

    gr.Markdown(
        """
        ---
        **Model:** `Backened/sarcasm-model` on HuggingFace Hub  
        **Code:** [GitHub](https://github.com/YOUR_USERNAME/sarcasm-detector)  
        **API:** Deployed via Render.com
        """
    )

if __name__ == "__main__":
    demo.launch()