Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
|
| 8 |
-
# API-Schlüssel laden
|
| 9 |
-
api_key
|
| 10 |
|
| 11 |
-
# Gemini
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
st.title("Bildanalyse mit Gemini")
|
| 15 |
|
|
@@ -20,21 +24,30 @@ if uploaded_file is not None:
|
|
| 20 |
st.image(image, caption="Hochgeladenes Bild", use_column_width=True)
|
| 21 |
|
| 22 |
if st.button("Analysieren"):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# API-Schlüssel laden (aus Umgebungsvariable)
|
| 8 |
+
genai.configure(api_key=os.getenv('KEY')) # Sicherer!
|
| 9 |
|
| 10 |
+
# Gemini Modell erstellen
|
| 11 |
+
generation_config = {
|
| 12 |
+
"temperature": 0.5, # Anpassbar
|
| 13 |
+
"top_p": 0.95, # Anpassbar
|
| 14 |
+
"max_output_tokens": 200, # Anpassbar
|
| 15 |
+
}
|
| 16 |
+
model = genai.GenerativeModel(model_name="gemini-pro", generation_config=generation_config) # Gemini Pro verwenden
|
| 17 |
|
| 18 |
st.title("Bildanalyse mit Gemini")
|
| 19 |
|
|
|
|
| 24 |
st.image(image, caption="Hochgeladenes Bild", use_column_width=True)
|
| 25 |
|
| 26 |
if st.button("Analysieren"):
|
| 27 |
+
with st.spinner("Analysiere Bild..."): # Spinner für Benutzerfreundlichkeit
|
| 28 |
+
try:
|
| 29 |
+
# Bild in Bytes umwandeln und hochladen (wie im Beispielcode)
|
| 30 |
+
image_bytes = io.BytesIO()
|
| 31 |
+
image.save(image_bytes, format=image.format)
|
| 32 |
+
image_bytes = image_bytes.getvalue()
|
| 33 |
+
|
| 34 |
+
# Dateiähnliches Objekt für Gemini erstellen
|
| 35 |
+
file_like_object = genai.File(
|
| 36 |
+
content=image_bytes, mime_type=f"image/{image.format.lower()}" # MIME-Type dynamisch
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Prompt erstellen (angepasst)
|
| 40 |
+
prompt = "Beschreibe dieses Bild und identifiziere das Hauptobjekt."
|
| 41 |
+
|
| 42 |
+
# Anfrage an Gemini senden (mit Bild)
|
| 43 |
+
response = model.generate_text(
|
| 44 |
+
prompt=prompt,
|
| 45 |
+
image=file_like_object, # Bild direkt übergeben
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Antwort anzeigen
|
| 49 |
+
st.write("## Analyseergebnis:")
|
| 50 |
+
st.write(response.result)
|
| 51 |
+
|
| 52 |
+
except Exception as e:
|
| 53 |
+
st.error(f"Ein Fehler ist aufgetreten: {e}") # Fehlerbehandlung
|