Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from google.generativeai import text_model
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# API-Schlüssel laden
|
| 9 |
+
api_key = os.getenv('KEY')
|
| 10 |
+
|
| 11 |
+
# Gemini API-Modell initialisieren
|
| 12 |
+
model = text_model.TextModel.from_pretrained("gemini-2.0-flash")
|
| 13 |
+
|
| 14 |
+
st.title("Bildanalyse mit Gemini")
|
| 15 |
+
|
| 16 |
+
uploaded_file = st.file_uploader("Bild hochladen", type=["jpg", "png", "jpeg"])
|
| 17 |
+
|
| 18 |
+
if uploaded_file is not None:
|
| 19 |
+
image = Image.open(uploaded_file)
|
| 20 |
+
st.image(image, caption="Hochgeladenes Bild", use_column_width=True)
|
| 21 |
+
|
| 22 |
+
if st.button("Analysieren"):
|
| 23 |
+
# Bild in Bytes umwandeln
|
| 24 |
+
image_bytes = io.BytesIO()
|
| 25 |
+
image.save(image_bytes, format=image.format)
|
| 26 |
+
image_bytes = image_bytes.getvalue()
|
| 27 |
+
|
| 28 |
+
# Gemini API-Anfrage erstellen
|
| 29 |
+
prompt = "Was ist das Hauptobjekt in diesem Bild?"
|
| 30 |
+
response = model.generate_text(
|
| 31 |
+
model=model,
|
| 32 |
+
prompt=prompt,
|
| 33 |
+
image=image_bytes,
|
| 34 |
+
temperature=0.5,
|
| 35 |
+
max_output_tokens=100
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Antwort anzeigen
|
| 39 |
+
st.write("## Analyseergebnis:")
|
| 40 |
+
st.write(response.result)
|