Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,49 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
)
|
| 20 |
-
|
| 21 |
-
# Create a pipeline
|
| 22 |
-
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 23 |
-
|
| 24 |
-
# Define emotion annotation pipeline
|
| 25 |
def emotion_annotator(text):
|
| 26 |
-
# Step 1:
|
| 27 |
-
prompt1 = f"""
|
| 28 |
-
|
|
|
|
| 29 |
Emotions:"""
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
return
|
| 38 |
|
| 39 |
-
# Gradio
|
| 40 |
demo = gr.Interface(
|
| 41 |
fn=emotion_annotator,
|
| 42 |
-
inputs=gr.Textbox(lines=2, placeholder="
|
| 43 |
outputs=[
|
| 44 |
gr.Textbox(label="Candidate Emotions"),
|
| 45 |
gr.Textbox(label="Most Likely Emotion + Explanation")
|
| 46 |
],
|
| 47 |
-
title="🧠 Emotion Annotator AI",
|
| 48 |
-
description="
|
| 49 |
)
|
| 50 |
|
| 51 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
# Set up Hugging Face Inference API
|
| 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 |
+
return response.json()[0]["generated_text"]
|
| 15 |
+
except (KeyError, IndexError, TypeError):
|
| 16 |
+
return "⚠️ Error: Could not generate a valid response. Check API status or token."
|
| 17 |
+
|
| 18 |
+
# Emotion disambiguation pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Step 2: Choose most likely one
|
| 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 you chose it>"""
|
| 33 |
+
final = call_model(prompt2)
|
| 34 |
|
| 35 |
+
return candidates.strip(), final.strip()
|
| 36 |
|
| 37 |
+
# Gradio UI
|
| 38 |
demo = gr.Interface(
|
| 39 |
fn=emotion_annotator,
|
| 40 |
+
inputs=gr.Textbox(lines=2, placeholder="e.g., I’m proud but I feel like I let them down."),
|
| 41 |
outputs=[
|
| 42 |
gr.Textbox(label="Candidate Emotions"),
|
| 43 |
gr.Textbox(label="Most Likely Emotion + Explanation")
|
| 44 |
],
|
| 45 |
+
title="🧠 Emotion Annotator AI (via Hugging Face Inference API)",
|
| 46 |
+
description="Uses Mistral 7B Instruct on Hugging Face to analyze and disambiguate mixed emotions from text input. No model download needed!"
|
| 47 |
)
|
| 48 |
|
| 49 |
demo.launch()
|