Spaces:
Running
Running
Lucasstranger1
commited on
Commit
•
b878745
1
Parent(s):
bddd4b6
update
Browse files
app.py
CHANGED
@@ -1,55 +1,70 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
# Load environment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
load_dotenv()
|
12 |
|
13 |
-
# Set up
|
|
|
|
|
14 |
openai.api_key = os.getenv('OPENAI_API_KEY')
|
15 |
|
16 |
-
# Load the processor and model for facial expression recognition
|
17 |
processor = AutoProcessor.from_pretrained("trpakov/vit-face-expression")
|
18 |
model = AutoModelForImageClassification.from_pretrained("trpakov/vit-face-expression")
|
19 |
|
20 |
-
|
|
|
21 |
def query_emotion(image):
|
22 |
-
# Preprocess the image
|
23 |
inputs = processor(images=image, return_tensors="pt")
|
24 |
|
25 |
-
# Perform inference
|
26 |
with torch.no_grad():
|
27 |
outputs = model(**inputs)
|
28 |
|
29 |
-
# Get predicted class index (the class with the highest logit)
|
30 |
logits = outputs.logits
|
31 |
predicted_class_idx = torch.argmax(logits, dim=-1).item()
|
32 |
|
33 |
-
# Retrieve the label names from the model
|
34 |
-
label_names = model.config.id2label # Mapping of indices to emotion labels
|
35 |
-
predicted_label = label_names[predicted_class_idx] # Get the predicted label
|
36 |
|
37 |
return predicted_label
|
38 |
|
39 |
-
|
|
|
40 |
def generate_text_based_on_mood(emotion):
|
41 |
try:
|
|
|
42 |
prompt = f"Generate a light-hearted joke or motivational message for someone who is feeling {emotion}."
|
43 |
-
|
44 |
-
# Call OpenAI's API using GPT-4
|
45 |
response = openai.ChatCompletion.create(
|
46 |
-
model="gpt-4", # Specify the GPT-4 model
|
47 |
messages=[
|
48 |
{"role": "user", "content": prompt}
|
49 |
]
|
50 |
)
|
51 |
|
52 |
-
# Extract the generated text
|
53 |
generated_text = response['choices'][0]['message']['content']
|
54 |
return generated_text.strip()
|
55 |
|
@@ -57,42 +72,50 @@ def generate_text_based_on_mood(emotion):
|
|
57 |
st.error(f"Error generating text: {e}")
|
58 |
return "Sorry, I couldn't come up with a message at this moment."
|
59 |
|
60 |
-
|
|
|
61 |
def text_to_speech(text):
|
62 |
from gtts import gTTS
|
63 |
try:
|
64 |
tts = gTTS(text, lang='en')
|
65 |
audio_file = "output.mp3"
|
66 |
-
tts.save(audio_file) # Save the audio file
|
67 |
return audio_file
|
68 |
except Exception as e:
|
69 |
st.error(f"Error with TTS: {e}")
|
70 |
return None
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
|
77 |
-
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
st.image(image, caption='Uploaded Image', use_column_width=True)
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
st.write("Here's something to cheer you up:")
|
91 |
-
st.write(message)
|
92 |
|
93 |
-
|
94 |
-
|
|
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
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.
|
10 |
+
import os # Load environment variable(s).
|
11 |
+
import requests # Send HTTP GET request to Hugging Face models for inference.
|
12 |
+
import streamlit as st # Build the GUI of the application.
|
13 |
+
from PIL import Image # Handle image operations.
|
14 |
+
from dotenv import load_dotenv # Load environment variables.
|
15 |
+
import torch # For tensor operations.
|
16 |
+
from transformers import AutoProcessor, AutoModelForImageClassification # Hugging Face models.
|
17 |
+
import openai # OpenAI API for generating text responses.
|
18 |
+
|
19 |
+
#############################################################################################################################
|
20 |
+
# Load environment variable(s).
|
21 |
load_dotenv()
|
22 |
|
23 |
+
# Set up the Hugging Face API for emotion detection.
|
24 |
+
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
25 |
+
# Set up OpenAI API key.
|
26 |
openai.api_key = os.getenv('OPENAI_API_KEY')
|
27 |
|
28 |
+
# Load the processor and model for facial expression recognition.
|
29 |
processor = AutoProcessor.from_pretrained("trpakov/vit-face-expression")
|
30 |
model = AutoModelForImageClassification.from_pretrained("trpakov/vit-face-expression")
|
31 |
|
32 |
+
#############################################################################################################################
|
33 |
+
# Function to query the facial expression recognition model.
|
34 |
def query_emotion(image):
|
35 |
+
# Preprocess the image.
|
36 |
inputs = processor(images=image, return_tensors="pt")
|
37 |
|
38 |
+
# Perform inference.
|
39 |
with torch.no_grad():
|
40 |
outputs = model(**inputs)
|
41 |
|
42 |
+
# Get predicted class index (the class with the highest logit).
|
43 |
logits = outputs.logits
|
44 |
predicted_class_idx = torch.argmax(logits, dim=-1).item()
|
45 |
|
46 |
+
# Retrieve the label names from the model.
|
47 |
+
label_names = model.config.id2label # Mapping of indices to emotion labels.
|
48 |
+
predicted_label = label_names[predicted_class_idx] # Get the predicted label.
|
49 |
|
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 |
]
|
65 |
)
|
66 |
|
67 |
+
# Extract the generated text.
|
68 |
generated_text = response['choices'][0]['message']['content']
|
69 |
return generated_text.strip()
|
70 |
|
|
|
72 |
st.error(f"Error generating text: {e}")
|
73 |
return "Sorry, I couldn't come up with a message at this moment."
|
74 |
|
75 |
+
#############################################################################################################################
|
76 |
+
# Function to convert text to speech using gTTS.
|
77 |
def text_to_speech(text):
|
78 |
from gtts import gTTS
|
79 |
try:
|
80 |
tts = gTTS(text, lang='en')
|
81 |
audio_file = "output.mp3"
|
82 |
+
tts.save(audio_file) # Save the audio file.
|
83 |
return audio_file
|
84 |
except Exception as e:
|
85 |
st.error(f"Error with TTS: {e}")
|
86 |
return None
|
87 |
|
88 |
+
#############################################################################################################################
|
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"])
|
96 |
|
97 |
+
if uploaded_file is not None:
|
98 |
+
# Load and display the image.
|
99 |
+
image = Image.open(uploaded_file)
|
100 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
101 |
|
102 |
+
# Detect facial expression.
|
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.
|
120 |
+
if __name__ == "__main__":
|
121 |
+
main()
|