Update app.py
Browse files
app.py
CHANGED
|
@@ -47,8 +47,8 @@ def chat(user_fact, history):
|
|
| 47 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 48 |
{"role": "user", "content": f"الفعل: {user_fact}"}
|
| 49 |
],
|
|
|
|
| 50 |
}
|
| 51 |
-
|
| 52 |
try:
|
| 53 |
r = requests.post(
|
| 54 |
"http://127.0.0.1:8080/v1/chat/completions",
|
|
@@ -57,8 +57,9 @@ def chat(user_fact, history):
|
|
| 57 |
timeout=400
|
| 58 |
)
|
| 59 |
r.raise_for_status()
|
| 60 |
-
|
| 61 |
result = ""
|
|
|
|
|
|
|
| 62 |
for raw in r.iter_lines():
|
| 63 |
if not raw:
|
| 64 |
continue
|
|
@@ -71,15 +72,19 @@ def chat(user_fact, history):
|
|
| 71 |
try:
|
| 72 |
chunk = json.loads(data)
|
| 73 |
delta = chunk["choices"][0]["delta"].get("content") or ""
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
except (KeyError, json.JSONDecodeError):
|
| 77 |
continue
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
yield f"حدث خطأ أثناء الاتصال بالخادم الداخلي: {str(e)}"
|
| 81 |
|
| 82 |
-
|
| 83 |
# =========================
|
| 84 |
# 5. GRADIO UI
|
| 85 |
# =========================
|
|
|
|
| 47 |
{"role": "system", "content": SYSTEM_PROMPT},
|
| 48 |
{"role": "user", "content": f"الفعل: {user_fact}"}
|
| 49 |
],
|
| 50 |
+
"stream": True, # ← ADD THIS — you were missing the stream flag in payload
|
| 51 |
}
|
|
|
|
| 52 |
try:
|
| 53 |
r = requests.post(
|
| 54 |
"http://127.0.0.1:8080/v1/chat/completions",
|
|
|
|
| 57 |
timeout=400
|
| 58 |
)
|
| 59 |
r.raise_for_status()
|
|
|
|
| 60 |
result = ""
|
| 61 |
+
yielded = False # ← track whether we've yielded anything
|
| 62 |
+
|
| 63 |
for raw in r.iter_lines():
|
| 64 |
if not raw:
|
| 65 |
continue
|
|
|
|
| 72 |
try:
|
| 73 |
chunk = json.loads(data)
|
| 74 |
delta = chunk["choices"][0]["delta"].get("content") or ""
|
| 75 |
+
if delta:
|
| 76 |
+
result += delta
|
| 77 |
+
yielded = True
|
| 78 |
+
yield result
|
| 79 |
except (KeyError, json.JSONDecodeError):
|
| 80 |
continue
|
| 81 |
|
| 82 |
+
if not yielded: # ← always yield something
|
| 83 |
+
yield "لم يتم استلام أي رد من النموذج."
|
| 84 |
+
|
| 85 |
except Exception as e:
|
| 86 |
yield f"حدث خطأ أثناء الاتصال بالخادم الداخلي: {str(e)}"
|
| 87 |
|
|
|
|
| 88 |
# =========================
|
| 89 |
# 5. GRADIO UI
|
| 90 |
# =========================
|