-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
41 lines (35 loc) · 1.28 KB
/
app.py
File metadata and controls
41 lines (35 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import gradio as gr
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
def generate_caption(image):
if image.mode != "RGB":
image = image.convert("RGB")
inputs = processor(images=image, return_tensors="pt")
outputs = model.generate(
**inputs,
max_new_tokens=30,
num_beams=5,
repetition_penalty=1.2,
no_repeat_ngram_size=2,
early_stopping=True
)
return processor.decode(outputs[0], skip_special_tokens=True)
def caption_image(image):
try:
return generate_caption(image)
except Exception as e:
return f"Error: {str(e)}"
interface = gr.Interface(
fn=caption_image,
inputs=gr.Image(type="pil"),
outputs="text",
title="Image Captioning with BLIP",
description="Upload an image to generate a caption"
)
# if __name__ == "__main__":
# interface.launch(server_name="127.0.0.1", server_port=7070)
# деплою на Hugging Face Spaces, передача параметра `server_name="0.0.0.0"`
if __name__ == "__main__":
interface.launch(server_name="0.0.0.0")