Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
2 |
+
from gradio import Interface
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
# Load the BLIP-2 model and processor
|
6 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip2-flan-t5-xl")
|
7 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xl")
|
8 |
+
|
9 |
+
def generate_response(image, prompt):
|
10 |
+
"""Generate a response from the model based on the image and prompt."""
|
11 |
+
inputs = processor(image, prompt, return_tensors="pt")
|
12 |
+
outputs = model.generate(**inputs)
|
13 |
+
return processor.decode(outputs[0], skip_special_tokens=True)
|
14 |
+
|
15 |
+
# Create a Gradio interface
|
16 |
+
def predict(image, prompt):
|
17 |
+
return generate_response(image, prompt)
|
18 |
+
|
19 |
+
interface = Interface(
|
20 |
+
fn=predict,
|
21 |
+
inputs=["image", "text"],
|
22 |
+
outputs="text",
|
23 |
+
title="BLIP-2: Introspective Monologue Generator",
|
24 |
+
description="Upload an image and provide a prompt. The model will respond with introspective thoughts about the image."
|
25 |
+
)
|
26 |
+
|
27 |
+
if __name__ == "__main__":
|
28 |
+
interface.launch()
|