Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,11 +6,11 @@ from PIL import Image
|
|
| 6 |
from collections import deque
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
-
# Load main BLIP model for
|
| 10 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 11 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 12 |
|
| 13 |
-
# Load YOLOv5 model for object detection using ultralytics
|
| 14 |
detect_model = YOLO('yolov5s.pt')
|
| 15 |
|
| 16 |
# Session memory for last 15 images and captions
|
|
@@ -51,30 +51,55 @@ def generate_caption(image):
|
|
| 51 |
result_text = f"Detected objects: {tags}\nCaption: {caption}"
|
| 52 |
return result_text, gallery
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
Upload an image and click **Generate Caption** to discover:
|
| 63 |
-
- π― The **objects** detected in your image
|
| 64 |
-
- ποΈ A **caption** created automatically by the AI
|
| 65 |
-
|
| 66 |
-
π Your last 15 results are displayed below for quick reference.
|
| 67 |
-
π‘ Tip: Upload clear, well-lit images for best results!
|
| 68 |
-
""",
|
| 69 |
-
elem_id="instructions"
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
with gr.Column():
|
| 73 |
-
image_input = gr.Image(type="pil", label="Upload Image")
|
| 74 |
-
generate_btn = gr.Button("Generate Caption")
|
| 75 |
|
| 76 |
-
|
|
|
|
| 77 |
|
|
|
|
| 78 |
gallery = gr.Gallery(label="Last 15 Images and Captions", scale=3)
|
| 79 |
|
| 80 |
def on_generate(image):
|
|
|
|
| 6 |
from collections import deque
|
| 7 |
import numpy as np
|
| 8 |
|
| 9 |
+
# Load main BLIP model for English captioning
|
| 10 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 11 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 12 |
|
| 13 |
+
# Load YOLOv5 small model for object detection using ultralytics package
|
| 14 |
detect_model = YOLO('yolov5s.pt')
|
| 15 |
|
| 16 |
# Session memory for last 15 images and captions
|
|
|
|
| 51 |
result_text = f"Detected objects: {tags}\nCaption: {caption}"
|
| 52 |
return result_text, gallery
|
| 53 |
|
| 54 |
+
# Custom CSS for styling header and button
|
| 55 |
+
custom_css = """
|
| 56 |
+
#app-title {
|
| 57 |
+
text-align: center;
|
| 58 |
+
font-size: 36px;
|
| 59 |
+
color: #4DB8FF; /* light blue */
|
| 60 |
+
font-weight: bold;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
#instructions {
|
| 64 |
+
text-align: center;
|
| 65 |
+
font-size: 18px;
|
| 66 |
+
color: #333;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
#generate-btn {
|
| 70 |
+
background: linear-gradient(90deg, #1E90FF, #32CD32); /* lake blue to light green */
|
| 71 |
+
color: white;
|
| 72 |
+
font-weight: bold;
|
| 73 |
+
border: none;
|
| 74 |
+
border-radius: 10px;
|
| 75 |
+
transition: 0.3s ease;
|
| 76 |
+
}
|
| 77 |
+
#generate-btn:hover {
|
| 78 |
+
box-shadow: 0 0 10px rgba(50,205,50,0.4);
|
| 79 |
+
transform: scale(1.05);
|
| 80 |
+
}
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
with gr.Blocks(css=custom_css) as iface:
|
| 84 |
+
# Centered header and readable instructions
|
| 85 |
+
gr.HTML('<h1 id="app-title">πΌοΈ Image Captioning with Object Detection</h1>')
|
| 86 |
+
gr.HTML(
|
| 87 |
+
'<p id="instructions">π Welcome! This app detects objects in your image and generates a descriptive caption.<br>'
|
| 88 |
+
'πͺ <b>How to use:</b><br>'
|
| 89 |
+
'1οΈβ£ Upload an image below<br>'
|
| 90 |
+
'2οΈβ£ Click <b>β Generate Caption</b> to start analysis<br>'
|
| 91 |
+
'3οΈβ£ View caption and detected items instantly below.<br>'
|
| 92 |
+
'π‘ The last <b>15 results</b> will be saved for your review!</p>'
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Image upload and repositioned button directly below
|
| 96 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 97 |
+
generate_btn = gr.Button("β Generate Caption", elem_id="generate-btn")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
# Output: caption + objects
|
| 100 |
+
caption_output = gr.Textbox(label="π Caption and Detected Objects", lines=3, interactive=False)
|
| 101 |
|
| 102 |
+
# History gallery
|
| 103 |
gallery = gr.Gallery(label="Last 15 Images and Captions", scale=3)
|
| 104 |
|
| 105 |
def on_generate(image):
|