Spaces:
Running
Running
Lucasstranger1
commited on
Commit
•
bddd4b6
1
Parent(s):
3e57df9
update
Browse files
app.py
CHANGED
@@ -10,38 +10,35 @@ from transformers import AutoProcessor, AutoModelForImageClassification
|
|
10 |
# Load environment variables from .env file
|
11 |
load_dotenv()
|
12 |
|
13 |
-
# Set up the Hugging Face API for emotion detection
|
14 |
-
emotion_model_url = "https://api-inference.huggingface.co/models/trpakov/vit-face-expression"
|
15 |
-
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
|
16 |
-
|
17 |
# Set up OpenAI API key
|
18 |
openai.api_key = os.getenv('OPENAI_API_KEY')
|
19 |
|
|
|
|
|
|
|
|
|
20 |
# Function to query the facial expression recognition model
|
21 |
def query_emotion(image):
|
22 |
-
# Load the processor and model
|
23 |
-
processor = AutoProcessor.from_pretrained("trpakov/vit-face-expression")
|
24 |
-
model = AutoModelForImageClassification.from_pretrained("trpakov/vit-face-expression")
|
25 |
-
|
26 |
# Preprocess the image
|
27 |
inputs = processor(images=image, return_tensors="pt")
|
28 |
|
29 |
# Perform inference
|
30 |
with torch.no_grad():
|
31 |
outputs = model(**inputs)
|
32 |
-
|
33 |
-
# Get predicted class index
|
34 |
logits = outputs.logits
|
35 |
predicted_class_idx = torch.argmax(logits, dim=-1).item()
|
36 |
|
37 |
-
#
|
38 |
-
|
|
|
|
|
39 |
return predicted_label
|
40 |
|
41 |
# Function to generate a response using OpenAI based on detected emotion
|
42 |
def generate_text_based_on_mood(emotion):
|
43 |
try:
|
44 |
-
# Create a dynamic prompt based on the detected emotion
|
45 |
prompt = f"Generate a light-hearted joke or motivational message for someone who is feeling {emotion}."
|
46 |
|
47 |
# Call OpenAI's API using GPT-4
|
|
|
10 |
# Load environment variables from .env file
|
11 |
load_dotenv()
|
12 |
|
|
|
|
|
|
|
|
|
13 |
# Set up OpenAI API key
|
14 |
openai.api_key = os.getenv('OPENAI_API_KEY')
|
15 |
|
16 |
+
# Load the processor and model for facial expression recognition
|
17 |
+
processor = AutoProcessor.from_pretrained("trpakov/vit-face-expression")
|
18 |
+
model = AutoModelForImageClassification.from_pretrained("trpakov/vit-face-expression")
|
19 |
+
|
20 |
# Function to query the facial expression recognition model
|
21 |
def query_emotion(image):
|
|
|
|
|
|
|
|
|
22 |
# Preprocess the image
|
23 |
inputs = processor(images=image, return_tensors="pt")
|
24 |
|
25 |
# Perform inference
|
26 |
with torch.no_grad():
|
27 |
outputs = model(**inputs)
|
28 |
+
|
29 |
+
# Get predicted class index (the class with the highest logit)
|
30 |
logits = outputs.logits
|
31 |
predicted_class_idx = torch.argmax(logits, dim=-1).item()
|
32 |
|
33 |
+
# Retrieve the label names from the model
|
34 |
+
label_names = model.config.id2label # Mapping of indices to emotion labels
|
35 |
+
predicted_label = label_names[predicted_class_idx] # Get the predicted label
|
36 |
+
|
37 |
return predicted_label
|
38 |
|
39 |
# Function to generate a response using OpenAI based on detected emotion
|
40 |
def generate_text_based_on_mood(emotion):
|
41 |
try:
|
|
|
42 |
prompt = f"Generate a light-hearted joke or motivational message for someone who is feeling {emotion}."
|
43 |
|
44 |
# Call OpenAI's API using GPT-4
|