Update app_1.py
Browse files
app_1.py
CHANGED
|
@@ -9,68 +9,67 @@ client = genai.Client(
|
|
| 9 |
)
|
| 10 |
|
| 11 |
def model_chat(message, history):
|
| 12 |
-
# WICHTIG:
|
| 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 |
-
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 |
-
#
|
| 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(
|
|
|
|
| 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()
|