Update app_1.py
Browse files
app_1.py
CHANGED
|
@@ -3,24 +3,21 @@ import gradio as gr
|
|
| 3 |
from google import genai
|
| 4 |
from google.genai import types
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
# Der API Key wird aus den Hugging Face Secrets geladen
|
| 8 |
client = genai.Client(
|
| 9 |
api_key=os.environ.get("GEMINI_API_KEY"),
|
| 10 |
)
|
| 11 |
|
| 12 |
def model_chat(message, history):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
message: Die aktuelle Nutzereingabe
|
| 16 |
-
history: Die bisherige Chat-Historie (wird hier zur Vereinfachung ignoriert oder kann eingebunden werden)
|
| 17 |
-
"""
|
| 18 |
-
|
| 19 |
-
# Vorbereiten der Historie für das Modell (optional, aber empfohlen)
|
| 20 |
contents = []
|
| 21 |
for human, assistant in history:
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Aktuelle Nachricht hinzufügen
|
| 26 |
contents.append(
|
|
@@ -30,7 +27,11 @@ def model_chat(message, history):
|
|
| 30 |
)
|
| 31 |
)
|
| 32 |
|
| 33 |
-
# Konfiguration
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
tools = [
|
| 35 |
types.Tool(googleSearch=types.GoogleSearch()),
|
| 36 |
]
|
|
@@ -44,27 +45,33 @@ def model_chat(message, history):
|
|
| 44 |
|
| 45 |
response_text = ""
|
| 46 |
|
| 47 |
-
# Streaming an Gradio übertragen
|
| 48 |
try:
|
|
|
|
| 49 |
for chunk in client.models.generate_content_stream(
|
| 50 |
-
model=
|
| 51 |
contents=contents,
|
| 52 |
config=generate_content_config,
|
| 53 |
):
|
|
|
|
| 54 |
if chunk.text:
|
| 55 |
response_text += chunk.text
|
| 56 |
yield response_text
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
# Gradio Interface
|
| 61 |
demo = gr.ChatInterface(
|
| 62 |
fn=model_chat,
|
| 63 |
-
title="Gemini Thinking
|
| 64 |
-
description="
|
| 65 |
-
examples=["Wie ist das Wetter in Berlin?", "Erkläre die Quantenphysik kurz."],
|
| 66 |
-
#theme="soft"
|
| 67 |
)
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from google import genai
|
| 4 |
from google.genai import types
|
| 5 |
|
| 6 |
+
# Client initialisieren
|
|
|
|
| 7 |
client = genai.Client(
|
| 8 |
api_key=os.environ.get("GEMINI_API_KEY"),
|
| 9 |
)
|
| 10 |
|
| 11 |
def model_chat(message, history):
|
| 12 |
+
# WICHTIG: Die Historie für Gemini konvertieren
|
| 13 |
+
# Gradio history kommt als Liste von Listen: [[user_msg1, bot_msg1], [user_msg2, bot_msg2]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
contents = []
|
| 15 |
for human, assistant in history:
|
| 16 |
+
if human:
|
| 17 |
+
contents.append(types.Content(role="user", parts=[types.Part.from_text(text=human)]))
|
| 18 |
+
if assistant:
|
| 19 |
+
# Wichtig: Nur den Text des Assistenten nehmen, keine Metadaten
|
| 20 |
+
contents.append(types.Content(role="model", parts=[types.Part.from_text(text=assistant)]))
|
| 21 |
|
| 22 |
# Aktuelle Nachricht hinzufügen
|
| 23 |
contents.append(
|
|
|
|
| 27 |
)
|
| 28 |
)
|
| 29 |
|
| 30 |
+
# Konfiguration
|
| 31 |
+
# Hinweis: Falls "gemini-3-flash-preview" einen Fehler wirft,
|
| 32 |
+
# nutze "gemini-2.0-flash-thinking-exp" oder "gemini-2.0-flash"
|
| 33 |
+
model_id = "gemini-3-flash-preview"
|
| 34 |
+
|
| 35 |
tools = [
|
| 36 |
types.Tool(googleSearch=types.GoogleSearch()),
|
| 37 |
]
|
|
|
|
| 45 |
|
| 46 |
response_text = ""
|
| 47 |
|
|
|
|
| 48 |
try:
|
| 49 |
+
# Stream starten
|
| 50 |
for chunk in client.models.generate_content_stream(
|
| 51 |
+
model=model_id,
|
| 52 |
contents=contents,
|
| 53 |
config=generate_content_config,
|
| 54 |
):
|
| 55 |
+
# Sicherstellen, dass Text im Chunk ist
|
| 56 |
if chunk.text:
|
| 57 |
response_text += chunk.text
|
| 58 |
yield response_text
|
| 59 |
+
|
| 60 |
except Exception as e:
|
| 61 |
+
# Detaillierte Fehlermeldung im Chat anzeigen
|
| 62 |
+
error_message = f"❌ API Fehler: {str(e)}"
|
| 63 |
+
print(error_message) # Erscheint in den Hugging Face Logs
|
| 64 |
+
yield error_message
|
| 65 |
|
| 66 |
+
# Gradio Interface
|
| 67 |
demo = gr.ChatInterface(
|
| 68 |
fn=model_chat,
|
| 69 |
+
title="Gemini Thinking AI",
|
| 70 |
+
description="KI mit Suchfunktion und Thinking-Mode.",
|
|
|
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
| 74 |
+
demo.launch()
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|