File size: 4,224 Bytes
99f3f86
 
3ffbe14
a9e9298
 
99f3f86
5961683
a9e9298
 
 
8a5469e
3ffbe14
 
a9e9298
f45ffea
 
95bd235
f45ffea
5809146
95bd235
 
 
 
 
 
 
 
5809146
95bd235
 
 
 
 
 
 
 
 
2a7df20
95bd235
 
 
3ffbe14
5809146
 
3ffbe14
5809146
 
 
3ffbe14
5809146
3ffbe14
 
 
 
 
5809146
3ffbe14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a5469e
5809146
3ffbe14
 
5809146
 
 
3ffbe14
f45ffea
 
5809146
f45ffea
 
5809146
f45ffea
 
a9edb04
3ffbe14
f45ffea
a9e9298
5961683
a9e9298
 
 
 
 
 
5809146
a9edb04
5809146
a9edb04
3ffbe14
8a5469e
a9e9298
5961683
3ffbe14
5809146
8a5469e
 
 
f45ffea
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import os
import gradio as gr
import mimetypes
from google import genai
from google.genai import types

# Client initialisieren
client = genai.Client(
    api_key=os.environ.get("GEMINI_API_KEY"),
)

MAX_FILE_SIZE_MB = 2

def model_chat(message, history):
    try:
        contents = []
        last_role = None
        
        # 1. Historie verarbeiten
        for msg in history:
            if isinstance(msg, dict):
                role = msg.get("role")
                content = msg.get("content", "")
            else:
                role = getattr(msg, "role", "")
                content = getattr(msg, "content", "")
            
            text_val = content if isinstance(content, str) else "[Datei]"
            
            if role == "user":
                if last_role == "user":
                    contents.append(types.Content(role="model", parts=[types.Part.from_text(text="[Keine Antwort erhalten]")]))
                contents.append(types.Content(role="user", parts=[types.Part.from_text(text=text_val)]))
                last_role = "user"
            elif role in ["assistant", "model"]:
                contents.append(types.Content(role="model", parts=[types.Part.from_text(text=text_val)]))
                last_role = "model"

        if last_role == "user":
            contents.append(types.Content(role="model", parts=[types.Part.from_text(text="[Keine Antwort erhalten]")]))

        # 2. Aktuelle Nachricht & Datei-Upload (Universell mit 2MB Limit)
        current_parts = []
        
        # Text hinzufügen
        if message["text"]:
            current_parts.append(types.Part.from_text(text=message["text"]))
        
        # Dateien verarbeiten
        for file_path in message["files"]:
            file_size = os.path.getsize(file_path) / (1024 * 1024) # In MB
            
            if file_size > MAX_FILE_SIZE_MB:
                yield f"⚠️ Datei '{os.path.basename(file_path)}' überspringt das 2 MB Limit ({file_size:.2f} MB)."
                continue

            mime_type, _ = mimetypes.guess_type(file_path)
            mime_type = mime_type or "application/octet-stream"

            # Unterscheidung: Text vs. Binär (Bild, PDF, etc.)
            if mime_type.startswith("text/"):
                try:
                    with open(file_path, "r", encoding="utf-8", errors="replace") as f:
                        content_str = f.read()
                        current_parts.append(types.Part.from_text(text=f"Dateiinhalt ({os.path.basename(file_path)}):\n\n{content_str}"))
                except Exception:
                    # Fallback auf Bytes, falls Text-Lesen scheitert
                    with open(file_path, "rb") as f:
                        current_parts.append(types.Part.from_bytes(data=f.read(), mime_type=mime_type))
            else:
                with open(file_path, "rb") as f:
                    current_parts.append(types.Part.from_bytes(data=f.read(), mime_type=mime_type))

        if not current_parts:
            yield "Bitte gib eine Nachricht ein oder lade eine passende Datei hoch."
            return

        contents.append(types.Content(role="user", parts=current_parts))

        # 3. Konfiguration (Unverändert: gemini-3.1-flash-lite-preview)
        model_id = "gemini-3.1-flash-lite-preview" 
        
        tools = [types.Tool(googleSearch=types.GoogleSearch())]
        
        generate_content_config = types.GenerateContentConfig(
            thinking_config=types.ThinkingConfig(thinking_level="MINIMAL"),
            tools=tools,
        )

        # 4. Stream starten
        response_text = ""
        for chunk in client.models.generate_content_stream(
            model=model_id,
            contents=contents,
            config=generate_content_config,
        ):
            if chunk.text:
                response_text += chunk.text
                yield response_text
                    
    except Exception as e:
        yield f"❌ Fehler: {str(e)}"

# Gradio Interface
demo = gr.ChatInterface(
    fn=model_chat,
    title="Gemini Thinking AI",
    description="KI mit Suche und universellem Datei-Upload (max. 2 MB).",
    multimodal=True,
)

if __name__ == "__main__":
    demo.launch()