Update app_2.py
Browse files
app_2.py
CHANGED
|
@@ -1,40 +1,52 @@
|
|
| 1 |
-
import os,
|
|
|
|
| 2 |
from google import genai
|
| 3 |
from google.genai import types
|
| 4 |
|
| 5 |
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
| 6 |
MODEL = "gemini-3.1-flash-lite-preview"
|
| 7 |
|
| 8 |
-
def
|
|
|
|
| 9 |
if state is None:
|
| 10 |
state = {"chat": client.chats.create(model=MODEL), "lock": threading.Lock()}
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
parts = []
|
| 13 |
-
if
|
| 14 |
-
parts.append(types.Part.from_text(text=
|
| 15 |
|
| 16 |
-
for file_path in
|
| 17 |
mime, _ = mimetypes.guess_type(file_path)
|
| 18 |
mime = mime or "application/octet-stream"
|
| 19 |
with open(file_path, "rb") as f:
|
| 20 |
parts.append(types.Part.from_bytes(data=f.read(), mime_type=mime))
|
| 21 |
|
| 22 |
if not parts:
|
| 23 |
-
|
|
|
|
| 24 |
return
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
with state["lock"]:
|
| 27 |
out = ""
|
| 28 |
for chunk in state["chat"].send_message_stream(parts):
|
| 29 |
-
if chunk
|
| 30 |
out += chunk.text
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
)
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
demo.launch()
|
|
|
|
| 1 |
+
import os, mimetypes, threading
|
| 2 |
+
import gradio as gr
|
| 3 |
from google import genai
|
| 4 |
from google.genai import types
|
| 5 |
|
| 6 |
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
| 7 |
MODEL = "gemini-3.1-flash-lite-preview"
|
| 8 |
|
| 9 |
+
def respond(message, history, state):
|
| 10 |
+
# state bleibt server-side (darf Python-Objekte enthalten)
|
| 11 |
if state is None:
|
| 12 |
state = {"chat": client.chats.create(model=MODEL), "lock": threading.Lock()}
|
| 13 |
|
| 14 |
+
text = (message or {}).get("text") or ""
|
| 15 |
+
files = (message or {}).get("files") or []
|
| 16 |
+
|
| 17 |
parts = []
|
| 18 |
+
if text:
|
| 19 |
+
parts.append(types.Part.from_text(text=text))
|
| 20 |
|
| 21 |
+
for file_path in files:
|
| 22 |
mime, _ = mimetypes.guess_type(file_path)
|
| 23 |
mime = mime or "application/octet-stream"
|
| 24 |
with open(file_path, "rb") as f:
|
| 25 |
parts.append(types.Part.from_bytes(data=f.read(), mime_type=mime))
|
| 26 |
|
| 27 |
if not parts:
|
| 28 |
+
# nichts gesendet -> keine Änderung
|
| 29 |
+
yield history, state
|
| 30 |
return
|
| 31 |
|
| 32 |
+
# User Nachricht in UI-Historie
|
| 33 |
+
history = (history or []) + [[text if text else "[Datei]", ""]]
|
| 34 |
+
|
| 35 |
with state["lock"]:
|
| 36 |
out = ""
|
| 37 |
for chunk in state["chat"].send_message_stream(parts):
|
| 38 |
+
if getattr(chunk, "text", None):
|
| 39 |
out += chunk.text
|
| 40 |
+
history[-1][1] = out
|
| 41 |
+
yield history, state
|
| 42 |
+
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("# Gemini Thinking AI")
|
| 45 |
+
chatbot = gr.Chatbot(height=600)
|
| 46 |
+
msg = gr.MultimodalTextbox(placeholder="Schreib was oder lade Dateien hoch…", file_count="multiple")
|
| 47 |
+
state = gr.State(None)
|
| 48 |
+
|
| 49 |
+
msg.submit(respond, inputs=[msg, chatbot, state], outputs=[chatbot, state])
|
| 50 |
+
gr.Button("Clear").click(lambda: ([], None), outputs=[chatbot, state])
|
| 51 |
|
| 52 |
demo.launch()
|