Download app_2.py from dtometzki/VL_AI: direct link, hf CLI and curl.
- Browser
- Download file 2.83 kB
-
https://huggingface.co/spaces/dtometzki/VL_AI/resolve/00161b717bc8d2b67a34cea66521348139a7157d/app_2.py
- Command line
-
hf download hf://spaces/dtometzki/VL_AI@00161b717bc8d2b67a34cea66521348139a7157d/app_2.py
-
curl -L -o app_2.py https://huggingface.co/spaces/dtometzki/VL_AI/resolve/00161b717bc8d2b67a34cea66521348139a7157d/app_2.py
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() |