Vasu456 commited on
Commit
98c175e
·
verified ·
1 Parent(s): 930f089

Delete mood.py

Browse files
Files changed (1) hide show
  1. mood.py +0 -172
mood.py DELETED
@@ -1,172 +0,0 @@
1
- import os
2
- import cv2
3
- import numpy as np
4
- import streamlit as st
5
- from dotenv import load_dotenv
6
- import tensorflow as tf
7
- import google.generativeai as gen_ai
8
-
9
- # Load environment variables
10
- load_dotenv()
11
-
12
- # Configure Streamlit page settings
13
- st.set_page_config(
14
- page_title="Emotion-aware Chatbot",
15
- page_icon=":brain:", # Favicon emoji
16
- layout="centered", # Page layout option
17
- )
18
-
19
- GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
20
-
21
- # Set up Google Gemini-Pro AI model
22
- gen_ai.configure(api_key=GOOGLE_API_KEY)
23
- model = gen_ai.GenerativeModel('gemini-pro')
24
-
25
- # Load Emotion Detection Model
26
- def load_emotion_model(model_path):
27
- return tf.keras.models.load_model(model_path)
28
-
29
- # Preprocess Frame for Emotion Detection
30
- def preprocess_frame(frame, image_size):
31
- img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
32
- img = cv2.resize(img, image_size)
33
- img = img.astype('float32') / 255.0
34
- return np.expand_dims(img, axis=0)
35
-
36
- # Predict Emotion from Webcam Frame
37
- def predict_emotion(model, frame, image_size):
38
- preprocessed_frame = preprocess_frame(frame, image_size)
39
- predictions = model.predict(preprocessed_frame)
40
- emotion = np.argmax(predictions) # Assuming your model returns a probability distribution
41
- return emotion
42
-
43
- # Initialize chat session in Streamlit if not already present
44
- if "chat_session" not in st.session_state:
45
- st.session_state.chat_session = model.start_chat(history=[])
46
-
47
- # Define CSS styles for chat messages
48
- st.markdown("""
49
- <style>
50
- .message-container {
51
- max-height: 400px;
52
- overflow-y: auto;
53
- display: flex;
54
- flex-direction: column;
55
- margin-bottom: 10px;
56
- }
57
- .user-message {
58
- align-self: flex-end;
59
- background-color: #DCF8C6;
60
- border-radius: 10px;
61
- padding: 8px 12px;
62
- margin-bottom: 5px;
63
- max-width: 70%;
64
- text-align: left;
65
- }
66
- .ai-message {
67
- align-self: flex-start;
68
- background-color: #E0E0E0;
69
- border-radius: 10px;
70
- padding: 8px 12px;
71
- margin-bottom: 5px;
72
- max-width: 70%;
73
- text-align: left;
74
- }
75
- .system-message {
76
- align-self: center;
77
- background-color: #F5F5F5;
78
- border-radius: 10px;
79
- padding: 8px 12px;
80
- margin-bottom: 5px;
81
- max-width: 70%;
82
- text-align: center;
83
- }
84
- </style>
85
- """, unsafe_allow_html=True)
86
-
87
- # Initialize conversation history
88
- if 'flowmessages' not in st.session_state:
89
- st.session_state['flowmessages'] = [
90
- {"role": "system", "content": "Welcome! I'm here to chat with you."}
91
- ]
92
-
93
- # Function to add messages to chat history
94
- def add_message(role, content):
95
- st.session_state['flowmessages'].append({"role": role, "content": content})
96
-
97
- # Display messages
98
- def display_messages():
99
- st.markdown('<div class="message-container">', unsafe_allow_html=True)
100
- for message in st.session_state['flowmessages']:
101
- if message['role'] == 'system':
102
- st.markdown(f'<div class="system-message">{message["content"]}</div>', unsafe_allow_html=True)
103
- elif message['role'] == 'user':
104
- st.markdown(f'<div class="user-message">{message["content"]}</div>', unsafe_allow_html=True)
105
- elif message['role'] == 'bot':
106
- st.markdown(f'<div class="ai-message">{message["content"]}</div>', unsafe_allow_html=True)
107
- st.markdown('</div>', unsafe_allow_html=True)
108
-
109
- # Display previous chat history
110
- display_messages()
111
-
112
- # Capture Frame from Webcam
113
- def capture_frame():
114
- cap = cv2.VideoCapture(0)
115
- ret, frame = cap.read()
116
- cap.release()
117
- return frame
118
-
119
- # Load emotion detection model
120
- emotion_model_path = "model_f.h5"
121
- emotion_model = load_emotion_model(emotion_model_path)
122
-
123
- # Emotion dictionary
124
- emotion_dict = {0: 'happiness', 1: 'sadness', 2: 'anger', 3: 'fear'}
125
-
126
- # Get Gemini response based on prompt
127
- def get_gemini_response(question, prompt):
128
- response = model.generate_content([prompt, question])
129
- return response.text
130
-
131
- # Define the base prompt for Gemini-Pro
132
- base_prompt = """
133
- You are an empathetic assistant dedicated to improving the user's mood based on their given detected emotion: happy, sad, fear, or anger
134
- and input text. Your responses should be friendly and mood-appropriate:
135
- Happy: Celebrate their joy and ask about the reason for their happiness. Provide enthusiastic reinforcement.
136
- Sad: Offer comfort and empathy. Ask if they want to share what's making them sad and offer help.
137
- Fear: Reassure them and acknowledge their fear. Offer practical advice to alleviate their fear.
138
- Anger: Stay calm and listen. Validate their feelings and suggest ways to manage anger.
139
- Your goal is to create a positive and supportive environment, helping users feel understood and valued.
140
- """
141
-
142
- # Get user input
143
- with st.form(key="user_input_form", clear_on_submit=True):
144
- user_input = st.text_input("You: ", key="input")
145
- submit_button = st.form_submit_button(label="Submit")
146
-
147
- if submit_button:
148
- if user_input.strip() == "":
149
- st.warning("Please enter a message.")
150
- elif user_input.lower().strip() == "quit":
151
- st.stop()
152
- else:
153
- # Add user message to chat history
154
- add_message('user', user_input)
155
-
156
- # Capture frame from webcam
157
- frame = capture_frame()
158
-
159
- # Predict emotion
160
- emotion = predict_emotion(emotion_model, frame, image_size=(128, 128))
161
- detected_emotion = emotion_dict.get(emotion, 'neutral')
162
-
163
- # Prepare the prompt
164
- prompt = f"{base_prompt} The user is currently feeling {detected_emotion} and his input message is {user_input}."
165
- # Send user's message to Gemini-Pro and get the response
166
- gemini_response = get_gemini_response(user_input, prompt)
167
-
168
- # Add bot response to chat history
169
- add_message('bot', gemini_response)
170
-
171
- # Display updated chat history
172
- display_messages()