Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,11 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
-
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
-
import base64
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
genai.configure(api_key=os.getenv('KEY')) # Sicherer!
|
| 11 |
-
|
| 12 |
-
# Gemini Modell erstellen
|
| 13 |
-
generation_config = {
|
| 14 |
-
"temperature": 0.5, # Anpassbar
|
| 15 |
-
"top_p": 0.95, # Anpassbar
|
| 16 |
-
"max_output_tokens": 200, # Anpassbar
|
| 17 |
-
}
|
| 18 |
-
model = genai.GenerativeModel(model_name="gemini-pro", generation_config=generation_config) # Gemini Pro verwenden
|
| 19 |
|
| 20 |
st.title("Bildanalyse mit Gemini")
|
| 21 |
|
|
@@ -23,7 +13,7 @@ uploaded_file = st.file_uploader("Bild hochladen", type=["jpg", "png", "jpeg"])
|
|
| 23 |
|
| 24 |
if uploaded_file is not None:
|
| 25 |
image = Image.open(uploaded_file)
|
| 26 |
-
st.image(image, caption="Hochgeladenes Bild",
|
| 27 |
|
| 28 |
if st.button("Analysieren"):
|
| 29 |
with st.spinner("Analysiere Bild..."):
|
|
@@ -33,18 +23,21 @@ if uploaded_file is not None:
|
|
| 33 |
image.save(image_bytes, format=image.format)
|
| 34 |
image_bytes = image_bytes.getvalue()
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
prompt = f"Beschreibe dieses Bild (Base64-kodiert) und identifiziere das Hauptobjekt:\n\n{encoded_image}"
|
| 41 |
|
| 42 |
# Anfrage an Gemini senden
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
# Antwort anzeigen
|
| 46 |
st.write("## Analyseergebnis:")
|
| 47 |
-
st.write(response.
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
st.error(f"Ein Fehler ist aufgetreten: {e}")
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
from google import genai
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
|
|
|
| 6 |
|
| 7 |
+
# API-Schlüssel laden
|
| 8 |
+
genai.configure(api_key=os.get("KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
st.title("Bildanalyse mit Gemini")
|
| 11 |
|
|
|
|
| 13 |
|
| 14 |
if uploaded_file is not None:
|
| 15 |
image = Image.open(uploaded_file)
|
| 16 |
+
st.image(image, caption="Hochgeladenes Bild", use_column_width=True)
|
| 17 |
|
| 18 |
if st.button("Analysieren"):
|
| 19 |
with st.spinner("Analysiere Bild..."):
|
|
|
|
| 23 |
image.save(image_bytes, format=image.format)
|
| 24 |
image_bytes = image_bytes.getvalue()
|
| 25 |
|
| 26 |
+
# Dateiähnliches Objekt für Gemini erstellen
|
| 27 |
+
file_like_object = genai.File(
|
| 28 |
+
content=image_bytes, mime_type=f"image/{image.format.lower()}"
|
| 29 |
+
)
|
|
|
|
| 30 |
|
| 31 |
# Anfrage an Gemini senden
|
| 32 |
+
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY")) # Client innerhalb der Funktion erstellen
|
| 33 |
+
response = client.models.generate_content(
|
| 34 |
+
model="gemini-pro", # Oder "gemini-2.0-flash-exp", je nach Verfügbarkeit
|
| 35 |
+
contents=["Beschreibe dieses Bild und identifiziere das Hauptobjekt.", file_like_object]
|
| 36 |
+
)
|
| 37 |
|
| 38 |
# Antwort anzeigen
|
| 39 |
st.write("## Analyseergebnis:")
|
| 40 |
+
st.write(response.text)
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
st.error(f"Ein Fehler ist aufgetreten: {e}")
|