Commit
·
e3a39c5
1
Parent(s):
6ba54d7
commit
Browse files
app.py
CHANGED
|
@@ -21,14 +21,27 @@ def generate_caption_and_analyze_emotions(image):
|
|
| 21 |
# Decode the output caption
|
| 22 |
decoded_caption = caption_processor.decode(caption[0], skip_special_tokens=True)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
# Analyze emotions of the generated caption
|
| 25 |
# Preprocess the caption for emotion analysis
|
| 26 |
-
emotion_inputs = emotion_tokenizer(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
emotion_outputs = emotion_model(**emotion_inputs)
|
| 28 |
|
| 29 |
# Get the predicted emotion label
|
| 30 |
emotion_label = emotion_tokenizer.decode(emotion_outputs.logits.argmax())
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Prepare the final output with sentiment information
|
| 33 |
final_output = f"The sentiment in the provided image shows: {emotion_label}.\n\nGenerated Caption: {decoded_caption}"
|
| 34 |
return final_output
|
|
|
|
| 21 |
# Decode the output caption
|
| 22 |
decoded_caption = caption_processor.decode(caption[0], skip_special_tokens=True)
|
| 23 |
|
| 24 |
+
# Debug print for the raw output caption
|
| 25 |
+
print("Raw Generated Caption:", decoded_caption)
|
| 26 |
+
|
| 27 |
# Analyze emotions of the generated caption
|
| 28 |
# Preprocess the caption for emotion analysis
|
| 29 |
+
emotion_inputs = emotion_tokenizer.encode_plus(
|
| 30 |
+
decoded_caption,
|
| 31 |
+
max_length=128,
|
| 32 |
+
padding="max_length",
|
| 33 |
+
truncation=True,
|
| 34 |
+
return_tensors="pt"
|
| 35 |
+
)
|
| 36 |
emotion_outputs = emotion_model(**emotion_inputs)
|
| 37 |
|
| 38 |
# Get the predicted emotion label
|
| 39 |
emotion_label = emotion_tokenizer.decode(emotion_outputs.logits.argmax())
|
| 40 |
|
| 41 |
+
# Debug print for caption and emotion label
|
| 42 |
+
#print("Generated Caption:", decoded_caption)
|
| 43 |
+
#print("Predicted Emotion Label:", emotion_label)
|
| 44 |
+
|
| 45 |
# Prepare the final output with sentiment information
|
| 46 |
final_output = f"The sentiment in the provided image shows: {emotion_label}.\n\nGenerated Caption: {decoded_caption}"
|
| 47 |
return final_output
|