Spaces:
Running
Running
import os | |
import requests | |
import streamlit as st | |
from transformers import pipeline | |
from PIL import Image | |
from dotenv import load_dotenv | |
import time | |
# Load environment variables from .env file | |
load_dotenv() | |
# Set up the Hugging Face API URLs and your API key | |
emotion_model_url = "https://api-inference.huggingface.co/models/trpakov/vit-face-expression" | |
text_model_url = "https://api-inference.huggingface.co/models/mrm8488/t5-base-finetuned-emotion" | |
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"} | |
# Function to query the facial expression recognition model | |
def query_emotion(filename): | |
with open(filename, "rb") as f: | |
data = f.read() | |
response = requests.post(emotion_model_url, headers=headers, data=data) | |
if response.status_code == 200: | |
return response.json() | |
else: | |
st.error("Error detecting facial expression: " + response.text) | |
return None | |
# Function to generate a joke or uplifting text based on the mood | |
def generate_text_based_on_mood(emotion): | |
try: | |
prompt = f"Generate a light-hearted joke or uplifting message for someone who is feeling {emotion}." | |
# Retry mechanism for text generation | |
for attempt in range(5): # Retry up to 5 times | |
response = requests.post(text_model_url, headers=headers, json={"inputs": prompt}) | |
if response.status_code == 200: | |
generated_text = response.json()[0]['generated_text'] | |
if generated_text.strip(): # Ensure the response is not empty | |
return generated_text | |
else: | |
st.warning("Received an empty joke, retrying...") | |
elif response.status_code == 503: # Service Unavailable | |
st.warning("Model is loading, retrying...") | |
time.sleep(5) # Wait before retrying | |
else: | |
st.error("Error generating text: " + response.text) | |
return "Sorry, I couldn't come up with a joke at this moment." | |
st.error("Failed to generate text after multiple attempts.") | |
return "Sorry, I couldn't come up with a joke at this moment." | |
except Exception as e: | |
st.error(f"Error generating text: {e}") | |
return "Sorry, I couldn't come up with a joke at this moment." | |
# Function to convert text to speech using gTTS | |
def text_to_speech(text): | |
from gtts import gTTS | |
try: | |
tts = gTTS(text, lang='en') | |
audio_file = "output.mp3" | |
tts.save(audio_file) # Save the audio file | |
return audio_file | |
except Exception as e: | |
st.error(f"Error with TTS: {e}") | |
return None | |
# Streamlit UI | |
st.title("Facial Expression Mood Detector") | |
st.write("Upload an image of a face to detect mood and receive uplifting messages or jokes.") | |
# Upload image | |
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) | |
if uploaded_file is not None: | |
# Load and display the image | |
image = Image.open(uploaded_file) | |
st.image(image, caption='Uploaded Image', use_column_width=True) | |
# Save the uploaded file temporarily | |
with open("uploaded_image.jpg", "wb") as f: | |
f.write(uploaded_file.getbuffer()) | |
# Detect facial expression | |
expression_output = query_emotion("uploaded_image.jpg") | |
if expression_output: | |
# Assuming the response has a 'label' field with the detected emotion | |
emotion = expression_output[0]['label'] # Adjust based on response structure | |
st.write(f"Detected emotion: {emotion}") | |
# Generate text based on detected emotion | |
joke = generate_text_based_on_mood(emotion) | |
st.write("Here's something to cheer you up:") | |
st.write(joke) | |
# Convert the generated joke to audio | |
audio_file = text_to_speech(joke) | |
# Provide an audio player in the Streamlit app if audio file exists | |
if audio_file: | |
st.audio(audio_file) # Streamlit will handle playback | |