Navsatitagain commited on
Commit
67a8fe6
·
verified ·
1 Parent(s): 36013b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -15
app.py CHANGED
@@ -1,21 +1,68 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
- text_emotion = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
 
 
5
 
6
- def analyze_emotion(text):
7
- results = text_emotion(text)[0]
8
- results = sorted(results, key=lambda x: x['score'], reverse=True)
9
- output = {r['label']: round(r['score'], 3) for r in results}
10
- return output
11
 
12
- demo = gr.Interface(
13
- fn=analyze_emotion,
14
- inputs=gr.Textbox(lines=3, placeholder="Type something here..."),
15
- outputs=gr.Label(num_top_classes=3),
16
- title="Empath AI - Emotion Detection",
17
- description="Type a sentence to see what emotions it contains!"
18
- )
19
 
20
- if __name__ == "__main__":
21
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from PIL import Image
4
 
5
+ TEXT_MODEL = "j-hartmann/emotion-english-distilroberta-base"
6
+ IMAGE_MODEL = "trpakov/vit-face-expression"
7
+ AUDIO_MODEL = "superb/hubert-large-superb-er"
8
 
9
+ text_pipe = pipeline("text-classification", model=TEXT_MODEL, return_all_scores=True)
10
+ image_pipe = pipeline("image-classification", model=IMAGE_MODEL, top_k=None)
11
+ audio_pipe = pipeline("audio-classification", model=AUDIO_MODEL, top_k=None)
 
 
12
 
13
+ def _as_label_dict(preds):
14
+
15
+ preds_sorted = sorted(preds, key=lambda p: p["score"], reverse=True)
16
+ return {p["label"]: float(round(p["score"], 4)) for p in preds_sorted}
 
 
 
17
 
18
+ def analyze_text(text: str):
19
+ if not text or not text.strip():
20
+ return {"(enter some text)": 1.0}
21
+ preds = text_pipe(text)[0]
22
+ return _as_label_dict(preds)
23
+
24
+ def analyze_face(img):
25
+ if img is None:
26
+ return {"(no image)": 1.0}
27
+ if isinstance(img, Image.Image):
28
+ pil = img
29
+ else:
30
+ pil = Image.fromarray(img)
31
+ preds = image_pipe(pil)
32
+ return _as_label_dict(preds)
33
+
34
+ def analyze_voice(audio_path):
35
+ if audio_path is None:
36
+ return {"(no audio)": 1.0}
37
+ preds = audio_pipe(audio_path)
38
+ return _as_label_dict(preds)
39
+
40
+ with gr.Blocks(title="Empath AI — Multimodal Emotion Detection") as demo:
41
+ gr.Markdown(
42
+ """
43
+ # Empath AI — Emotion Detection (Text • Face • Voice)
44
+ Grant permission when the browser asks for **camera/microphone**.
45
+ Nothing is stored; analysis happens in memory and the scores are shown back to you.
46
+ """
47
+ )
48
+
49
+ with gr.Tab("Text"):
50
+ t_in = gr.Textbox(label="Enter text", lines=3, placeholder="Type something here…")
51
+ t_btn = gr.Button("Analyze Text", variant="primary")
52
+ t_out = gr.Label(num_top_classes=3)
53
+ t_btn.click(analyze_text, inputs=t_in, outputs=t_out)
54
+
55
+ with gr.Tab("Face (Webcam or Upload)"):
56
+ i_in = gr.Image(sources=["webcam", "upload"], type="pil", label="Webcam / Upload")
57
+ i_btn = gr.Button("Analyze Face", variant="primary")
58
+ i_out = gr.Label(num_top_classes=3)
59
+ i_btn.click(analyze_face, inputs=i_in, outputs=i_out)
60
+
61
+ with gr.Tab("Voice (Mic or Upload)"):
62
+ a_in = gr.Audio(sources=["microphone", "upload"], type="filepath",
63
+ label="Record or upload a short clip (≤30s)")
64
+ a_btn = gr.Button("Analyze Voice", variant="primary")
65
+ a_out = gr.Label(num_top_classes=3)
66
+ a_btn.click(analyze_voice, inputs=a_in, outputs=a_out)
67
+
68
+ demo.launch()