Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,62 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from PIL import Image
|
| 3 |
-
from transformers import (
|
| 4 |
-
BlipProcessor, BlipForConditionalGeneration,
|
| 5 |
-
MarianTokenizer, MarianMTModel
|
| 6 |
-
)
|
| 7 |
-
import torch
|
| 8 |
-
import base64
|
| 9 |
-
from io import BytesIO
|
| 10 |
-
|
| 11 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
-
|
| 13 |
-
# Bildbeschreibung (BLIP)
|
| 14 |
-
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 15 |
-
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
|
| 16 |
-
|
| 17 |
-
# Übersetzung EN → DE (Helsinki)
|
| 18 |
-
translation_model_name = "Helsinki-NLP/opus-mt-en-de"
|
| 19 |
-
translator_tokenizer = MarianTokenizer.from_pretrained(translation_model_name)
|
| 20 |
-
translator_model = MarianMTModel.from_pretrained(translation_model_name).to(device)
|
| 21 |
-
|
| 22 |
-
# Hilfsfunktion: Bild als HTML Thumbnail
|
| 23 |
-
def image_to_base64_html(img):
|
| 24 |
-
buffered = BytesIO()
|
| 25 |
-
img.save(buffered, format="PNG")
|
| 26 |
-
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 27 |
-
html = f'<img src="data:image/png;base64,{img_str}" width="150"/>'
|
| 28 |
-
return html
|
| 29 |
-
|
| 30 |
-
# Bildbeschreibung + Übersetzung
|
| 31 |
-
def describe_image_with_translation(image, history):
|
| 32 |
-
if image is None:
|
| 33 |
-
return history
|
| 34 |
-
|
| 35 |
-
# EN: Bildbeschreibung
|
| 36 |
-
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 37 |
-
output = model.generate(**inputs)
|
| 38 |
-
caption_en = processor.decode(output[0], skip_special_tokens=True)
|
| 39 |
-
|
| 40 |
-
# Übersetzen nach DE
|
| 41 |
-
tokens = translator_tokenizer(caption_en, return_tensors="pt", padding=True).to(device)
|
| 42 |
-
translated_ids = translator_model.generate(**tokens)
|
| 43 |
-
caption_de = translator_tokenizer.decode(translated_ids[0], skip_special_tokens=True)
|
| 44 |
-
|
| 45 |
-
# Bild als Thumbnail
|
| 46 |
-
image_html = image_to_base64_html(image)
|
| 47 |
-
|
| 48 |
-
# Chatverlauf aktualisieren
|
| 49 |
-
history.append((image_html, caption_de))
|
| 50 |
-
return history
|
| 51 |
-
|
| 52 |
-
# Gradio UI
|
| 53 |
-
with gr.Blocks() as demo:
|
| 54 |
-
gr.Markdown("## 🇩🇪 KI-Chat: Automatische Bildbeschreibung auf Deutsch")
|
| 55 |
-
chatbot = gr.Chatbot(label="Bild-zu-Text (DE)")
|
| 56 |
-
with gr.Row():
|
| 57 |
-
image_input = gr.Image(type="pil", label="Bild hier hochladen")
|
| 58 |
-
btn = gr.Button("Bild beschreiben")
|
| 59 |
-
|
| 60 |
-
btn.click(fn=describe_image_with_translation, inputs=[image_input, chatbot], outputs=chatbot)
|
| 61 |
-
|
| 62 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|