Spaces:
Runtime error
Runtime error
Commit
·
bac3301
1
Parent(s):
524f1cf
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,25 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
7 |
|
8 |
-
def
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
|
|
|
|
16 |
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
3 |
|
4 |
+
# Load the model and tokenizer
|
5 |
+
model_name = "Salesforce/blip-image-captioning-large"
|
6 |
+
processor = BlipProcessor.from_pretrained(model_name)
|
7 |
+
model = BlipForConditionalGeneration.from_pretrained(model_name)
|
8 |
|
9 |
+
def generate_caption(image):
|
10 |
+
# Preprocess the image
|
11 |
+
inputs = processor(images=image, return_tensors="pt")
|
12 |
|
13 |
+
# Generate caption using the model
|
14 |
+
caption = model.generate(**inputs)
|
15 |
+
|
16 |
+
# Decode the output caption
|
17 |
+
decoded_caption = processor.decode(caption[0], skip_special_tokens=True)
|
18 |
+
return decoded_caption
|
19 |
|
20 |
+
# Define the Gradio interface
|
21 |
+
inputs = gr.inputs.Image(label="Upload an image")
|
22 |
+
outputs = gr.outputs.Textbox(label="Generated Caption")
|
23 |
|
24 |
+
# Create the Gradio app
|
25 |
+
gr.Interface(fn=generate_caption, inputs=inputs, outputs=outputs).launch()
|