Spaces:
Running
Running
fix errors with file watching and toolbar mode
Browse files- .streamlit/config.toml +6 -0
- streamlit_app.py +30 -12
.streamlit/config.toml
CHANGED
|
@@ -1,6 +1,12 @@
|
|
| 1 |
[browser]
|
| 2 |
gatherUsageStats = false
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
[theme]
|
| 5 |
base = "dark"
|
| 6 |
primaryColor = "#C9A84C"
|
|
|
|
| 1 |
[browser]
|
| 2 |
gatherUsageStats = false
|
| 3 |
|
| 4 |
+
[server]
|
| 5 |
+
fileWatcherType = "none"
|
| 6 |
+
|
| 7 |
+
[client]
|
| 8 |
+
toolbarMode = "viewer"
|
| 9 |
+
|
| 10 |
[theme]
|
| 11 |
base = "dark"
|
| 12 |
primaryColor = "#C9A84C"
|
streamlit_app.py
CHANGED
|
@@ -264,7 +264,11 @@ def _synthesis_gen(
|
|
| 264 |
model: str,
|
| 265 |
ctx: _StreamCtx,
|
| 266 |
) -> Generator[str, None, None]:
|
| 267 |
-
"""Yield only the response-field tokens; populate ctx.full_text throughout.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
stream = client.chat.completions.create(
|
| 269 |
model=model,
|
| 270 |
messages=messages,
|
|
@@ -272,24 +276,38 @@ def _synthesis_gen(
|
|
| 272 |
temperature=0.6,
|
| 273 |
max_tokens=4096,
|
| 274 |
)
|
|
|
|
| 275 |
in_response = False
|
|
|
|
|
|
|
| 276 |
for chunk in stream:
|
| 277 |
delta = (chunk.choices[0].delta.content or "") if chunk.choices else ""
|
| 278 |
ctx.full_text += delta
|
| 279 |
|
| 280 |
if not in_response:
|
| 281 |
-
if _RESPONSE_MARKER in ctx.full_text:
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
yield after.split(_SOURCES_MARKER, 1)[0].rstrip()
|
| 286 |
-
return
|
| 287 |
-
if after:
|
| 288 |
-
yield after
|
| 289 |
else:
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
|
| 295 |
def _explain_gen(
|
|
|
|
| 264 |
model: str,
|
| 265 |
ctx: _StreamCtx,
|
| 266 |
) -> Generator[str, None, None]:
|
| 267 |
+
"""Yield only the response-field tokens; populate ctx.full_text throughout.
|
| 268 |
+
|
| 269 |
+
We keep the last GUARD characters buffered at all times so that a marker
|
| 270 |
+
arriving split across multiple chunks never gets partially yielded to the UI.
|
| 271 |
+
"""
|
| 272 |
stream = client.chat.completions.create(
|
| 273 |
model=model,
|
| 274 |
messages=messages,
|
|
|
|
| 276 |
temperature=0.6,
|
| 277 |
max_tokens=4096,
|
| 278 |
)
|
| 279 |
+
GUARD = len(_SOURCES_MARKER)
|
| 280 |
in_response = False
|
| 281 |
+
buf = "" # response text buffered but not yet yielded
|
| 282 |
+
|
| 283 |
for chunk in stream:
|
| 284 |
delta = (chunk.choices[0].delta.content or "") if chunk.choices else ""
|
| 285 |
ctx.full_text += delta
|
| 286 |
|
| 287 |
if not in_response:
|
| 288 |
+
if _RESPONSE_MARKER not in ctx.full_text:
|
| 289 |
+
continue
|
| 290 |
+
in_response = True
|
| 291 |
+
buf = ctx.full_text.split(_RESPONSE_MARKER, 1)[1].lstrip("\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
else:
|
| 293 |
+
buf += delta
|
| 294 |
+
|
| 295 |
+
# Sources marker has arrived — yield safe prefix and stop
|
| 296 |
+
if _SOURCES_MARKER in buf:
|
| 297 |
+
safe = buf.split(_SOURCES_MARKER, 1)[0].rstrip()
|
| 298 |
+
if safe:
|
| 299 |
+
yield safe
|
| 300 |
+
return
|
| 301 |
+
|
| 302 |
+
# Yield only the confirmed-safe portion; keep last GUARD chars buffered
|
| 303 |
+
# to guard against the marker arriving split across chunks
|
| 304 |
+
if len(buf) > GUARD:
|
| 305 |
+
yield buf[:-GUARD]
|
| 306 |
+
buf = buf[-GUARD:]
|
| 307 |
+
|
| 308 |
+
# Stream ended without a sources marker — flush remaining buffer
|
| 309 |
+
if in_response and buf:
|
| 310 |
+
yield buf.rstrip()
|
| 311 |
|
| 312 |
|
| 313 |
def _explain_gen(
|