Lucasstranger1 commited on
Commit
4a17310
1 Parent(s): d9c2f6c
Files changed (1) hide show
  1. app.py +17 -22
app.py CHANGED
@@ -1,20 +1,22 @@
1
  import os
2
  import streamlit as st
3
  import requests
4
- from gtts import gTTS
5
  from transformers import pipeline
6
  from PIL import Image
7
  from dotenv import load_dotenv
8
 
9
- API_URL = "https://api-inference.huggingface.co/models/trpakov/vit-face-expression"
10
- headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
11
 
12
- # Your existing functions and Streamlit code...
 
13
 
14
- # Example query function
15
  def query(filename):
16
  with open(filename, "rb") as f:
17
  data = f.read()
 
 
18
  response = requests.post(API_URL, headers=headers, data=data)
19
 
20
  if response.status_code == 200:
@@ -25,16 +27,15 @@ def query(filename):
25
 
26
  # Function to generate a joke or uplifting text based on the mood
27
  def generate_text_based_on_mood(emotion):
28
- generator = pipeline('text-generation', model='gpt2')
29
- prompt = f"Tell a joke that would cheer someone who is feeling {emotion}."
30
- response = generator(prompt, max_length=50, num_return_sequences=1)
31
- return response[0]['generated_text']
32
-
33
- # Function to convert text to speech using gTTS
34
- def text_to_speech(text):
35
- tts = gTTS(text, lang='en')
36
- tts.save("output.mp3") # Save to a file
37
- return "output.mp3" # Return the filename for playing
38
 
39
  # Streamlit UI
40
  st.title("Facial Expression Mood Detector")
@@ -55,16 +56,10 @@ if uploaded_file is not None:
55
  # Detect facial expression
56
  expression_output = query("uploaded_image.jpg")
57
  if expression_output:
58
- emotion = expression_output[0]['label'] # Adjust as per the actual response structure
59
  st.write(f"Detected emotion: {emotion}")
60
 
61
  # Generate text based on detected emotion
62
  joke = generate_text_based_on_mood(emotion)
63
  st.write("Here's something to cheer you up:")
64
  st.write(joke)
65
-
66
- # Convert the generated joke to audio
67
- audio_file = text_to_speech(joke)
68
-
69
- # Provide an audio player in the Streamlit app
70
- st.audio(audio_file) # Streamlit will handle playback
 
1
  import os
2
  import streamlit as st
3
  import requests
 
4
  from transformers import pipeline
5
  from PIL import Image
6
  from dotenv import load_dotenv
7
 
8
+ # Load environment variables from .env file
9
+ load_dotenv()
10
 
11
+ # Set up the Hugging Face API URL and your API key
12
+ headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
13
 
14
+ # Function to query the Hugging Face model for facial expression
15
  def query(filename):
16
  with open(filename, "rb") as f:
17
  data = f.read()
18
+ # Use the facial expression model
19
+ API_URL = "https://api-inference.huggingface.co/models/microsoft/face-expression-recognition"
20
  response = requests.post(API_URL, headers=headers, data=data)
21
 
22
  if response.status_code == 200:
 
27
 
28
  # Function to generate a joke or uplifting text based on the mood
29
  def generate_text_based_on_mood(emotion):
30
+ try:
31
+ # Use GPT-Neo model
32
+ generator = pipeline('text-generation', model='EleutherAI/gpt-neo-125M')
33
+ prompt = f"Tell a joke that would cheer someone who is feeling {emotion}."
34
+ response = generator(prompt, max_length=50, num_return_sequences=1)
35
+ return response[0]['generated_text']
36
+ except Exception as e:
37
+ st.error(f"Error generating text: {e}")
38
+ return "Sorry, I couldn't come up with a joke at this moment."
 
39
 
40
  # Streamlit UI
41
  st.title("Facial Expression Mood Detector")
 
56
  # Detect facial expression
57
  expression_output = query("uploaded_image.jpg")
58
  if expression_output:
59
+ emotion = expression_output[0]['label'] # Adjust based on response structure
60
  st.write(f"Detected emotion: {emotion}")
61
 
62
  # Generate text based on detected emotion
63
  joke = generate_text_based_on_mood(emotion)
64
  st.write("Here's something to cheer you up:")
65
  st.write(joke)