Ross - Final submission
Browse files
app.py
CHANGED
|
@@ -1,136 +1,60 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
"image-classification",
|
| 10 |
-
model="google/vit-base-patch16-224",
|
| 11 |
-
use_fast=True
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
captioner = pipeline(
|
| 15 |
-
"image-text-to-text",
|
| 16 |
-
model="Salesforce/blip-image-captioning-base"
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
sentiment = pipeline(
|
| 20 |
-
"sentiment-analysis",
|
| 21 |
-
model="distilbert-base-uncased-finetuned-sst-2-english"
|
| 22 |
)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
caption = caption.lower()
|
| 28 |
-
|
| 29 |
-
keyword_map = {
|
| 30 |
-
"dog": ["dog", "puppy"],
|
| 31 |
-
"cat": ["cat", "kitten"],
|
| 32 |
-
"car": ["car", "vehicle"],
|
| 33 |
-
"pizza": ["pizza"],
|
| 34 |
-
"bird": ["bird"],
|
| 35 |
-
"person": ["person", "man", "woman", "people"],
|
| 36 |
-
"bicycle": ["bike", "bicycle"],
|
| 37 |
-
"laptop": ["laptop", "computer"],
|
| 38 |
-
"beach": ["beach", "ocean", "sea"],
|
| 39 |
-
"mountain": ["mountain"]
|
| 40 |
-
}
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
if word in caption:
|
| 46 |
-
return True
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
# Image Processing Function
|
| 51 |
-
def process_image(image):
|
| 52 |
try:
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
print(f"Debug: Image info (if PIL): {image.format, image.size, image.mode}")
|
| 64 |
-
# Pass image as a list and provide a default text prompt
|
| 65 |
-
caption_result = captioner(images=[image], text="a photo of")
|
| 66 |
-
|
| 67 |
-
caption = caption_result[0]['generated_text']
|
| 68 |
-
|
| 69 |
-
# Handle empty caption
|
| 70 |
-
if not caption or len(caption.strip()) == 0:
|
| 71 |
-
sentiment_label = "N/A"
|
| 72 |
-
sentiment_score = 0.0
|
| 73 |
-
comparison_text = "No Caption ❌"
|
| 74 |
-
caption = "No caption generated."
|
| 75 |
-
else:
|
| 76 |
-
# 3. Sentiment
|
| 77 |
-
sentiment_result = sentiment(caption)[0]
|
| 78 |
-
sentiment_label = sentiment_result['label']
|
| 79 |
-
sentiment_score = round(sentiment_result['score'], 3)
|
| 80 |
-
|
| 81 |
-
# 4. Comparison
|
| 82 |
-
match = top_class.lower() in caption.lower()
|
| 83 |
-
comparison_text = "Match ✅" if match else "No Match ❌"
|
| 84 |
-
|
| 85 |
-
return (
|
| 86 |
-
f"{top_class} (Confidence: {confidence})",
|
| 87 |
-
caption,
|
| 88 |
-
f"{sentiment_label} (Score: {sentiment_score})",
|
| 89 |
-
comparison_text
|
| 90 |
)
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
"Error in captioning",
|
| 96 |
-
"Error in sentiment",
|
| 97 |
-
str(e)
|
| 98 |
)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
dummy_image_path = "dummy_image.png"
|
| 102 |
-
img_data = np.zeros((100, 100, 3), dtype=np.uint8) # 100x100 black image
|
| 103 |
-
dummy_img = Image.fromarray(img_data)
|
| 104 |
-
dummy_img.save(dummy_image_path)
|
| 105 |
-
|
| 106 |
-
# Load the dummy image for testing
|
| 107 |
-
img = Image.open(dummy_image_path)
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
# Pass image as a list and provide a default text prompt for testing
|
| 113 |
-
captioner_test_result = captioner(images=[img], text="a photo of")
|
| 114 |
-
print("Captioner test result:", captioner_test_result)
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
inputs=gr.Image(type="pil"),
|
| 120 |
outputs=[
|
| 121 |
-
gr.
|
| 122 |
-
gr.
|
| 123 |
-
gr.Text(label="Caption Sentiment"),
|
| 124 |
-
gr.Text(label="Model Agreement")
|
| 125 |
],
|
| 126 |
-
title="Multimodal Image
|
| 127 |
description=(
|
| 128 |
-
"
|
| 129 |
-
"
|
| 130 |
-
"
|
| 131 |
)
|
| 132 |
)
|
| 133 |
|
| 134 |
-
|
| 135 |
-
if __name__ == "__main__":
|
| 136 |
-
interface.launch(debug=True)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import (
|
| 4 |
+
BlipProcessor,
|
| 5 |
+
BlipForConditionalGeneration,
|
| 6 |
+
AutoTokenizer,
|
| 7 |
+
AutoModelForSeq2SeqLM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
)
|
| 9 |
|
| 10 |
+
# Image captioning model
|
| 11 |
+
caption_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 12 |
+
caption_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# English-to-Spanish translation model
|
| 15 |
+
translation_tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-es")
|
| 16 |
+
translation_model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-es")
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
def analyze_image(image):
|
| 19 |
+
if image is None:
|
| 20 |
+
return "Please upload an image first.", ""
|
| 21 |
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
+
# Generate English caption
|
| 24 |
+
inputs = caption_processor(image, return_tensors="pt")
|
| 25 |
+
caption_ids = caption_model.generate(**inputs, max_new_tokens=40)
|
| 26 |
+
english_caption = caption_processor.decode(caption_ids[0], skip_special_tokens=True)
|
| 27 |
+
|
| 28 |
+
# Translate caption to Spanish
|
| 29 |
+
translation_inputs = translation_tokenizer(
|
| 30 |
+
english_caption,
|
| 31 |
+
return_tensors="pt",
|
| 32 |
+
padding=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
+
translation_ids = translation_model.generate(**translation_inputs, max_new_tokens=60)
|
| 35 |
+
spanish_caption = translation_tokenizer.decode(
|
| 36 |
+
translation_ids[0],
|
| 37 |
+
skip_special_tokens=True
|
|
|
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
+
return english_caption, spanish_caption
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Error: {str(e)}", ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
demo = gr.Interface(
|
| 46 |
+
fn=analyze_image,
|
| 47 |
+
inputs=gr.Image(type="pil", label="Upload an Image"),
|
|
|
|
| 48 |
outputs=[
|
| 49 |
+
gr.Textbox(label="English Image Caption"),
|
| 50 |
+
gr.Textbox(label="Spanish Translation")
|
|
|
|
|
|
|
| 51 |
],
|
| 52 |
+
title="Multimodal Image Captioning and Spanish Translation Tool",
|
| 53 |
description=(
|
| 54 |
+
"This app uses two AI models. First, it uses a vision-language model to describe "
|
| 55 |
+
"an uploaded image in English. Then, it uses a language translation model to translate "
|
| 56 |
+
"that caption into Spanish."
|
| 57 |
)
|
| 58 |
)
|
| 59 |
|
| 60 |
+
demo.launch()
|
|
|
|
|
|