Spaces:
Running
Running
Lucasstranger1
commited on
Commit
•
e1d89e6
1
Parent(s):
3648473
create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
import requests
|
4 |
+
import pyttsx3
|
5 |
+
from transformers import pipeline
|
6 |
+
from PIL import Image
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
# Load environment variables from .env file
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
# Set up the Hugging Face API URL and your API key
|
13 |
+
API_URL = "https://api-inference.huggingface.co/models/trpakov/vit-face-expression"
|
14 |
+
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
|
15 |
+
|
16 |
+
# Function to query the Hugging Face model for facial expression
|
17 |
+
def query(filename):
|
18 |
+
with open(filename, "rb") as f:
|
19 |
+
data = f.read()
|
20 |
+
response = requests.post(API_URL, headers=headers, data=data)
|
21 |
+
|
22 |
+
if response.status_code == 200:
|
23 |
+
return response.json()
|
24 |
+
else:
|
25 |
+
st.error("Error detecting facial expression: " + response.text)
|
26 |
+
return None
|
27 |
+
|
28 |
+
# Function to generate a joke or uplifting text based on the mood
|
29 |
+
def generate_text_based_on_mood(emotion):
|
30 |
+
generator = pipeline('text-generation', model='gpt2')
|
31 |
+
prompt = f"Tell a joke that would cheer someone who is feeling {emotion}."
|
32 |
+
response = generator(prompt, max_length=50, num_return_sequences=1)
|
33 |
+
return response[0]['generated_text']
|
34 |
+
|
35 |
+
# Function to convert text to speech
|
36 |
+
def text_to_speech(text):
|
37 |
+
engine = pyttsx3.init()
|
38 |
+
engine.say(text)
|
39 |
+
engine.runAndWait()
|
40 |
+
|
41 |
+
# Streamlit UI
|
42 |
+
st.title("Facial Expression Mood Detector")
|
43 |
+
st.write("Upload an image of a face to detect mood and receive uplifting messages or jokes.")
|
44 |
+
|
45 |
+
# Upload image
|
46 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
47 |
+
|
48 |
+
if uploaded_file is not None:
|
49 |
+
# Load and display the image
|
50 |
+
image = Image.open(uploaded_file)
|
51 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
52 |
+
|
53 |
+
# Save the uploaded file temporarily
|
54 |
+
with open("uploaded_image.jpg", "wb") as f:
|
55 |
+
f.write(uploaded_file.getbuffer())
|
56 |
+
|
57 |
+
# Detect facial expression
|
58 |
+
expression_output = query("uploaded_image.jpg")
|
59 |
+
if expression_output:
|
60 |
+
emotion = expression_output[0]['label'] # Adjust as per the actual response structure
|
61 |
+
st.write(f"Detected emotion: {emotion}")
|
62 |
+
|
63 |
+
# Generate text based on detected emotion
|
64 |
+
joke = generate_text_based_on_mood(emotion)
|
65 |
+
st.write("Here's something to cheer you up:")
|
66 |
+
st.write(joke)
|
67 |
+
|
68 |
+
# Convert the generated joke to audio
|
69 |
+
text_to_speech(joke)
|