File size: 1,605 Bytes
6459986
d1dce8a
 
 
1b087ce
d4df12a
1d8d466
5f554b3
 
05169a0
062f2cb
 
d1dce8a
 
02d80b6
 
 
d1dce8a
 
 
 
82003f9
1d8d466
d1dce8a
 
4eb1216
6459986
4eb1216
6459986
 
 
 
 
4eb1216
eb71174
05169a0
89eeedc
813d2f8
1d8d466
05169a0
6459986
02d80b6
6459986
 
02d80b6
 
 
 
 
 
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
44
45
46
47
48
49
50
51
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()}")
                    ]
                )

              

            except Exception as e:
                st.error(f"Ein Fehler ist aufgetreten: {e}")
with col2:
    if uploaded_file is not None and response not None:
          # Antwort anzeigen
            st.write("## Analyseergebnis:")
            st.write(response.text)