Spaces:
Running
Running
Update gen.py
Browse files
gen.py
CHANGED
|
@@ -702,12 +702,12 @@ async def generate_text(
|
|
| 702 |
if r.status_code >= 400:
|
| 703 |
print("[STREAM FALLBACK] Primary provider failed → switching to Groq fallback")
|
| 704 |
async for chunk in stream_fallback(client, body):
|
| 705 |
-
yield chunk
|
| 706 |
return
|
| 707 |
|
| 708 |
async for line in r.aiter_lines():
|
| 709 |
if not line:
|
| 710 |
-
yield "\n"
|
| 711 |
continue
|
| 712 |
if line.startswith("event: error"):
|
| 713 |
fallback()
|
|
@@ -720,12 +720,12 @@ async def generate_text(
|
|
| 720 |
except:
|
| 721 |
pass
|
| 722 |
|
| 723 |
-
yield line + "\n"
|
| 724 |
|
| 725 |
except Exception as e:
|
| 726 |
print(f"[STREAM ERROR] {e}")
|
| 727 |
async for chunk in stream_fallback(client, body):
|
| 728 |
-
yield chunk
|
| 729 |
|
| 730 |
|
| 731 |
async def stream_fallback(client, body):
|
|
@@ -754,7 +754,7 @@ async def generate_text(
|
|
| 754 |
|
| 755 |
if r.status_code >= 400:
|
| 756 |
err = (await r.aread()).decode("utf-8", errors="replace")
|
| 757 |
-
yield f'data: {{"error": "Fallback provider failed: {err[:500]}"}}\n\n'
|
| 758 |
return
|
| 759 |
|
| 760 |
async for line in r.aiter_lines():
|
|
@@ -763,23 +763,21 @@ async def generate_text(
|
|
| 763 |
continue
|
| 764 |
|
| 765 |
if not line.startswith("data:"):
|
| 766 |
-
yield f"data: {line}\n\n"
|
| 767 |
else:
|
| 768 |
-
yield
|
| 769 |
|
| 770 |
|
| 771 |
async def event_generator():
|
| 772 |
-
actual_model = None
|
| 773 |
sent_metadata = False
|
| 774 |
|
| 775 |
async with httpx.AsyncClient(timeout=None) as client:
|
| 776 |
-
async for chunk
|
| 777 |
|
| 778 |
if not sent_metadata:
|
| 779 |
-
actual_model = MODEL_MAP[model_used]
|
| 780 |
meta = {
|
| 781 |
"router_metadata": {
|
| 782 |
-
"model_name": MODEL_MAP.get(
|
| 783 |
}
|
| 784 |
}
|
| 785 |
yield f"data: {json.dumps(meta)}\n\n"
|
|
|
|
| 702 |
if r.status_code >= 400:
|
| 703 |
print("[STREAM FALLBACK] Primary provider failed → switching to Groq fallback")
|
| 704 |
async for chunk in stream_fallback(client, body):
|
| 705 |
+
yield chunk
|
| 706 |
return
|
| 707 |
|
| 708 |
async for line in r.aiter_lines():
|
| 709 |
if not line:
|
| 710 |
+
yield "\n"
|
| 711 |
continue
|
| 712 |
if line.startswith("event: error"):
|
| 713 |
fallback()
|
|
|
|
| 720 |
except:
|
| 721 |
pass
|
| 722 |
|
| 723 |
+
yield line + "\n"
|
| 724 |
|
| 725 |
except Exception as e:
|
| 726 |
print(f"[STREAM ERROR] {e}")
|
| 727 |
async for chunk in stream_fallback(client, body):
|
| 728 |
+
yield chunk
|
| 729 |
|
| 730 |
|
| 731 |
async def stream_fallback(client, body):
|
|
|
|
| 754 |
|
| 755 |
if r.status_code >= 400:
|
| 756 |
err = (await r.aread()).decode("utf-8", errors="replace")
|
| 757 |
+
yield f'data: {{"error": "Fallback provider failed: {err[:500]}"}}\n\n'
|
| 758 |
return
|
| 759 |
|
| 760 |
async for line in r.aiter_lines():
|
|
|
|
| 763 |
continue
|
| 764 |
|
| 765 |
if not line.startswith("data:"):
|
| 766 |
+
yield f"data: {line}\n\n"
|
| 767 |
else:
|
| 768 |
+
yield line + "\n"
|
| 769 |
|
| 770 |
|
| 771 |
async def event_generator():
|
|
|
|
| 772 |
sent_metadata = False
|
| 773 |
|
| 774 |
async with httpx.AsyncClient(timeout=None) as client:
|
| 775 |
+
async for chunk in stream_primary(client, url, body, headers):
|
| 776 |
|
| 777 |
if not sent_metadata:
|
|
|
|
| 778 |
meta = {
|
| 779 |
"router_metadata": {
|
| 780 |
+
"model_name": MODEL_MAP.get(chosen_model, chosen_model)
|
| 781 |
}
|
| 782 |
}
|
| 783 |
yield f"data: {json.dumps(meta)}\n\n"
|