Lucasstranger1 commited on
Commit
2bbfd62
·
verified ·
1 Parent(s): c2edaba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -18
app.py CHANGED
@@ -1,9 +1,9 @@
1
  #############################################################################################################################
2
  # Filename : app.py
3
  # Description: A Streamlit application to detect facial expressions from images and provide responses.
4
- # Author : Lucas Yao
5
  #
6
- # Copyright © 2024 by Lucas Yao
7
  #############################################################################################################################
8
 
9
  # Import libraries.
@@ -50,15 +50,17 @@ def query_emotion(image):
50
  return predicted_label
51
 
52
  #############################################################################################################################
53
- # Function to generate a response using OpenAI based on detected emotion.
54
- def generate_text_based_on_mood(emotion):
55
  try:
56
- # Create a dynamic prompt based on the detected emotion.
57
- prompt = f"Generate a light-hearted joke or motivational message for someone who is feeling {emotion}."
58
-
 
 
59
  # Call OpenAI's API using GPT-4.
60
  response = openai.ChatCompletion.create(
61
- model="gpt-4", # Specify the GPT-4 model.
62
  messages=[
63
  {"role": "user", "content": prompt}
64
  ]
@@ -89,7 +91,7 @@ def text_to_speech(text):
89
  # Main function to create the Streamlit web application.
90
  def main():
91
  st.title("Facial Expression Mood Detector")
92
- st.write("Upload an image of a face to detect mood and receive uplifting messages or jokes.")
93
 
94
  # Upload image.
95
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
@@ -103,17 +105,21 @@ def main():
103
  emotion = query_emotion(image)
104
  st.write(f"Detected emotion: {emotion}")
105
 
106
- # Generate text based on detected emotion.
107
- message = generate_text_based_on_mood(emotion)
108
- st.write("Here's something to remind you:")
109
- st.write(message)
 
 
 
 
110
 
111
- # Convert the generated message to audio.
112
- audio_file = text_to_speech(message)
113
 
114
- # Provide an audio player in the Streamlit app if audio file exists.
115
- if audio_file:
116
- st.audio(audio_file) # Streamlit will handle playback.
117
 
118
  #############################################################################################################################
119
  # Run the application.
 
1
  #############################################################################################################################
2
  # Filename : app.py
3
  # Description: A Streamlit application to detect facial expressions from images and provide responses.
4
+ # Author : [Your Name]
5
  #
6
+ # Copyright © 2024 by [Your Name]
7
  #############################################################################################################################
8
 
9
  # Import libraries.
 
50
  return predicted_label
51
 
52
  #############################################################################################################################
53
+ # Function to generate a response using OpenAI based on detected emotion and user preference.
54
+ def generate_text_based_on_mood(emotion, response_type):
55
  try:
56
+ if response_type == "Joke":
57
+ prompt = f"Generate a light-hearted joke for someone who is feeling {emotion}."
58
+ else: # Motivational Message
59
+ prompt = f"Generate a motivational message for someone who is feeling {emotion}."
60
+
61
  # Call OpenAI's API using GPT-4.
62
  response = openai.ChatCompletion.create(
63
+ model="gpt-4", # Specify the GPT-4 model
64
  messages=[
65
  {"role": "user", "content": prompt}
66
  ]
 
91
  # Main function to create the Streamlit web application.
92
  def main():
93
  st.title("Facial Expression Mood Detector")
94
+ st.write("Upload an image of a face to detect mood and receive a response.")
95
 
96
  # Upload image.
97
  uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
 
105
  emotion = query_emotion(image)
106
  st.write(f"Detected emotion: {emotion}")
107
 
108
+ # Dropdown for selecting response type.
109
+ response_type = st.selectbox("Select the type of response:", ["Joke", "Motivational Message"])
110
+
111
+ # Generate text based on detected emotion and user preference.
112
+ if st.button("Get Response"):
113
+ message = generate_text_based_on_mood(emotion, response_type)
114
+ st.write("Here's your response:")
115
+ st.write(message)
116
 
117
+ # Convert the generated message to audio.
118
+ audio_file = text_to_speech(message)
119
 
120
+ # Provide an audio player in the Streamlit app if audio file exists.
121
+ if audio_file:
122
+ st.audio(audio_file) # Streamlit will handle playback.
123
 
124
  #############################################################################################################################
125
  # Run the application.