Spaces:
Running
Running
Lucasstranger1
commited on
Commit
•
dc26248
1
Parent(s):
acaac68
update
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import streamlit as st
|
|
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()
|
@@ -25,17 +26,24 @@ def query_emotion(filename):
|
|
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
|
29 |
def generate_text_based_on_mood(emotion):
|
30 |
try:
|
31 |
prompt = f"Generate a light-hearted joke or uplifting message for someone who is feeling {emotion}."
|
32 |
-
response = requests.post(text_model_url, headers=headers, json={"inputs": prompt})
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
except Exception as e:
|
41 |
st.error(f"Error generating text: {e}")
|
@@ -44,10 +52,18 @@ def generate_text_based_on_mood(emotion):
|
|
44 |
# Function to convert text to speech using gTTS
|
45 |
def text_to_speech(text):
|
46 |
from gtts import gTTS
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
# Streamlit UI
|
53 |
st.title("Facial Expression Mood Detector")
|
@@ -80,5 +96,6 @@ if uploaded_file is not None:
|
|
80 |
# Convert the generated joke to audio
|
81 |
audio_file = text_to_speech(joke)
|
82 |
|
83 |
-
# Provide an audio player in the Streamlit app
|
84 |
-
|
|
|
|
4 |
from transformers import pipeline
|
5 |
from PIL import Image
|
6 |
from dotenv import load_dotenv
|
7 |
+
import time
|
8 |
|
9 |
# Load environment variables from .env file
|
10 |
load_dotenv()
|
|
|
26 |
st.error("Error detecting facial expression: " + response.text)
|
27 |
return None
|
28 |
|
29 |
+
# Function to generate a joke or uplifting text based on the mood
|
30 |
def generate_text_based_on_mood(emotion):
|
31 |
try:
|
32 |
prompt = f"Generate a light-hearted joke or uplifting message for someone who is feeling {emotion}."
|
|
|
33 |
|
34 |
+
for attempt in range(5): # Retry up to 5 times
|
35 |
+
response = requests.post(text_model_url, headers=headers, json={"inputs": prompt})
|
36 |
+
if response.status_code == 200:
|
37 |
+
return response.json()[0]['generated_text']
|
38 |
+
elif response.status_code == 503: # Service Unavailable
|
39 |
+
st.warning("Model is loading, retrying...")
|
40 |
+
time.sleep(5) # Wait before retrying
|
41 |
+
else:
|
42 |
+
st.error("Error generating text: " + response.text)
|
43 |
+
return "Sorry, I couldn't come up with a joke at this moment."
|
44 |
+
|
45 |
+
st.error("Failed to generate text after multiple attempts.")
|
46 |
+
return "Sorry, I couldn't come up with a joke at this moment."
|
47 |
|
48 |
except Exception as e:
|
49 |
st.error(f"Error generating text: {e}")
|
|
|
52 |
# Function to convert text to speech using gTTS
|
53 |
def text_to_speech(text):
|
54 |
from gtts import gTTS
|
55 |
+
try:
|
56 |
+
tts = gTTS(text, lang='en')
|
57 |
+
audio_file = "output.mp3"
|
58 |
+
tts.save(audio_file) # Save the audio file
|
59 |
+
return audio_file
|
60 |
+
except Exception as e:
|
61 |
+
if "429" in str(e):
|
62 |
+
st.error("Too many requests to the TTS service. Please try again later.")
|
63 |
+
return None
|
64 |
+
else:
|
65 |
+
st.error(f"Error with TTS: {e}")
|
66 |
+
return None
|
67 |
|
68 |
# Streamlit UI
|
69 |
st.title("Facial Expression Mood Detector")
|
|
|
96 |
# Convert the generated joke to audio
|
97 |
audio_file = text_to_speech(joke)
|
98 |
|
99 |
+
# Provide an audio player in the Streamlit app if audio file exists
|
100 |
+
if audio_file:
|
101 |
+
st.audio(audio_file) # Streamlit will handle playback
|