LejobuildYT's picture
Update app.py
306f10c verified
import gradio as gr
import requests
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
import time
# Lade den Blip-Prozessor und das Modell
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
def caption(img, min_len, max_len):
# Γ–ffne das Bild
raw_image = Image.open(img).convert('RGB')
# Verarbeite das Bild mit dem Blip-Prozessor
inputs = processor(raw_image, return_tensors="pt")
# Generiere eine Beschreibung mit dem Modell
out = model.generate(**inputs, min_length=min_len, max_length=max_len)
return processor.decode(out[0], skip_special_tokens=True)
def greet(img, min_len, max_len):
start = time.time()
result = caption(img, min_len, max_len)
end = time.time()
total_time = str(end - start)
result = result + '\n' + total_time + ' seconds'
return result
# Gradio Interface erstellen
iface = gr.Interface(
fn=greet,
title='Blip Image Captioning Large',
description="[Salesforce/blip-image-captioning-base](https://huggingface.co/Salesforce/blip-image-captioning-base) Runs on CPU",
inputs=[gr.Image(type='filepath', label='Image'),
gr.Slider(label='Minimum Length', minimum=1, maximum=1000, value=30),
gr.Slider(label='Maximum Length', minimum=1, maximum=1000, value=100)],
outputs=gr.Textbox(label='Caption'),
theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate"),
)
# API aktivieren, um sie von außen zu nutzen
iface.launch(share=True, allow_api=True) # API aktivieren