Create app_2.py
Browse files
app_2.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os, gradio as gr, mimetypes, threading
|
| 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 model_chat(message, state):
|
| 9 |
+
if state is None:
|
| 10 |
+
state = {"chat": client.chats.create(model=MODEL), "lock": threading.Lock()}
|
| 11 |
+
|
| 12 |
+
parts = []
|
| 13 |
+
if message.get("text"):
|
| 14 |
+
parts.append(types.Part.from_text(text=message["text"]))
|
| 15 |
+
|
| 16 |
+
for file_path in message.get("files") or []:
|
| 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 |
+
yield "Bitte Text oder Datei senden.", state
|
| 24 |
+
return
|
| 25 |
+
|
| 26 |
+
with state["lock"]:
|
| 27 |
+
out = ""
|
| 28 |
+
for chunk in state["chat"].send_message_stream(parts):
|
| 29 |
+
if chunk.text:
|
| 30 |
+
out += chunk.text
|
| 31 |
+
yield out, state
|
| 32 |
+
|
| 33 |
+
demo = gr.ChatInterface(
|
| 34 |
+
fn=model_chat,
|
| 35 |
+
multimodal=True,
|
| 36 |
+
additional_inputs=[gr.State(None)],
|
| 37 |
+
additional_outputs=[gr.State(None)],
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
demo.launch()
|