Kaiyeee commited on
Commit
1b1eae0
·
verified ·
1 Parent(s): 57ccc66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -16
app.py CHANGED
@@ -6,21 +6,16 @@ from safetensors.torch import load_file as load_safetensors
6
  from transformers import AutoTokenizer
7
  from openvino.runtime import Core
8
  import gradio as gr
 
9
 
10
- # 1) Model repo on HF Hub
11
  HF_MODEL = "Kaiyeee/goemotions-multilabel"
12
-
13
- # 2) Load tokenizer once
14
  tokenizer = AutoTokenizer.from_pretrained("roberta-base")
15
-
16
- # 3) Load and compile ONNX with OpenVINO on first request
17
  core = Core()
18
- # download and cache the .onnx from the model repo
19
  onnx_path = hf_hub_download(repo_id=HF_MODEL, filename="goemotions_multilabel.onnx")
20
  ov_model = core.read_model(model=onnx_path)
21
  compiled = core.compile_model(model=ov_model, device_name="CPU")
22
 
23
- # 4) Emotion labels
24
  emotion_labels = [
25
  "admiration","amusement","anger","annoyance","approval","caring","confusion",
26
  "curiosity","desire","disappointment","disapproval","disgust","embarrassment",
@@ -29,27 +24,51 @@ emotion_labels = [
29
  ]
30
 
31
  def predict(texts, threshold=0.3):
32
- # tokenize to numpy
33
  toks = tokenizer(texts, padding="max_length", truncation=True, max_length=128, return_tensors="np")
34
  outs = compiled([toks["input_ids"], toks["attention_mask"]])
35
  logits = outs[compiled.output(0)]
36
- probs = 1 / (1 + np.exp(-logits))
37
- preds = (probs > threshold).astype(int)
38
 
39
- # map back
40
  results = []
41
  for i, ps in enumerate(preds):
42
  fired = [emotion_labels[j] for j, flag in enumerate(ps) if flag]
43
  results.append(", ".join(fired) or "none")
44
  return results
45
 
46
- # 5) Gradio UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  with gr.Blocks() as demo:
48
  gr.Markdown("# 👀 GoEmotions Multi-Label Demo")
49
- inp = gr.Textbox(label="Enter text", lines=3, placeholder="How are you feeling today?")
50
  thr = gr.Slider(0.1, 0.9, 0.3, label="Threshold")
51
- out = gr.Textbox(label="Predicted emotions")
52
- btn = gr.Button("Analyze")
53
- btn.click(fn=predict, inputs=[inp, thr], outputs=out)
 
 
 
 
 
 
 
 
 
54
 
55
  demo.launch()
 
6
  from transformers import AutoTokenizer
7
  from openvino.runtime import Core
8
  import gradio as gr
9
+ import pandas as pd
10
 
11
+ # Model & tokenizer loading (same as before)
12
  HF_MODEL = "Kaiyeee/goemotions-multilabel"
 
 
13
  tokenizer = AutoTokenizer.from_pretrained("roberta-base")
 
 
14
  core = Core()
 
15
  onnx_path = hf_hub_download(repo_id=HF_MODEL, filename="goemotions_multilabel.onnx")
16
  ov_model = core.read_model(model=onnx_path)
17
  compiled = core.compile_model(model=ov_model, device_name="CPU")
18
 
 
19
  emotion_labels = [
20
  "admiration","amusement","anger","annoyance","approval","caring","confusion",
21
  "curiosity","desire","disappointment","disapproval","disgust","embarrassment",
 
24
  ]
25
 
26
  def predict(texts, threshold=0.3):
 
27
  toks = tokenizer(texts, padding="max_length", truncation=True, max_length=128, return_tensors="np")
28
  outs = compiled([toks["input_ids"], toks["attention_mask"]])
29
  logits = outs[compiled.output(0)]
30
+ probs = 1 / (1 + np.exp(-logits))
31
+ preds = (probs > threshold).astype(int)
32
 
 
33
  results = []
34
  for i, ps in enumerate(preds):
35
  fired = [emotion_labels[j] for j, flag in enumerate(ps) if flag]
36
  results.append(", ".join(fired) or "none")
37
  return results
38
 
39
+ # Process multiline text input
40
+ def predict_bulk(texts_str, threshold=0.3):
41
+ texts = [line.strip() for line in texts_str.split("\n") if line.strip()]
42
+ results = predict(texts, threshold)
43
+ return "\n".join(f"{t}: {r}" for t, r in zip(texts, results))
44
+
45
+ # Process CSV file upload
46
+ def predict_file(file_obj, threshold=0.3):
47
+ df = pd.read_csv(file_obj.name)
48
+ if 'text' not in df.columns:
49
+ return "CSV must have a 'text' column."
50
+ texts = df['text'].astype(str).tolist()
51
+ results = predict(texts, threshold)
52
+ df['emotions'] = results
53
+ out_path = "predictions.csv"
54
+ df.to_csv(out_path, index=False)
55
+ return out_path
56
+
57
  with gr.Blocks() as demo:
58
  gr.Markdown("# 👀 GoEmotions Multi-Label Demo")
59
+
60
  thr = gr.Slider(0.1, 0.9, 0.3, label="Threshold")
61
+
62
+ with gr.Tab("Paste Text (one per line)"):
63
+ inp = gr.Textbox(label="Enter texts (one per line)", lines=10, placeholder="Enter sentences here")
64
+ out = gr.Textbox(label="Predicted emotions")
65
+ btn = gr.Button("Analyze")
66
+ btn.click(fn=predict_bulk, inputs=[inp, thr], outputs=out)
67
+
68
+ with gr.Tab("Upload CSV"):
69
+ file_inp = gr.File(label="Upload CSV with a 'text' column")
70
+ out_file = gr.File(label="Download CSV with emotions")
71
+ file_btn = gr.Button("Analyze CSV")
72
+ file_btn.click(fn=predict_file, inputs=[file_inp, thr], outputs=out_file)
73
 
74
  demo.launch()