File size: 2,226 Bytes
99f3f86
 
a9e9298
 
99f3f86
5961683
a9e9298
 
 
8a5469e
a9e9298
5961683
 
a9e9298
 
5961683
 
 
 
 
a9e9298
 
 
 
 
 
8a5469e
a9e9298
8a5469e
5961683
 
 
 
 
a9e9298
 
 
 
 
 
 
 
 
 
a9edb04
a9e9298
 
 
5961683
a9e9298
5961683
a9e9298
 
 
5961683
a9e9298
 
 
5961683
a9edb04
5961683
 
 
 
a9edb04
5961683
8a5469e
a9e9298
5961683
 
8a5469e
 
 
5961683
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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()