Christine Zhou commited on
Commit
6a5af41
·
1 Parent(s): faf41dd
Files changed (2) hide show
  1. app.py +39 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
4
+
5
+ MODEL_ID = "cchristinezhou/comment-tone-classifier-demo"
6
+
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
8
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
9
+ device = 0 if torch.cuda.is_available() else -1
10
+ pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, device=device, return_all_scores=True)
11
+
12
+ # Optional warmup to reduce first-call latency
13
+ _ = pipe("warmup")
14
+
15
+ def predict(text, max_length=256):
16
+ if not text or not text.strip():
17
+ return {}
18
+ out = pipe(text, truncation=True, max_length=max_length)[0] # list of {label, score}
19
+ return {d["label"]: float(d["score"]) for d in out}
20
+
21
+ demo = gr.Interface(
22
+ fn=predict,
23
+ inputs=[
24
+ gr.Textbox(lines=4, placeholder="Paste a comment…"),
25
+ gr.Slider(64, 512, value=256, step=32, label="Max tokens"),
26
+ ],
27
+ outputs=gr.Label(num_top_classes=7),
28
+ title="Tone Classifier (BERT)",
29
+ description="Predicts tone across 7 categories: Supportive, Neutral, Passive-Aggressive, Blunt/Criticism, Emotionally Heavy, Objectifying, Parasocial Overreach.",
30
+ examples=[
31
+ ["You’re amazing, thanks for sharing!"],
32
+ ["lol okay sure 🙄"],
33
+ ["This wasn’t great—here’s why…"],
34
+ ["I feel overwhelmed reading this ngl."],
35
+ ],
36
+ )
37
+ demo.queue() # handles concurrent users
38
+ if __name__ == "__main__":
39
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio>=4.0.0
2
+ transformers
3
+ torch
4
+ accelerate