Update app_2.py
Browse files
app_2.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
-
import os
|
|
|
|
| 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
|
| 7 |
MODEL = "gemini-3.1-flash-lite-preview"
|
| 8 |
|
| 9 |
# Tools + Config (einmalig)
|
|
@@ -14,39 +15,39 @@ generate_content_config = types.GenerateContentConfig(
|
|
| 14 |
)
|
| 15 |
|
| 16 |
def respond(message, history, state):
|
| 17 |
-
# history: [{"role": "user"/"assistant", "content": "..."} , ...]
|
| 18 |
if state is None:
|
| 19 |
state = {
|
| 20 |
"chat": client.chats.create(model=MODEL, config=generate_content_config),
|
| 21 |
"lock": threading.Lock(),
|
| 22 |
}
|
| 23 |
-
|
| 24 |
history = history or []
|
| 25 |
-
text = (message or {}).get("text"
|
| 26 |
-
files = (message or {}).get("files"
|
| 27 |
-
|
| 28 |
parts = []
|
| 29 |
-
if text:
|
| 30 |
-
parts.append(types.Part.from_text(text=text))
|
| 31 |
|
|
|
|
| 32 |
for file_path in files:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
if not parts:
|
| 39 |
yield history, state
|
| 40 |
return
|
| 41 |
|
| 42 |
-
# UI:
|
| 43 |
-
user_label = text if text else "[Datei]"
|
| 44 |
-
history.append({"role": "user", "content": user_label})
|
| 45 |
-
# UI: assistant placeholder
|
| 46 |
history.append({"role": "assistant", "content": ""})
|
| 47 |
yield history, state
|
| 48 |
|
| 49 |
-
# Gemini:
|
| 50 |
with state["lock"]:
|
| 51 |
out = ""
|
| 52 |
for chunk in state["chat"].send_message_stream(parts):
|
|
@@ -59,18 +60,24 @@ def clear_msg():
|
|
| 59 |
return {"text": "", "files": []}
|
| 60 |
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
-
gr.Markdown("# Gemini
|
|
|
|
|
|
|
| 63 |
chatbot = gr.Chatbot(height=600)
|
|
|
|
| 64 |
msg = gr.MultimodalTextbox(
|
| 65 |
placeholder="Schreib was oder lade Dateien hoch…",
|
| 66 |
file_count="multiple",
|
| 67 |
)
|
| 68 |
state = gr.State(None)
|
| 69 |
-
|
| 70 |
msg.submit(respond, inputs=[msg, chatbot, state], outputs=[chatbot, state]) \
|
| 71 |
.then(clear_msg, outputs=msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import threading
|
| 3 |
import gradio as gr
|
| 4 |
from google import genai
|
| 5 |
from google.genai import types
|
| 6 |
|
| 7 |
+
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 8 |
MODEL = "gemini-3.1-flash-lite-preview"
|
| 9 |
|
| 10 |
# Tools + Config (einmalig)
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
def respond(message, history, state):
|
|
|
|
| 18 |
if state is None:
|
| 19 |
state = {
|
| 20 |
"chat": client.chats.create(model=MODEL, config=generate_content_config),
|
| 21 |
"lock": threading.Lock(),
|
| 22 |
}
|
| 23 |
+
|
| 24 |
history = history or []
|
| 25 |
+
text = (message or {}).get("text", "")
|
| 26 |
+
files = (message or {}).get("files", [])
|
|
|
|
| 27 |
parts = []
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# 1. Dateien verarbeiten (Upload via API & UI-Vorbereitung)
|
| 30 |
for file_path in files:
|
| 31 |
+
# Datei an Gemini Server senden
|
| 32 |
+
uploaded_file = client.files.upload(file=file_path)
|
| 33 |
+
parts.append(uploaded_file)
|
| 34 |
+
# Datei in der Gradio UI als Tupel anzeigen
|
| 35 |
+
history.append({"role": "user", "content": (file_path,)})
|
| 36 |
+
|
| 37 |
+
# 2. Text verarbeiten
|
| 38 |
+
if text:
|
| 39 |
+
parts.append(text)
|
| 40 |
+
history.append({"role": "user", "content": text})
|
| 41 |
|
| 42 |
if not parts:
|
| 43 |
yield history, state
|
| 44 |
return
|
| 45 |
|
| 46 |
+
# 3. UI: Assistant Placeholder hinzufügen
|
|
|
|
|
|
|
|
|
|
| 47 |
history.append({"role": "assistant", "content": ""})
|
| 48 |
yield history, state
|
| 49 |
|
| 50 |
+
# 4. Gemini: Senden + Streamen
|
| 51 |
with state["lock"]:
|
| 52 |
out = ""
|
| 53 |
for chunk in state["chat"].send_message_stream(parts):
|
|
|
|
| 60 |
return {"text": "", "files": []}
|
| 61 |
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
+
gr.Markdown("# Gemini AI Chat (Gradio 6.x & File API)")
|
| 64 |
+
|
| 65 |
+
# Standard-Initialisierung für Gradio >= 6.0
|
| 66 |
chatbot = gr.Chatbot(height=600)
|
| 67 |
+
|
| 68 |
msg = gr.MultimodalTextbox(
|
| 69 |
placeholder="Schreib was oder lade Dateien hoch…",
|
| 70 |
file_count="multiple",
|
| 71 |
)
|
| 72 |
state = gr.State(None)
|
| 73 |
+
|
| 74 |
msg.submit(respond, inputs=[msg, chatbot, state], outputs=[chatbot, state]) \
|
| 75 |
.then(clear_msg, outputs=msg)
|
| 76 |
+
|
| 77 |
+
gr.Button("Clear").click(
|
| 78 |
+
lambda: ([], None, {"text": "", "files": []}),
|
| 79 |
+
outputs=[chatbot, state, msg]
|
| 80 |
+
)
|
| 81 |
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
demo.launch()
|
|
|
|
|
|