Spaces:
Running
Running
import os | |
import streamlit as st | |
import requests | |
import pyttsx3 | |
from transformers import pipeline | |
from PIL import Image | |
from dotenv import load_dotenv | |
# Load environment variables from .env file | |
load_dotenv() | |
# Set up the Hugging Face API URL and your API key | |
API_URL = "https://api-inference.huggingface.co/models/trpakov/vit-face-expression" | |
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"} | |
# Function to query the Hugging Face model for facial expression | |
def query(filename): | |
with open(filename, "rb") as f: | |
data = f.read() | |
response = requests.post(API_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): | |
generator = pipeline('text-generation', model='gpt2') | |
prompt = f"Tell a joke that would cheer someone who is feeling {emotion}." | |
response = generator(prompt, max_length=50, num_return_sequences=1) | |
return response[0]['generated_text'] | |
# Function to convert text to speech | |
def text_to_speech(text): | |
engine = pyttsx3.init() | |
engine.say(text) | |
engine.runAndWait() | |
# 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("uploaded_image.jpg") | |
if expression_output: | |
emotion = expression_output[0]['label'] # Adjust as per the actual 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 | |
text_to_speech(joke) | |