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