Lucasstranger1 commited on
Commit
e436a2e
1 Parent(s): 4a17310
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -1,6 +1,6 @@
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
@@ -9,14 +9,13 @@ from dotenv import load_dotenv
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:
@@ -28,7 +27,7 @@ def query(filename):
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)
@@ -37,6 +36,14 @@ def generate_text_based_on_mood(emotion):
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")
42
  st.write("Upload an image of a face to detect mood and receive uplifting messages or jokes.")
@@ -56,6 +63,7 @@ if uploaded_file is not None:
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
 
@@ -63,3 +71,9 @@ if uploaded_file is not None:
63
  joke = generate_text_based_on_mood(emotion)
64
  st.write("Here's something to cheer you up:")
65
  st.write(joke)
 
 
 
 
 
 
 
1
  import os
 
2
  import requests
3
+ import streamlit as st
4
  from transformers import pipeline
5
  from PIL import Image
6
  from dotenv import load_dotenv
 
9
  load_dotenv()
10
 
11
  # Set up the Hugging Face API URL and your API key
12
+ API_URL = "https://api-inference.huggingface.co/models/trpakov/vit-face-expression"
13
  headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
14
 
15
  # Function to query the Hugging Face model for facial expression
16
  def query(filename):
17
  with open(filename, "rb") as f:
18
  data = f.read()
 
 
19
  response = requests.post(API_URL, headers=headers, data=data)
20
 
21
  if response.status_code == 200:
 
27
  # Function to generate a joke or uplifting text based on the mood
28
  def generate_text_based_on_mood(emotion):
29
  try:
30
+ # Use GPT-Neo model for text generation
31
  generator = pipeline('text-generation', model='EleutherAI/gpt-neo-125M')
32
  prompt = f"Tell a joke that would cheer someone who is feeling {emotion}."
33
  response = generator(prompt, max_length=50, num_return_sequences=1)
 
36
  st.error(f"Error generating text: {e}")
37
  return "Sorry, I couldn't come up with a joke at this moment."
38
 
39
+ # Function to convert text to speech using gTTS
40
+ def text_to_speech(text):
41
+ from gtts import gTTS
42
+ tts = gTTS(text, lang='en')
43
+ audio_file = "output.mp3"
44
+ tts.save(audio_file) # Save the audio file
45
+ return audio_file
46
+
47
  # Streamlit UI
48
  st.title("Facial Expression Mood Detector")
49
  st.write("Upload an image of a face to detect mood and receive uplifting messages or jokes.")
 
63
  # Detect facial expression
64
  expression_output = query("uploaded_image.jpg")
65
  if expression_output:
66
+ # Assuming the response has a 'label' field with the detected emotion
67
  emotion = expression_output[0]['label'] # Adjust based on response structure
68
  st.write(f"Detected emotion: {emotion}")
69
 
 
71
  joke = generate_text_based_on_mood(emotion)
72
  st.write("Here's something to cheer you up:")
73
  st.write(joke)
74
+
75
+ # Convert the generated joke to audio
76
+ audio_file = text_to_speech(joke)
77
+
78
+ # Provide an audio player in the Streamlit app
79
+ st.audio(audio_file) # Streamlit will handle playback