VL_AI / app_2.py
dtometzki's picture
Update app_2.py
00161b7 verified
Raw History Blame
2.83 kB
import os
import threading
import gradio as gr
from google import genai
from google.genai import types
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
MODEL = "gemini-3.1-flash-lite-preview"
# Tools + Config (einmalig)
tools = [types.Tool(googleSearch=types.GoogleSearch())]
generate_content_config = types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_level="MINIMAL"),
tools=tools,
)
def respond(message, history, state):
if state is None:
state = {
"chat": client.chats.create(model=MODEL, config=generate_content_config),
"lock": threading.Lock(),
}
history = history or []
text = (message or {}).get("text", "")
files = (message or {}).get("files", [])
parts = []
# 1. Dateien verarbeiten (Upload via API & UI-Vorbereitung)
for file_path in files:
# Erstelle einen sicheren Dateinamen (z.B. indem du nur den Basenamen nimmst
# und problematische Zeichen entfernst oder ignorierst)
safe_name = os.path.basename(file_path).encode('ascii', 'ignore').decode('ascii')
# Lade die Datei mit dem sicheren Namen hoch
uploaded_file = client.files.upload(
file=file_path,
config=types.UploadFileConfig(display_name=safe_name)
)
parts.append(uploaded_file)
# Datei in der Gradio UI anzeigen
history.append({"role": "user", "content": gr.FileData(path=file_path)})
# 2. Text verarbeiten
if text:
parts.append(text)
history.append({"role": "user", "content": text})
if not parts:
yield history, state
return
# 3. UI: Assistant Placeholder hinzufügen
history.append({"role": "assistant", "content": ""})
yield history, state
# 4. Gemini: Senden + Streamen
with state["lock"]:
out = ""
for chunk in state["chat"].send_message_stream(parts):
if getattr(chunk, "text", None):
out += chunk.text
history[-1]["content"] = out
yield history, state
def clear_msg():
return {"text": "", "files": []}
with gr.Blocks() as demo:
gr.Markdown("# Gemini AI Chat (Gradio 6.x & File API)")
# Standard-Initialisierung für Gradio >= 6.0
chatbot = gr.Chatbot(height=600)
msg = gr.MultimodalTextbox(
placeholder="Schreib was oder lade Dateien hoch…",
file_count="multiple",
)
state = gr.State(None)
msg.submit(respond, inputs=[msg, chatbot, state], outputs=[chatbot, state]) \
.then(clear_msg, outputs=msg)
gr.Button("Clear").click(
lambda: ([], None, {"text": "", "files": []}),
outputs=[chatbot, state, msg]
)
if __name__ == "__main__":
demo.launch()