dtometzki commited on
Commit
f45ffea
·
verified ·
1 Parent(s): 0983b2d

Update app_1.py

Browse files
Files changed (1) hide show
  1. app_1.py +38 -39
app_1.py CHANGED
@@ -9,68 +9,67 @@ client = genai.Client(
9
  )
10
 
11
  def model_chat(message, history):
12
- # WICHTIG: Die Historie für Gemini konvertieren
13
- contents = []
14
-
15
- for human, assistant in history:
16
- if human:
17
- contents.append(types.Content(role="user", parts=[types.Part.from_text(text=human)]))
18
-
19
- # Die API erzwingt zwingend einen 'model'-Eintrag nach einem 'user'-Eintrag!
20
- # Falls assistant leer ist (z. B. durch vorherigen Fehler), füllen wir einen
21
- # Platzhalter ein, um die strikte user->model Abfolge nicht zu zerstören.
22
- ans_text = assistant if assistant else "[Keine Antwort erhalten]"
23
- contents.append(types.Content(role="model", parts=[types.Part.from_text(text=ans_text)]))
24
 
25
- # Aktuelle Nachricht hinzufügen
26
- contents.append(
27
- types.Content(
28
- role="user",
29
- parts=[types.Part.from_text(text=message)],
 
30
  )
31
- )
32
 
33
- # Konfiguration
34
- model_id = "gemini-3.1-flash-lite-preview"
35
-
36
- # KORREKTUR: Das Python SDK nutzt snake_case für die Tool-Zuweisung
37
- tools = [
38
- types.Tool(google_search=types.GoogleSearch()),
39
- ]
40
-
41
- generate_content_config = types.GenerateContentConfig(
42
- thinking_config=types.ThinkingConfig(
43
- thinking_level="HIGH",
44
- ),
45
- tools=tools,
46
- )
47
 
48
- response_text = ""
49
-
50
- try:
51
  # Stream starten
52
  for chunk in client.models.generate_content_stream(
53
  model=model_id,
54
  contents=contents,
55
  config=generate_content_config,
56
  ):
57
- # Sicherstellen, dass Text im Chunk ist
58
  if chunk.text:
59
  response_text += chunk.text
60
  yield response_text
61
 
62
  except Exception as e:
63
- # Detaillierte Fehlermeldung im Chat anzeigen
64
- error_message = f"❌ API Fehler: {str(e)}"
65
  print(error_message)
66
  yield error_message
67
 
68
  # Gradio Interface
69
  demo = gr.ChatInterface(
70
  fn=model_chat,
 
71
  title="Gemini Thinking AI",
72
  description="KI mit Suchfunktion und Thinking-Mode.",
73
  )
74
 
75
  if __name__ == "__main__":
76
- demo.launch(debug=True)
 
9
  )
10
 
11
  def model_chat(message, history):
12
+ # WICHTIG: Den try-Block ganz nach oben ziehen, um Gradio-Popups zu verhindern!
13
+ try:
14
+ # Die Historie für Gemini konvertieren
15
+ contents = []
16
+
17
+ for human, assistant in history:
18
+ if human:
19
+ contents.append(types.Content(role="user", parts=[types.Part.from_text(text=human)]))
20
+
21
+ # Platzhalter, falls assistant leer ist
22
+ ans_text = assistant if assistant else "[Keine Antwort erhalten]"
23
+ contents.append(types.Content(role="model", parts=[types.Part.from_text(text=ans_text)]))
24
 
25
+ # Aktuelle Nachricht hinzufügen
26
+ contents.append(
27
+ types.Content(
28
+ role="user",
29
+ parts=[types.Part.from_text(text=message)],
30
+ )
31
  )
 
32
 
33
+ # Konfiguration
34
+ # HINWEIS: Laut Dokumentation unterstützt dieses Modell keine Google Search Fundierung.
35
+ model_id = "gemini-3.1-flash-lite-preview"
36
+
37
+ tools = [
38
+ types.Tool(google_search=types.GoogleSearch()),
39
+ ]
40
+
41
+ generate_content_config = types.GenerateContentConfig(
42
+ thinking_config=types.ThinkingConfig(
43
+ thinking_level="HIGH",
44
+ ),
45
+ tools=tools,
46
+ )
47
 
48
+ response_text = ""
49
+
 
50
  # Stream starten
51
  for chunk in client.models.generate_content_stream(
52
  model=model_id,
53
  contents=contents,
54
  config=generate_content_config,
55
  ):
 
56
  if chunk.text:
57
  response_text += chunk.text
58
  yield response_text
59
 
60
  except Exception as e:
61
+ # Fehlermeldungen landen jetzt IMMER im Chat und erzeugen kein rotes Popup mehr
62
+ error_message = f"❌ API oder Code Fehler: {str(e)}"
63
  print(error_message)
64
  yield error_message
65
 
66
  # Gradio Interface
67
  demo = gr.ChatInterface(
68
  fn=model_chat,
69
+ type="tuples", # KORREKTUR: Zwingt Gradio 5, das alte "Liste von Listen" Format zu nutzen
70
  title="Gemini Thinking AI",
71
  description="KI mit Suchfunktion und Thinking-Mode.",
72
  )
73
 
74
  if __name__ == "__main__":
75
+ demo.launch()