Sebbe33 commited on
Commit
6459986
·
verified ·
1 Parent(s): 36486b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -25
app.py CHANGED
@@ -1,15 +1,19 @@
 
1
  import streamlit as st
2
- from google.generativeai import text_model
3
  from PIL import Image
4
  import io
5
- import os
6
-
7
 
8
- # API-Schlüssel laden
9
- api_key = os.getenv('KEY')
10
 
11
- # Gemini API-Modell initialisieren
12
- model = text_model.TextModel.from_pretrained("gemini-2.0-flash")
 
 
 
 
 
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
- # Bild in Bytes umwandeln
24
- image_bytes = io.BytesIO()
25
- image.save(image_bytes, format=image.format)
26
- image_bytes = image_bytes.getvalue()
27
-
28
- # Gemini API-Anfrage erstellen
29
- prompt = "Was ist das Hauptobjekt in diesem Bild?"
30
- response = model.generate_text(
31
- model=model,
32
- prompt=prompt,
33
- image=image_bytes,
34
- temperature=0.5,
35
- max_output_tokens=100
36
- )
37
-
38
- # Antwort anzeigen
39
- st.write("## Analyseergebnis:")
40
- st.write(response.result)
 
 
 
 
 
 
 
 
 
 
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