Lucasstranger1 commited on
Commit
acaac68
1 Parent(s): 244ff0b
Files changed (1) hide show
  1. app.py +16 -39
app.py CHANGED
@@ -8,15 +8,16 @@ from dotenv import load_dotenv
8
  # Load environment variables from .env file
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:
22
  return response.json()
@@ -24,41 +25,17 @@ def query(filename):
24
  st.error("Error detecting facial expression: " + response.text)
25
  return None
26
 
27
- # Function to generate a joke or uplifting text based on the mood
28
  def generate_text_based_on_mood(emotion):
29
  try:
30
- # Predefined jokes for different emotions
31
- jokes = {
32
- "happy": [
33
- "Why did the scarecrow win an award? Because he was outstanding in his field!",
34
- "What do you call fake spaghetti? An impasta!"
35
- ],
36
- "sad": [
37
- "Why don’t scientists trust atoms? Because they make up everything!",
38
- "Why did the bicycle fall over? Because it was two-tired."
39
- ],
40
- "angry": [
41
- "Why did the tomato turn red? Because it saw the salad dressing!",
42
- "What did one angry man say to the other? Nothing, he just threw a rock!"
43
- ],
44
- "surprised": [
45
- "Did you hear about the claustrophobic astronaut? He just needed a little space!",
46
- "Why don’t skeletons fight each other? They don’t have the guts."
47
- ],
48
- "neutral": [
49
- "What do you call cheese that isn't yours? Nacho cheese!"
50
- ]
51
- }
52
-
53
- # Use predefined jokes if available
54
- if emotion in jokes:
55
- return jokes[emotion][0] # Return the first joke for simplicity
56
-
57
- # If no predefined joke, use GPT-Neo
58
- generator = pipeline('text-generation', model='EleutherAI/gpt-neo-125M')
59
- prompt = f"Generate a light-hearted joke for someone who is feeling {emotion}."
60
- response = generator(prompt, max_length=50, num_return_sequences=1)
61
- return response[0]['generated_text']
62
 
63
  except Exception as e:
64
  st.error(f"Error generating text: {e}")
@@ -89,7 +66,7 @@ if uploaded_file is not None:
89
  f.write(uploaded_file.getbuffer())
90
 
91
  # Detect facial expression
92
- expression_output = query("uploaded_image.jpg")
93
  if expression_output:
94
  # Assuming the response has a 'label' field with the detected emotion
95
  emotion = expression_output[0]['label'] # Adjust based on response structure
 
8
  # Load environment variables from .env file
9
  load_dotenv()
10
 
11
+ # Set up the Hugging Face API URLs and your API key
12
+ emotion_model_url = "https://api-inference.huggingface.co/models/trpakov/vit-face-expression"
13
+ text_model_url = "https://api-inference.huggingface.co/models/mrm8488/t5-base-finetuned-emotion"
14
  headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
15
 
16
+ # Function to query the facial expression recognition model
17
+ def query_emotion(filename):
18
  with open(filename, "rb") as f:
19
  data = f.read()
20
+ response = requests.post(emotion_model_url, headers=headers, data=data)
21
 
22
  if response.status_code == 200:
23
  return response.json()
 
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 detected emotion
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
+ if response.status_code == 200:
35
+ return response.json()[0]['generated_text']
36
+ else:
37
+ st.error("Error generating text: " + response.text)
38
+ return "Sorry, I couldn't come up with a joke at this moment."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  except Exception as e:
41
  st.error(f"Error generating text: {e}")
 
66
  f.write(uploaded_file.getbuffer())
67
 
68
  # Detect facial expression
69
+ expression_output = query_emotion("uploaded_image.jpg")
70
  if expression_output:
71
  # Assuming the response has a 'label' field with the detected emotion
72
  emotion = expression_output[0]['label'] # Adjust based on response structure