Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,37 +10,45 @@ caption_model = BlipForConditionalGeneration.from_pretrained(caption_model_name)
|
|
| 10 |
emotion_model_name = "SamLowe/roberta-base-go_emotions"
|
| 11 |
emotion_classifier = pipeline("text-classification", model=emotion_model_name)
|
| 12 |
|
| 13 |
-
def generate_caption_and_analyze_emotions(image):
|
| 14 |
try:
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
results = emotion_classifier(decoded_caption)
|
| 26 |
sentiment_label = results[0]['label']
|
| 27 |
if sentiment_label == 'neutral':
|
| 28 |
-
sentiment_text = "Sentiment of the
|
| 29 |
else:
|
| 30 |
-
sentiment_text = "Sentiment of the
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
-
return f"An error occurred: {e}"
|
| 36 |
|
| 37 |
# Define the Gradio interface using the new API
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# Create the Gradio app
|
| 42 |
-
app = gr.Interface(fn=generate_caption_and_analyze_emotions, inputs=
|
| 43 |
|
| 44 |
# Launch the Gradio app
|
| 45 |
if __name__ == "__main__":
|
| 46 |
app.launch()
|
|
|
|
|
|
| 10 |
emotion_model_name = "SamLowe/roberta-base-go_emotions"
|
| 11 |
emotion_classifier = pipeline("text-classification", model=emotion_model_name)
|
| 12 |
|
| 13 |
+
def generate_caption_and_analyze_emotions(image, text=None):
|
| 14 |
try:
|
| 15 |
+
if image is not None:
|
| 16 |
+
# Preprocess the image for caption generation
|
| 17 |
+
caption_inputs = caption_processor(images=image, return_tensors="pt")
|
| 18 |
|
| 19 |
+
# Generate caption using the caption model
|
| 20 |
+
caption_ids = caption_model.generate(**caption_inputs)
|
| 21 |
|
| 22 |
+
# Decode the output caption
|
| 23 |
+
decoded_caption = caption_processor.decode(caption_ids[0], skip_special_tokens=True)
|
| 24 |
+
else:
|
| 25 |
+
decoded_caption = text
|
| 26 |
+
|
| 27 |
+
# Perform emotion analysis on the generated caption or provided text
|
| 28 |
results = emotion_classifier(decoded_caption)
|
| 29 |
sentiment_label = results[0]['label']
|
| 30 |
if sentiment_label == 'neutral':
|
| 31 |
+
sentiment_text = "Sentiment of the text is"
|
| 32 |
else:
|
| 33 |
+
sentiment_text = "Sentiment of the text shows"
|
| 34 |
|
| 35 |
+
caption_output = f"Caption: '{decoded_caption}'"
|
| 36 |
+
sentiment_output = f"{sentiment_text} {sentiment_label}."
|
| 37 |
+
|
| 38 |
+
return caption_output, sentiment_output
|
| 39 |
except Exception as e:
|
| 40 |
+
return f"An error occurred: {e}", ""
|
| 41 |
|
| 42 |
# Define the Gradio interface using the new API
|
| 43 |
+
image_input = gr.inputs.Image(label="Upload an image")
|
| 44 |
+
text_input = gr.inputs.Textbox(label="Or enter text")
|
| 45 |
+
|
| 46 |
+
outputs = [gr.outputs.Textbox(label="Generated Caption"), gr.outputs.Textbox(label="Sentiment Analysis")]
|
| 47 |
|
| 48 |
# Create the Gradio app
|
| 49 |
+
app = gr.Interface(fn=generate_caption_and_analyze_emotions, inputs=[image_input, text_input], outputs=outputs)
|
| 50 |
|
| 51 |
# Launch the Gradio app
|
| 52 |
if __name__ == "__main__":
|
| 53 |
app.launch()
|
| 54 |
+
|