Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
+
|
| 4 |
+
# Load the BLIP model and processor from Hugging Face
|
| 5 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 6 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 7 |
+
|
| 8 |
+
def generate_caption(image):
|
| 9 |
+
# Process the image
|
| 10 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 11 |
+
|
| 12 |
+
# Generate caption using BLIP model
|
| 13 |
+
out = model.generate(**inputs)
|
| 14 |
+
|
| 15 |
+
# Decode the output into a string
|
| 16 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 17 |
+
|
| 18 |
+
# Custom description to match the theme of surroundings
|
| 19 |
+
custom_description = """
|
| 20 |
+
A tropical escape where the azure waves meet the golden sand, sheltered by palm trees and embraced by the distant hills.
|
| 21 |
+
A place to unwind, breathe, and reconnect with nature.
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
return caption + "\n" + custom_description
|
| 25 |
+
|
| 26 |
+
# Create the Gradio interface
|
| 27 |
+
iface = gr.Interface(fn=generate_caption,
|
| 28 |
+
inputs=gr.Image(type="pil"),
|
| 29 |
+
outputs=gr.Textbox(),
|
| 30 |
+
title="Image Caption Generator",
|
| 31 |
+
description="Upload an image and get a description with the surroundings of the image.")
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
iface.launch()
|