Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,48 +2,76 @@ import os
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
|
5 |
-
#
|
6 |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1"
|
7 |
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_TOKEN')}"}
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def call_model(prompt):
|
10 |
payload = {"inputs": prompt}
|
11 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
12 |
-
|
13 |
try:
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
# Emotion
|
19 |
def emotion_annotator(text):
|
20 |
-
# Step 1: List candidate emotions
|
21 |
prompt1 = f"""You are an expert in human emotions. Based on the sentence below, list all possible emotions the person might be feeling.
|
22 |
|
23 |
Sentence: "{text}"
|
24 |
Emotions:"""
|
25 |
candidates = call_model(prompt1)
|
26 |
|
27 |
-
|
28 |
-
prompt2 = f"""You are an emotion disambiguation expert. Given the sentence "{text}" and the following candidate emotions: {candidates}, pick the most likely one and explain why.
|
29 |
|
30 |
Format:
|
31 |
Most likely emotion: <emotion>
|
32 |
-
Reason: <why
|
33 |
final = call_model(prompt2)
|
34 |
|
35 |
return candidates.strip(), final.strip()
|
36 |
|
37 |
# Gradio UI
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
gr.Textbox(label="
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
demo.launch()
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
|
5 |
+
# Use Mistral via Hugging Face Inference API (requires token)
|
6 |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1"
|
7 |
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_TOKEN')}"}
|
8 |
|
9 |
+
# Example prompts
|
10 |
+
preset_prompts = [
|
11 |
+
"I finally got the promotion, but I feel guilty because my best friend got laid off.",
|
12 |
+
"Moving to a new city is exciting, but leaving my family breaks my heart.",
|
13 |
+
"I passed the test, but my friend failed — and I don’t know how to feel.",
|
14 |
+
"They applauded me on stage, but all I could think about was how lonely I felt.",
|
15 |
+
"I’m happy for her, but I wish I had that too.",
|
16 |
+
]
|
17 |
+
|
18 |
+
# Call Mistral Inference API with error handling
|
19 |
def call_model(prompt):
|
20 |
payload = {"inputs": prompt}
|
|
|
|
|
21 |
try:
|
22 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=45)
|
23 |
+
|
24 |
+
print("Status:", response.status_code)
|
25 |
+
print("Raw:", response.text)
|
26 |
+
|
27 |
+
if response.status_code != 200:
|
28 |
+
return f"⚠️ Error: API returned {response.status_code} - model may be loading or access may be restricted."
|
29 |
+
|
30 |
+
data = response.json()
|
31 |
+
if isinstance(data, list) and "generated_text" in data[0]:
|
32 |
+
return data[0]["generated_text"]
|
33 |
+
elif isinstance(data, dict) and "error" in data:
|
34 |
+
return f"⚠️ API Error: {data['error']}"
|
35 |
+
else:
|
36 |
+
return "⚠️ Unexpected response format from model."
|
37 |
+
|
38 |
+
except Exception as e:
|
39 |
+
return f"⚠️ Request failed: {str(e)}"
|
40 |
|
41 |
+
# Emotion analysis logic
|
42 |
def emotion_annotator(text):
|
|
|
43 |
prompt1 = f"""You are an expert in human emotions. Based on the sentence below, list all possible emotions the person might be feeling.
|
44 |
|
45 |
Sentence: "{text}"
|
46 |
Emotions:"""
|
47 |
candidates = call_model(prompt1)
|
48 |
|
49 |
+
prompt2 = f"""Given the sentence "{text}" and the following candidate emotions: {candidates}, pick the most likely emotion and explain why.
|
|
|
50 |
|
51 |
Format:
|
52 |
Most likely emotion: <emotion>
|
53 |
+
Reason: <why>"""
|
54 |
final = call_model(prompt2)
|
55 |
|
56 |
return candidates.strip(), final.strip()
|
57 |
|
58 |
# Gradio UI
|
59 |
+
with gr.Blocks() as demo:
|
60 |
+
gr.Markdown("## 🧠 Emotion Annotator AI (Mistral 7B via Hugging Face API)")
|
61 |
+
gr.Markdown("Disambiguates mixed emotions using `mistralai/Mistral-7B-Instruct-v0.1`. Token required.")
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
text_input = gr.Textbox(label="Enter your sentence", placeholder="e.g., I’m proud but I feel like I let them down.", lines=2)
|
65 |
+
dropdown = gr.Dropdown(preset_prompts, label="💬 Choose an example")
|
66 |
+
|
67 |
+
run_button = gr.Button("Submit")
|
68 |
+
|
69 |
+
with gr.Row():
|
70 |
+
candidate_output = gr.Textbox(label="🧠 Candidate Emotions")
|
71 |
+
final_output = gr.Textbox(label="🎯 Most Likely Emotion + Explanation")
|
72 |
+
|
73 |
+
# Auto-fill from dropdown
|
74 |
+
dropdown.change(fn=lambda x: x, inputs=dropdown, outputs=text_input)
|
75 |
+
run_button.click(fn=emotion_annotator, inputs=text_input, outputs=[candidate_output, final_output])
|
76 |
|
77 |
demo.launch()
|