Lucasstranger1 commited on
Commit
c884652
1 Parent(s): 22d33d7
Files changed (1) hide show
  1. app.py +21 -26
app.py CHANGED
@@ -1,19 +1,20 @@
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
7
- import time
8
 
9
  # Load environment variables from .env file
10
  load_dotenv()
11
 
12
- # Set up the Hugging Face API URLs and your API key
13
  emotion_model_url = "https://api-inference.huggingface.co/models/trpakov/vit-face-expression"
14
- text_model_url = "https://api-inference.huggingface.co/models/mrm8488/t5-base-finetuned-emotion"
15
  headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
16
 
 
 
 
17
  # Function to query the facial expression recognition model
18
  def query_emotion(filename):
19
  with open(filename, "rb") as f:
@@ -26,33 +27,27 @@ def query_emotion(filename):
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
- # Retry mechanism for text generation
35
- for attempt in range(5): # Retry up to 5 times
36
- response = requests.post(text_model_url, headers=headers, json={"inputs": prompt})
37
- if response.status_code == 200:
38
- generated_text = response.json()[0]['generated_text']
39
- if generated_text.strip(): # Ensure the response is not empty
40
- return generated_text
41
- else:
42
- st.warning("Received an empty joke, retrying...")
43
- elif response.status_code == 503: # Service Unavailable
44
- st.warning("Model is loading, retrying...")
45
- time.sleep(5) # Wait before retrying
46
- else:
47
- st.error("Error generating text: " + response.text)
48
- return "Sorry, I couldn't come up with a joke at this moment."
49
-
50
- st.error("Failed to generate text after multiple attempts.")
51
- return "Sorry, I couldn't come up with a joke at this moment."
52
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  except Exception as e:
54
  st.error(f"Error generating text: {e}")
55
- return "Sorry, I couldn't come up with a joke at this moment."
56
 
57
  # Function to convert text to speech using gTTS
58
  def text_to_speech(text):
 
1
  import os
2
  import requests
3
+ import openai
4
  import streamlit as st
 
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
  emotion_model_url = "https://api-inference.huggingface.co/models/trpakov/vit-face-expression"
 
13
  headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
14
 
15
+ # Set up OpenAI API key
16
+ openai.api_key = os.getenv('OPENAI_API_KEY')
17
+
18
  # Function to query the facial expression recognition model
19
  def query_emotion(filename):
20
  with open(filename, "rb") as f:
 
27
  st.error("Error detecting facial expression: " + response.text)
28
  return None
29
 
30
+ # Function to generate a joke or motivational text using OpenAI
31
  def generate_text_based_on_mood(emotion):
32
  try:
33
+ # Create a prompt for OpenAI
34
+ prompt = f"Generate a light-hearted joke or a motivational quote for someone who is feeling {emotion}."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ # Call OpenAI's API
37
+ response = openai.ChatCompletion.create(
38
+ model="gpt-3.5-turbo", # Use the desired model
39
+ messages=[
40
+ {"role": "user", "content": prompt}
41
+ ]
42
+ )
43
+
44
+ # Extract the generated text
45
+ generated_text = response.choices[0].message['content']
46
+ return generated_text.strip()
47
+
48
  except Exception as e:
49
  st.error(f"Error generating text: {e}")
50
+ return "Sorry, I couldn't come up with a joke or motivation at this moment."
51
 
52
  # Function to convert text to speech using gTTS
53
  def text_to_speech(text):