Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,51 @@
|
|
1 |
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# 1. Emotion Detection Model (Using Hugging Face's transformer)
|
6 |
# Choose a suitable model - 'emotion-classification' is the task, you can specify a model from Hugging Face Model Hub.
|
@@ -10,34 +55,25 @@ emotion_classifier = pipeline("text-classification", model="AnasAlokla/multiling
|
|
10 |
def get_ai_response(user_input, emotion_predictions):
|
11 |
"""Generates AI response based on user input and detected emotions."""
|
12 |
|
13 |
-
# Basic response generation based on detected emotions
|
14 |
-
responses = {
|
15 |
-
"anger": "I understand you're feeling angry. Let's take a deep breath and try to resolve this.",
|
16 |
-
"sadness": "I'm sorry to hear you're feeling sad. Is there anything I can do to help?",
|
17 |
-
"joy": "That's wonderful! I'm so happy for you!",
|
18 |
-
"surprise": "Wow, that's surprising! Tell me more.",
|
19 |
-
"fear": "I understand you're afraid. How can I help?",
|
20 |
-
"neutral": "Understood.", # or a more neutral response
|
21 |
-
"default": "I am not able to understand the emotion, please try again"
|
22 |
-
}
|
23 |
-
|
24 |
|
25 |
dominant_emotion = None
|
26 |
max_score = 0
|
27 |
-
|
28 |
for prediction in emotion_predictions:
|
29 |
if prediction['score'] > max_score:
|
30 |
max_score = prediction['score']
|
31 |
dominant_emotion = prediction['label']
|
32 |
|
33 |
|
|
|
|
|
|
|
|
|
34 |
# Handle cases where no specific emotion is clear
|
35 |
if dominant_emotion is None:
|
36 |
-
return
|
37 |
-
elif dominant_emotion in responses:
|
38 |
-
return responses[dominant_emotion]
|
39 |
else:
|
40 |
-
return
|
41 |
|
42 |
# 3. Streamlit Frontend
|
43 |
st.title("Emotionally Aware Chatbot")
|
|
|
1 |
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
4 |
+
import google.generativeai as genai
|
5 |
+
import json
|
6 |
+
import random
|
7 |
+
|
8 |
+
# Load the JSON data
|
9 |
+
with open('emotion_templates.json', 'r') as f:
|
10 |
+
data = json.load(f)
|
11 |
+
|
12 |
+
# Configure Gemini (replace with your API key)
|
13 |
+
genai.configure(api_key="AIzaSyCYRYNwCU1f9cgJYn8pd86Xcf6hiSMwJr0")
|
14 |
+
|
15 |
+
model = genai.GenerativeModel('gemini-2.0-flash')
|
16 |
+
|
17 |
+
|
18 |
+
def generate_text(prompt, context=""):
|
19 |
+
"""
|
20 |
+
Generates text using the Gemini model.
|
21 |
+
"""
|
22 |
+
try:
|
23 |
+
response = model.generate_content(prompt)
|
24 |
+
return response.text
|
25 |
+
except Exception as e:
|
26 |
+
print(f"Error generating text: {e}")
|
27 |
+
return "I am sorry, I encountered an error while generating the text."
|
28 |
+
|
29 |
+
def create_prompt(emotion, topic = None):
|
30 |
+
"""
|
31 |
+
Chooses a random prompt from the template list.
|
32 |
+
"""
|
33 |
+
|
34 |
+
templates = data["emotion_templates"][emotion]
|
35 |
+
prompt = random.choice(templates)
|
36 |
+
|
37 |
+
if topic:
|
38 |
+
prompt = prompt.replace("[topic/person]",topic)
|
39 |
+
prompt = prompt.replace("[topic]",topic)
|
40 |
+
prompt = prompt.replace("[person]",topic)
|
41 |
+
prompt = prompt.replace("[object]",topic) # added object replace
|
42 |
+
prompt = prompt.replace("[outcome]",topic) # added outcome replace
|
43 |
+
subfix_prompt = "Make the generated text in the same language as the topic.\n"
|
44 |
+
prefix_prompt = "## topic language and contant\n"+topic
|
45 |
+
prompt = prefix_prompt + prompt + subfix_prompt
|
46 |
+
return prompt
|
47 |
+
|
48 |
+
|
49 |
|
50 |
# 1. Emotion Detection Model (Using Hugging Face's transformer)
|
51 |
# Choose a suitable model - 'emotion-classification' is the task, you can specify a model from Hugging Face Model Hub.
|
|
|
55 |
def get_ai_response(user_input, emotion_predictions):
|
56 |
"""Generates AI response based on user input and detected emotions."""
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
dominant_emotion = None
|
60 |
max_score = 0
|
61 |
+
responses = None
|
62 |
for prediction in emotion_predictions:
|
63 |
if prediction['score'] > max_score:
|
64 |
max_score = prediction['score']
|
65 |
dominant_emotion = prediction['label']
|
66 |
|
67 |
|
68 |
+
prompt_text = create_prompt(dominant_emotion,user_input)
|
69 |
+
|
70 |
+
responses = generate_text(prompt_text)
|
71 |
+
|
72 |
# Handle cases where no specific emotion is clear
|
73 |
if dominant_emotion is None:
|
74 |
+
return "Error for response"
|
|
|
|
|
75 |
else:
|
76 |
+
return responses
|
77 |
|
78 |
# 3. Streamlit Frontend
|
79 |
st.title("Emotionally Aware Chatbot")
|