Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +48 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 4 |
+
import torch
|
| 5 |
+
import base64
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
# Modell vorbereiten
|
| 9 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 11 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
|
| 12 |
+
|
| 13 |
+
# Hilfsfunktion: Bild in base64 für HTML-Thumbnail umwandeln
|
| 14 |
+
def image_to_base64_html(img):
|
| 15 |
+
buffered = BytesIO()
|
| 16 |
+
img.save(buffered, format="PNG")
|
| 17 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 18 |
+
html = f'<img src="data:image/png;base64,{img_str}" width="150"/>'
|
| 19 |
+
return html
|
| 20 |
+
|
| 21 |
+
# Hauptfunktion: Bild → Beschreibung → beides in Chat
|
| 22 |
+
def describe_image_with_thumbnail(image, history):
|
| 23 |
+
if image is None:
|
| 24 |
+
return history
|
| 25 |
+
|
| 26 |
+
# Beschreibung generieren
|
| 27 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 28 |
+
output = model.generate(**inputs)
|
| 29 |
+
caption = processor.decode(output[0], skip_special_tokens=True)
|
| 30 |
+
|
| 31 |
+
# Bild in HTML konvertieren
|
| 32 |
+
image_html = image_to_base64_html(image)
|
| 33 |
+
|
| 34 |
+
# Chatverlauf aktualisieren (Bild + Beschreibung)
|
| 35 |
+
history.append((image_html, caption))
|
| 36 |
+
return history
|
| 37 |
+
|
| 38 |
+
# UI bauen
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown("## 🤖 Bildbeschreibung-Chatbot mit Thumbnail-Vorschau")
|
| 41 |
+
chatbot = gr.Chatbot(label="Bilder-Chat")
|
| 42 |
+
with gr.Row():
|
| 43 |
+
image_input = gr.Image(type="pil", label="Bild hier hochladen")
|
| 44 |
+
send_btn = gr.Button("Bild analysieren")
|
| 45 |
+
|
| 46 |
+
send_btn.click(fn=describe_image_with_thumbnail, inputs=[image_input, chatbot], outputs=chatbot)
|
| 47 |
+
|
| 48 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
gradio
|
| 4 |
+
Pillow
|