Backened commited on
Commit
9c8cbad
·
verified ·
1 Parent(s): 0ba24b8

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import os
4
+
5
+ REPO = os.getenv("HF_REPO_ID", "Backened/sarcasm-model")
6
+ LABELS = {"LABEL_0": "Not Sarcastic", "LABEL_1": "Sarcastic"}
7
+ CUES = [
8
+ "oh great","oh wow","oh sure","just what","so helpful","love how",
9
+ "clearly","obviously","yeah right","only took","amazing","wonderful",
10
+ "fantastic","totally","absolutely","of course","sure","as if",
11
+ ]
12
+
13
+ clf = pipeline("text-classification", model=REPO, device=-1)
14
+
15
+ def predict(text: str, threshold: float):
16
+ if not text.strip():
17
+ return "—", 0.0, "—", "—"
18
+
19
+ out = clf(text[:512])[0]
20
+ label = LABELS.get(out["label"], out["label"])
21
+ conf = round(float(out["score"]), 4)
22
+
23
+ if conf < threshold:
24
+ display = f"Uncertain (confidence {conf:.0%} < threshold {threshold:.0%})"
25
+ else:
26
+ display = label
27
+
28
+ signals = [f'"{c}"' for c in CUES if c in text.lower()]
29
+ sig_txt = "Detected: " + ", ".join(signals[:4]) if signals else "No common sarcasm cues"
30
+
31
+ bar = f"{conf:.0%} confident it is {label.lower()}"
32
+ return display, conf, sig_txt, bar
33
+
34
+
35
+ examples = [
36
+ ["Oh great, another Monday. Just what I needed.", 0.65],
37
+ ["I really enjoyed this product, works perfectly!", 0.65],
38
+ ["Sure, because that always works out so well.", 0.65],
39
+ ["Thank you for the quick response, very helpful!", 0.65],
40
+ ["Oh wow, my flight got cancelled again. Loving this airline.", 0.65],
41
+ ["The team did an excellent job, really proud of everyone.", 0.65],
42
+ ]
43
+
44
+ with gr.Blocks(title="Sarcasm Detector", theme=gr.themes.Soft()) as demo:
45
+ gr.Markdown(
46
+ """
47
+ # Sarcasm Detector
48
+ **Fine-tuned DistilBERT** trained on 120k+ samples across Reddit, Twitter & news headlines.
49
+ F1 score: **0.872** · Accuracy: **85.3%**
50
+
51
+ > Try the examples below or paste your own text.
52
+ """
53
+ )
54
+
55
+ with gr.Row():
56
+ with gr.Column(scale=3):
57
+ text_in = gr.Textbox(
58
+ label="Input text",
59
+ placeholder="Type something sarcastic (or not)...",
60
+ lines=3,
61
+ )
62
+ threshold = gr.Slider(
63
+ minimum=0.5, maximum=0.95, value=0.65, step=0.05,
64
+ label="Confidence threshold — below this returns 'Uncertain'",
65
+ )
66
+ btn = gr.Button("Detect", variant="primary")
67
+
68
+ with gr.Column(scale=2):
69
+ label_out = gr.Textbox(label="Prediction")
70
+ conf_out = gr.Number(label="Confidence score")
71
+ signal_out = gr.Textbox(label="Sarcasm signals found")
72
+ bar_out = gr.Textbox(label="Summary")
73
+
74
+ gr.Examples(examples=examples, inputs=[text_in, threshold])
75
+
76
+ btn.click(
77
+ fn=predict,
78
+ inputs=[text_in, threshold],
79
+ outputs=[label_out, conf_out, signal_out, bar_out],
80
+ )
81
+ text_in.submit(
82
+ fn=predict,
83
+ inputs=[text_in, threshold],
84
+ outputs=[label_out, conf_out, signal_out, bar_out],
85
+ )
86
+
87
+ gr.Markdown(
88
+ """
89
+ ---
90
+ **Model:** `Backened/sarcasm-model` on HuggingFace Hub
91
+ **Code:** [GitHub](https://github.com/YOUR_USERNAME/sarcasm-detector)
92
+ **API:** Deployed via Render.com
93
+ """
94
+ )
95
+
96
+ if __name__ == "__main__":
97
+ demo.launch()