Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -129,21 +129,26 @@ def parse_page_selection(value, num_pages: int) -> int:
|
|
| 129 |
idx = int(m.group(1)) - 1 if m else 0
|
| 130 |
return max(0, min(num_pages - 1, idx))
|
| 131 |
|
| 132 |
-
def build_messages(
|
| 133 |
"""
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
"""
|
| 136 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 137 |
-
trimmed = history[-4:] if history else []
|
| 138 |
-
for u, a in trimmed:
|
| 139 |
-
messages.append({"role": "user", "content": u})
|
| 140 |
-
messages.append({"role": "assistant", "content": a})
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
multimodal = []
|
| 143 |
for im in images:
|
| 144 |
multimodal.append(im)
|
| 145 |
if user_text.strip():
|
| 146 |
multimodal.append(user_text.strip())
|
|
|
|
| 147 |
messages.append({"role": "user", "content": multimodal})
|
| 148 |
return messages
|
| 149 |
|
|
@@ -230,19 +235,37 @@ def page_picker_changed(pages_dropdown, images_state):
|
|
| 230 |
selected = images_state[idx]
|
| 231 |
return selected, selected # preview + selected state
|
| 232 |
|
| 233 |
-
def chat(user_text,
|
| 234 |
if not user_text or not user_text.strip():
|
| 235 |
-
return gr.update(),
|
|
|
|
| 236 |
sel_img = selected_img if selected_img is not None else (images_state[0] if images_state else None)
|
| 237 |
if sel_img is None:
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
acc = ""
|
| 243 |
for chunk in stream:
|
| 244 |
acc = chunk
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
# -----------------------------
|
| 248 |
# App definition
|
|
|
|
| 129 |
idx = int(m.group(1)) - 1 if m else 0
|
| 130 |
return max(0, min(num_pages - 1, idx))
|
| 131 |
|
| 132 |
+
def build_messages(history_msgs: list, user_text: str, images: List[Image.Image]):
|
| 133 |
"""
|
| 134 |
+
Compose the full prompt for the model:
|
| 135 |
+
- system prompt
|
| 136 |
+
- trimmed history (already in {'role','content'} format)
|
| 137 |
+
- current user turn with images + text
|
| 138 |
"""
|
| 139 |
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
# Keep last 8 messages to stay lean
|
| 142 |
+
trimmed = history_msgs[-8:] if history_msgs else []
|
| 143 |
+
messages.extend(trimmed)
|
| 144 |
+
|
| 145 |
+
# Current user turn: images first, then text
|
| 146 |
multimodal = []
|
| 147 |
for im in images:
|
| 148 |
multimodal.append(im)
|
| 149 |
if user_text.strip():
|
| 150 |
multimodal.append(user_text.strip())
|
| 151 |
+
|
| 152 |
messages.append({"role": "user", "content": multimodal})
|
| 153 |
return messages
|
| 154 |
|
|
|
|
| 235 |
selected = images_state[idx]
|
| 236 |
return selected, selected # preview + selected state
|
| 237 |
|
| 238 |
+
def chat(user_text, history_msgs, images_state, selected_img):
|
| 239 |
if not user_text or not user_text.strip():
|
| 240 |
+
return gr.update(), history_msgs
|
| 241 |
+
|
| 242 |
sel_img = selected_img if selected_img is not None else (images_state[0] if images_state else None)
|
| 243 |
if sel_img is None:
|
| 244 |
+
# push a system-style nudge
|
| 245 |
+
history_msgs = history_msgs + [
|
| 246 |
+
{"role": "user", "content": user_text},
|
| 247 |
+
{"role": "assistant", "content": "Please upload a document first."}
|
| 248 |
+
]
|
| 249 |
+
return gr.update(value=history_msgs), history_msgs
|
| 250 |
+
|
| 251 |
+
# Stream the assistant reply
|
| 252 |
+
stream = generate_reply([sel_img], user_text, history_msgs)
|
| 253 |
acc = ""
|
| 254 |
for chunk in stream:
|
| 255 |
acc = chunk
|
| 256 |
+
# do incremental streaming by replacing the last assistant message
|
| 257 |
+
yield (
|
| 258 |
+
history_msgs + [
|
| 259 |
+
{"role": "user", "content": user_text},
|
| 260 |
+
{"role": "assistant", "content": acc},
|
| 261 |
+
],
|
| 262 |
+
history_msgs + [
|
| 263 |
+
{"role": "user", "content": user_text},
|
| 264 |
+
{"role": "assistant", "content": acc},
|
| 265 |
+
]
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
|
| 269 |
|
| 270 |
# -----------------------------
|
| 271 |
# App definition
|