Spaces:
Sleeping
Sleeping
Commit ·
42e3bbf
1
Parent(s): 55594be
fix: restore streaming in call_llama — heartbeat proxy requires stream=True
Browse filesWithout stream=True, llama-server returns plain JSON instead of SSE.
The heartbeat proxy can't send keepalive pings → Cloudflare 524.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -150,13 +150,31 @@ def call_llama(img_b64: str, prompt: str) -> str:
|
|
| 150 |
}
|
| 151 |
elif OUTPUT_MODE == "line":
|
| 152 |
payload["grammar"] = LINE_GRAMMAR
|
|
|
|
| 153 |
r = requests.post(
|
| 154 |
f"{LLAMA_SERVER}/v1/chat/completions",
|
| 155 |
json=payload,
|
| 156 |
timeout=TIMEOUT,
|
|
|
|
| 157 |
)
|
| 158 |
r.raise_for_status()
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
# ---------- Parsing ----------
|
| 162 |
|
|
|
|
| 150 |
}
|
| 151 |
elif OUTPUT_MODE == "line":
|
| 152 |
payload["grammar"] = LINE_GRAMMAR
|
| 153 |
+
payload["stream"] = True
|
| 154 |
r = requests.post(
|
| 155 |
f"{LLAMA_SERVER}/v1/chat/completions",
|
| 156 |
json=payload,
|
| 157 |
timeout=TIMEOUT,
|
| 158 |
+
stream=True,
|
| 159 |
)
|
| 160 |
r.raise_for_status()
|
| 161 |
+
content = []
|
| 162 |
+
for line in r.iter_lines():
|
| 163 |
+
if not line:
|
| 164 |
+
continue
|
| 165 |
+
line = line.decode("utf-8") if isinstance(line, bytes) else line
|
| 166 |
+
if line.startswith("data: "):
|
| 167 |
+
data = line[6:]
|
| 168 |
+
if data.strip() == "[DONE]":
|
| 169 |
+
break
|
| 170 |
+
try:
|
| 171 |
+
chunk = json.loads(data)
|
| 172 |
+
delta = chunk["choices"][0]["delta"].get("content", "")
|
| 173 |
+
if delta:
|
| 174 |
+
content.append(delta)
|
| 175 |
+
except (json.JSONDecodeError, KeyError, IndexError):
|
| 176 |
+
pass
|
| 177 |
+
return "".join(content)
|
| 178 |
|
| 179 |
# ---------- Parsing ----------
|
| 180 |
|