Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -70,8 +70,12 @@ wav2vec2_model.eval()
|
|
| 70 |
|
| 71 |
label_map = {0: "Neutral", 1: "Happy", 2: "Angry", 3: "Sad", 4: "Surprise"}
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
def create_prompt_from_label(label):
|
| 77 |
return f"""
|
|
@@ -84,10 +88,11 @@ Ensure your tone is friendly and supportive, and make the recommendations short,
|
|
| 84 |
You can add some lovely emoji to let it become warm.
|
| 85 |
"""
|
| 86 |
|
|
|
|
| 87 |
def get_recommendations(label):
|
| 88 |
prompt = create_prompt_from_label(label)
|
| 89 |
try:
|
| 90 |
-
response =
|
| 91 |
model="gpt-4",
|
| 92 |
messages=[
|
| 93 |
{"role": "system", "content": "You are a helpful assistant that provides entertainment recommendations."},
|
|
@@ -96,7 +101,7 @@ def get_recommendations(label):
|
|
| 96 |
max_tokens=500,
|
| 97 |
temperature=0.7
|
| 98 |
)
|
| 99 |
-
return response
|
| 100 |
except Exception as e:
|
| 101 |
return f"❌ GPT Error: {str(e)}"
|
| 102 |
|
|
@@ -119,7 +124,7 @@ def process_audio_and_recommend(file_path):
|
|
| 119 |
pred_idx = torch.argmax(outputs, dim=1).item()
|
| 120 |
emotion = label_map[pred_idx]
|
| 121 |
recommendations = get_recommendations(emotion)
|
| 122 |
-
return f"
|
| 123 |
|
| 124 |
|
| 125 |
# ----------------- Gradio UI ---------------------
|
|
@@ -134,16 +139,15 @@ def process_audio_and_recommend(file_path):
|
|
| 134 |
# interface.launch()
|
| 135 |
|
| 136 |
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
| 137 |
-
gr.Markdown("##
|
| 138 |
-
gr.Markdown("
|
| 139 |
|
| 140 |
with gr.Row():
|
|
|
|
|
|
|
| 141 |
with gr.Column():
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
with gr.Column():
|
| 145 |
-
output_text_1 = gr.Text(label="🧠 检测情绪")
|
| 146 |
-
output_text_2 = gr.Text(label="💬 GPT 回复")
|
| 147 |
|
| 148 |
submit_btn.click(fn=process_audio_and_recommend, inputs=audio_input, outputs=[output_text_1, output_text_2])
|
| 149 |
|
|
|
|
| 70 |
|
| 71 |
label_map = {0: "Neutral", 1: "Happy", 2: "Angry", 3: "Sad", 4: "Surprise"}
|
| 72 |
|
| 73 |
+
from openai import OpenAI
|
| 74 |
+
import os
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY", "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"))
|
| 78 |
+
|
| 79 |
|
| 80 |
def create_prompt_from_label(label):
|
| 81 |
return f"""
|
|
|
|
| 88 |
You can add some lovely emoji to let it become warm.
|
| 89 |
"""
|
| 90 |
|
| 91 |
+
|
| 92 |
def get_recommendations(label):
|
| 93 |
prompt = create_prompt_from_label(label)
|
| 94 |
try:
|
| 95 |
+
response = client.chat.completions.create(
|
| 96 |
model="gpt-4",
|
| 97 |
messages=[
|
| 98 |
{"role": "system", "content": "You are a helpful assistant that provides entertainment recommendations."},
|
|
|
|
| 101 |
max_tokens=500,
|
| 102 |
temperature=0.7
|
| 103 |
)
|
| 104 |
+
return response.choices[0].message.content.strip()
|
| 105 |
except Exception as e:
|
| 106 |
return f"❌ GPT Error: {str(e)}"
|
| 107 |
|
|
|
|
| 124 |
pred_idx = torch.argmax(outputs, dim=1).item()
|
| 125 |
emotion = label_map[pred_idx]
|
| 126 |
recommendations = get_recommendations(emotion)
|
| 127 |
+
return f"{emotion}", recommendations
|
| 128 |
|
| 129 |
|
| 130 |
# ----------------- Gradio UI ---------------------
|
|
|
|
| 139 |
# interface.launch()
|
| 140 |
|
| 141 |
with gr.Blocks(theme=gr.themes.Soft()) as interface:
|
| 142 |
+
gr.Markdown("## Your Personal Emotion Assistant 😇")
|
| 143 |
+
gr.Markdown("Whisper me your thoughts, and I’ll wrap them in a better mood 🧸 ")
|
| 144 |
|
| 145 |
with gr.Row():
|
| 146 |
+
audio_input = gr.Audio(label="🎙️ Audio Input", type="filepath", format="wav")
|
| 147 |
+
submit_btn = gr.Button("Submit")
|
| 148 |
with gr.Column():
|
| 149 |
+
output_text_1 = gr.Text(label="🪄 I feel that you are...:")
|
| 150 |
+
output_text_2 = gr.Text(label="💬 Entertainment You May Like: ")
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
submit_btn.click(fn=process_audio_and_recommend, inputs=audio_input, outputs=[output_text_1, output_text_2])
|
| 153 |
|