dtometzki commited on
Commit
43e9969
·
verified ·
1 Parent(s): 9a480ad

Update app_2.py

Browse files
Files changed (1) hide show
  1. app_2.py +11 -6
app_2.py CHANGED
@@ -7,10 +7,11 @@ 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
 
@@ -25,24 +26,28 @@ def respond(message, history, state):
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
 
 
7
  MODEL = "gemini-3.1-flash-lite-preview"
8
 
9
  def respond(message, history, state):
10
+ # history: [{"role": "user"/"assistant", "content": "..."} , ...]
11
  if state is None:
12
  state = {"chat": client.chats.create(model=MODEL), "lock": threading.Lock()}
13
 
14
+ history = history or []
15
  text = (message or {}).get("text") or ""
16
  files = (message or {}).get("files") or []
17
 
 
26
  parts.append(types.Part.from_bytes(data=f.read(), mime_type=mime))
27
 
28
  if not parts:
 
29
  yield history, state
30
  return
31
 
32
+ # UI: user msg hinzufügen
33
+ user_label = text if text else "[Datei]"
34
+ history.append({"role": "user", "content": user_label})
35
+ # UI: assistant placeholder
36
+ history.append({"role": "assistant", "content": ""})
37
+ yield history, state
38
 
39
+ # Gemini: send + stream (Historie steckt im state["chat"])
40
  with state["lock"]:
41
  out = ""
42
  for chunk in state["chat"].send_message_stream(parts):
43
  if getattr(chunk, "text", None):
44
  out += chunk.text
45
+ history[-1]["content"] = out
46
  yield history, state
47
 
48
  with gr.Blocks() as demo:
49
  gr.Markdown("# Gemini Thinking AI")
50
+ chatbot = gr.Chatbot(height=600) # messages-format default
51
  msg = gr.MultimodalTextbox(placeholder="Schreib was oder lade Dateien hoch…", file_count="multiple")
52
  state = gr.State(None)
53