File size: 1,588 Bytes
6459986
d1dce8a
05169a0
d1dce8a
 
1b087ce
05169a0
 
d1dce8a
 
 
 
 
 
82003f9
05169a0
d1dce8a
 
4eb1216
6459986
4eb1216
6459986
 
 
 
05169a0
 
 
 
6459986
4eb1216
05169a0
 
 
 
 
6459986
 
 
05169a0
6459986
 
4eb1216
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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}")