gemini_test / app.py
Sebbe33's picture
Update app.py
40a87e2 verified
Raw
History Blame Contribute Delete
1.7 kB
import os
import streamlit as st
from PIL import Image
import io
from google import genai
from google.genai import types
# API-Schlüssel laden
#genai.configure(api_key=os.get("KEY"))
st.title("Bildanalyse mit Gemini")
col1, col2 = st.columns(2)
with col1:
uploaded_file = st.file_uploader("Bild hochladen", type=["jpg", "png", "jpeg"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Hochgeladenes Bild", use_container_width=True)
if st.button("Analysieren"):
with st.spinner("Analysiere Bild..."):
try:
# Bild in Bytes umwandeln
image_bytes = io.BytesIO()
image.save(image_bytes, format=image.format)
image_bytes = image_bytes.getvalue()
# Anfrage an Gemini senden
client = genai.Client(api_key=os.getenv("KEY")) # Client innerhalb der Funktion erstellen
response = client.models.generate_content(
model="gemini-2.0-flash-exp", # Oder "gemini-2.0-flash-exp", je nach Verfügbarkeit
contents=["Beschreibe dieses Bild und identifiziere das Hauptobjekt.", types.Part.from_bytes(data=image_bytes, mime_type=f"image/{image.format.lower()}")
]
)
with col2:
# Antwort anzeigen
st.write("## Analyseergebnis:")
st.write(response.text)
except Exception as e:
st.error(f"Ein Fehler ist aufgetreten: {e}")