Spaces:
Sleeping
Sleeping
import os | |
import streamlit as st | |
from google import genai | |
from PIL import Image | |
import io | |
# API-Schlüssel laden | |
genai.configure(api_key=os.get("KEY")) | |
st.title("Bildanalyse mit Gemini") | |
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_column_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() | |
# Dateiähnliches Objekt für Gemini erstellen | |
file_like_object = genai.File( | |
content=image_bytes, mime_type=f"image/{image.format.lower()}" | |
) | |
# Anfrage an Gemini senden | |
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY")) # Client innerhalb der Funktion erstellen | |
response = client.models.generate_content( | |
model="gemini-pro", # Oder "gemini-2.0-flash-exp", je nach Verfügbarkeit | |
contents=["Beschreibe dieses Bild und identifiziere das Hauptobjekt.", file_like_object] | |
) | |
# Antwort anzeigen | |
st.write("## Analyseergebnis:") | |
st.write(response.text) | |
except Exception as e: | |
st.error(f"Ein Fehler ist aufgetreten: {e}") |