Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
# Hugging Face
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
preset_prompts = [
|
| 12 |
"I finally got the promotion, but I feel guilty because my best friend got laid off.",
|
| 13 |
"Moving to a new city is exciting, but leaving my family breaks my heart.",
|
|
@@ -16,43 +17,46 @@ preset_prompts = [
|
|
| 16 |
"I’m happy for her, but I wish I had that too.",
|
| 17 |
]
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
def
|
| 21 |
-
payload = {
|
| 22 |
-
"model": MODEL,
|
| 23 |
-
"messages": messages
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
try:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
return
|
| 32 |
except Exception as e:
|
| 33 |
return f"⚠️ Error: {str(e)}"
|
| 34 |
|
| 35 |
-
# Emotion
|
| 36 |
-
def emotion_annotator(
|
| 37 |
-
# Step 1:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
# Step 2:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
return
|
| 48 |
|
| 49 |
# Gradio UI
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
-
gr.Markdown("## 🧠 Emotion Annotator
|
| 52 |
-
gr.Markdown("
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
-
text_input = gr.Textbox(label="
|
| 56 |
dropdown = gr.Dropdown(preset_prompts, label="💬 Choose an example")
|
| 57 |
run_button = gr.Button("Submit")
|
| 58 |
|
|
@@ -60,7 +64,7 @@ with gr.Blocks() as demo:
|
|
| 60 |
candidate_output = gr.Textbox(label="🧠 Candidate Emotions")
|
| 61 |
final_output = gr.Textbox(label="🎯 Most Likely Emotion + Explanation")
|
| 62 |
|
| 63 |
-
#
|
| 64 |
dropdown.change(fn=lambda x: x, inputs=dropdown, outputs=text_input)
|
| 65 |
run_button.click(fn=emotion_annotator, inputs=text_input, outputs=[candidate_output, final_output])
|
| 66 |
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# Load Hugging Face token from secret
|
| 6 |
+
client = InferenceClient(
|
| 7 |
+
provider="nscale", # You can change to 'openrouter' or 'novita' if needed
|
| 8 |
+
api_key=os.environ["HF_TOKEN"],
|
| 9 |
+
)
|
| 10 |
|
| 11 |
+
# Test prompt list
|
| 12 |
preset_prompts = [
|
| 13 |
"I finally got the promotion, but I feel guilty because my best friend got laid off.",
|
| 14 |
"Moving to a new city is exciting, but leaving my family breaks my heart.",
|
|
|
|
| 17 |
"I’m happy for her, but I wish I had that too.",
|
| 18 |
]
|
| 19 |
|
| 20 |
+
# Core generation logic using chat completion
|
| 21 |
+
def call_llama(messages):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
+
completion = client.chat.completions.create(
|
| 24 |
+
model="meta-llama/Llama-3.1-8B-Instruct",
|
| 25 |
+
messages=messages,
|
| 26 |
+
)
|
| 27 |
+
return completion.choices[0].message.content.strip()
|
| 28 |
except Exception as e:
|
| 29 |
return f"⚠️ Error: {str(e)}"
|
| 30 |
|
| 31 |
+
# Emotion pipeline
|
| 32 |
+
def emotion_annotator(text):
|
| 33 |
+
# Step 1: List candidate emotions
|
| 34 |
+
msg1 = [
|
| 35 |
+
{
|
| 36 |
+
"role": "user",
|
| 37 |
+
"content": f'List all possible emotions the person might be feeling in this sentence:\n"{text}"\nJust give comma-separated emotion names.'
|
| 38 |
+
}
|
| 39 |
+
]
|
| 40 |
+
candidates = call_llama(msg1)
|
| 41 |
|
| 42 |
+
# Step 2: Choose most likely emotion with reason
|
| 43 |
+
msg2 = [
|
| 44 |
+
{
|
| 45 |
+
"role": "user",
|
| 46 |
+
"content": f'From these emotions: {candidates}, which is most likely the dominant one in the sentence "{text}"? Explain why briefly.\nFormat:\nMost likely emotion: <emotion>\nReason: <why>'
|
| 47 |
+
}
|
| 48 |
+
]
|
| 49 |
+
final = call_llama(msg2)
|
| 50 |
|
| 51 |
+
return candidates, final
|
| 52 |
|
| 53 |
# Gradio UI
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("## 🧠 Emotion Annotator (LLaMA 3.1 via Hugging Face Chat API)")
|
| 56 |
+
gr.Markdown("Powered by `meta-llama/Llama-3.1-8B-Instruct`, served using the InferenceClient chat interface.")
|
| 57 |
|
| 58 |
with gr.Row():
|
| 59 |
+
text_input = gr.Textbox(label="✏️ Input Sentence", placeholder="e.g., I’m proud but I feel like I let them down.")
|
| 60 |
dropdown = gr.Dropdown(preset_prompts, label="💬 Choose an example")
|
| 61 |
run_button = gr.Button("Submit")
|
| 62 |
|
|
|
|
| 64 |
candidate_output = gr.Textbox(label="🧠 Candidate Emotions")
|
| 65 |
final_output = gr.Textbox(label="🎯 Most Likely Emotion + Explanation")
|
| 66 |
|
| 67 |
+
# Dropdown autofill
|
| 68 |
dropdown.change(fn=lambda x: x, inputs=dropdown, outputs=text_input)
|
| 69 |
run_button.click(fn=emotion_annotator, inputs=text_input, outputs=[candidate_output, final_output])
|
| 70 |
|