Commit
·
3a40934
1
Parent(s):
8f2f602
commit
Browse files
app.py
CHANGED
|
@@ -1,18 +1,12 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import BlipProcessor, BlipForConditionalGeneration, AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
-
from transformers import
|
| 4 |
-
import torch
|
| 5 |
|
| 6 |
# Load the image captioning model and tokenizer
|
| 7 |
caption_model_name = "Salesforce/blip-image-captioning-large"
|
| 8 |
caption_processor = BlipProcessor.from_pretrained(caption_model_name)
|
| 9 |
caption_model = BlipForConditionalGeneration.from_pretrained(caption_model_name)
|
| 10 |
|
| 11 |
-
# Load the emotion analysis model and tokenizer
|
| 12 |
-
emotion_model_name = "SamLowe/roberta-base-go_emotions"
|
| 13 |
-
emotion_tokenizer = AutoTokenizer.from_pretrained(emotion_model_name)
|
| 14 |
-
emotion_model = AutoModelForSequenceClassification.from_pretrained(emotion_model_name)
|
| 15 |
-
|
| 16 |
def generate_caption_and_analyze_emotions(image):
|
| 17 |
# Preprocess the image for caption generation
|
| 18 |
caption_inputs = caption_processor(images=image, return_tensors="pt")
|
|
@@ -23,23 +17,16 @@ def generate_caption_and_analyze_emotions(image):
|
|
| 23 |
# Decode the output caption
|
| 24 |
decoded_caption = caption_processor.decode(caption[0], skip_special_tokens=True)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
# Get the predicted emotion label
|
| 38 |
-
emotion_label_id = emotion_outputs.logits.argmax().item()
|
| 39 |
-
emotion_label = emotion_tokenizer.decode(emotion_label_id)
|
| 40 |
-
|
| 41 |
-
# Prepare the final output with sentiment information
|
| 42 |
-
final_output = f"The sentiment in the provided image shows: {emotion_label}.\n\nGenerated Caption: {decoded_caption}"
|
| 43 |
return final_output
|
| 44 |
|
| 45 |
# Define the Gradio interface
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import BlipProcessor, BlipForConditionalGeneration, AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
from transformers import pipeline
|
|
|
|
| 4 |
|
| 5 |
# Load the image captioning model and tokenizer
|
| 6 |
caption_model_name = "Salesforce/blip-image-captioning-large"
|
| 7 |
caption_processor = BlipProcessor.from_pretrained(caption_model_name)
|
| 8 |
caption_model = BlipForConditionalGeneration.from_pretrained(caption_model_name)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def generate_caption_and_analyze_emotions(image):
|
| 11 |
# Preprocess the image for caption generation
|
| 12 |
caption_inputs = caption_processor(images=image, return_tensors="pt")
|
|
|
|
| 17 |
# Decode the output caption
|
| 18 |
decoded_caption = caption_processor.decode(caption[0], skip_special_tokens=True)
|
| 19 |
|
| 20 |
+
# Load the emotion analysis model and tokenizer
|
| 21 |
+
emotion_model_name = "SamLowe/roberta-base-go_emotions"
|
| 22 |
+
emotion_classifier = pipeline(model=emotion_model_name)
|
| 23 |
+
|
| 24 |
+
results = emotion_classifier(decoded_caption)
|
| 25 |
+
if results[0]['label'] == 'neutral' or results[0]['score'] <= 0.40:
|
| 26 |
+
final_output = f"Sentiment of image is not clear, image shows {decoded_caption}."
|
| 27 |
+
else:
|
| 28 |
+
final_output = f"Sentiment of the image shows {results[0]['label']}."
|
| 29 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return final_output
|
| 31 |
|
| 32 |
# Define the Gradio interface
|