AnasAlokla commited on
Commit
c6f7445
·
verified ·
1 Parent(s): 960da2c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ import streamlit as st
4
+ from transformers import pipeline
5
+
6
+ # 1. Emotion Detection Model (Using Hugging Face's transformer)
7
+ # Choose a suitable model - 'emotion-classification' is the task, you can specify a model from Hugging Face Model Hub.
8
+ emotion_classifier = pipeline("text-classification", model="SamLowe/roberta-base-go_emotions") # Or choose another model
9
+
10
+ # 2. Conversational Agent Logic
11
+ def get_ai_response(user_input, emotion_predictions):
12
+ """Generates AI response based on user input and detected emotions."""
13
+
14
+ # Basic response generation based on detected emotions
15
+ responses = {
16
+ "anger": "I understand you're feeling angry. Let's take a deep breath and try to resolve this.",
17
+ "sadness": "I'm sorry to hear you're feeling sad. Is there anything I can do to help?",
18
+ "joy": "That's wonderful! I'm so happy for you!",
19
+ "surprise": "Wow, that's surprising! Tell me more.",
20
+ "fear": "I understand you're afraid. How can I help?",
21
+ "neutral": "Understood.", # or a more neutral response
22
+ "default": "I am not able to understand the emotion, please try again"
23
+ }
24
+
25
+
26
+ dominant_emotion = None
27
+ max_score = 0
28
+
29
+ for prediction in emotion_predictions:
30
+ if prediction['score'] > max_score:
31
+ max_score = prediction['score']
32
+ dominant_emotion = prediction['label']
33
+
34
+
35
+ # Handle cases where no specific emotion is clear
36
+ if dominant_emotion is None:
37
+ return responses["default"] # or use default message if no emotion is detected.
38
+ elif dominant_emotion in responses:
39
+ return responses[dominant_emotion]
40
+ else:
41
+ return "I'm detecting some emotion, but I'm not sure how to respond." #Handle unexpected emotion labels.
42
+
43
+ # 3. Streamlit Frontend
44
+ st.title("Emotionally Aware Chatbot")
45
+
46
+ # Input Text Box
47
+ user_input = st.text_input("Enter your message:", "")
48
+
49
+ if user_input:
50
+ # Emotion Detection
51
+ emotion_predictions = emotion_classifier(user_input)
52
+
53
+ # Display Emotions
54
+ st.subheader("Detected Emotions:")
55
+ for prediction in emotion_predictions:
56
+ st.write(f"- {prediction['label']}: {prediction['score']:.2f}") # Show emotion score.
57
+
58
+ # Get AI Response
59
+ ai_response = get_ai_response(user_input, emotion_predictions)
60
+
61
+ # Display AI Response
62
+ st.subheader("AI Response:")
63
+ st.write(ai_response)