VL_AI / app_1.py
dtometzki's picture
Update app_1.py
5961683 verified
Raw History Blame
2.23 kB
import os
import gradio as gr
from google import genai
from google.genai import types
# Client initialisieren
client = genai.Client(
api_key=os.environ.get("GEMINI_API_KEY"),
)
def model_chat(message, history):
# WICHTIG: Die Historie für Gemini konvertieren
# Gradio history kommt als Liste von Listen: [[user_msg1, bot_msg1], [user_msg2, bot_msg2]]
contents = []
for human, assistant in history:
if human:
contents.append(types.Content(role="user", parts=[types.Part.from_text(text=human)]))
if assistant:
# Wichtig: Nur den Text des Assistenten nehmen, keine Metadaten
contents.append(types.Content(role="model", parts=[types.Part.from_text(text=assistant)]))
# Aktuelle Nachricht hinzufügen
contents.append(
types.Content(
role="user",
parts=[types.Part.from_text(text=message)],
)
)
# Konfiguration
# Hinweis: Falls "gemini-3-flash-preview" einen Fehler wirft,
# nutze "gemini-2.0-flash-thinking-exp" oder "gemini-2.0-flash"
model_id = "gemini-3-flash-preview"
tools = [
types.Tool(googleSearch=types.GoogleSearch()),
]
generate_content_config = types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
thinking_level="HIGH",
),
tools=tools,
)
response_text = ""
try:
# Stream starten
for chunk in client.models.generate_content_stream(
model=model_id,
contents=contents,
config=generate_content_config,
):
# Sicherstellen, dass Text im Chunk ist
if chunk.text:
response_text += chunk.text
yield response_text
except Exception as e:
# Detaillierte Fehlermeldung im Chat anzeigen
error_message = f"❌ API Fehler: {str(e)}"
print(error_message) # Erscheint in den Hugging Face Logs
yield error_message
# Gradio Interface
demo = gr.ChatInterface(
fn=model_chat,
title="Gemini Thinking AI",
description="KI mit Suchfunktion und Thinking-Mode.",
)
if __name__ == "__main__":
demo.launch()