Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
-
# app.py (minimal SSE + CORS)
|
| 2 |
from flask import Flask, request, Response, jsonify, stream_with_context
|
| 3 |
from flask_cors import CORS
|
| 4 |
import os, json, httpx
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
-
CORS(app)
|
| 8 |
|
| 9 |
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_NEW")
|
| 10 |
|
|
@@ -31,38 +30,54 @@ def _payload(q: str):
|
|
| 31 |
|
| 32 |
def stream_openrouter(q: str):
|
| 33 |
if not OPENROUTER_API_KEY:
|
| 34 |
-
yield "data: Missing
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
@app.post("/ask")
|
| 56 |
def ask():
|
| 57 |
payload = request.get_json(silent=True) or {}
|
| 58 |
-
q =
|
| 59 |
if not q:
|
| 60 |
q = "Say hello in one short sentence."
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
-
app.run(host="0.0.0.0", port=7860, debug=False
|
|
|
|
|
|
|
| 1 |
from flask import Flask, request, Response, jsonify, stream_with_context
|
| 2 |
from flask_cors import CORS
|
| 3 |
import os, json, httpx
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
+
CORS(app)
|
| 7 |
|
| 8 |
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_NEW")
|
| 9 |
|
|
|
|
| 30 |
|
| 31 |
def stream_openrouter(q: str):
|
| 32 |
if not OPENROUTER_API_KEY:
|
| 33 |
+
yield "data: Error: Missing API key\n\n"
|
| 34 |
+
return
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
with httpx.Client(timeout=30.0) as client:
|
| 38 |
+
with client.stream("POST",
|
| 39 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 40 |
+
headers=_headers(),
|
| 41 |
+
json=_payload(q)) as response:
|
| 42 |
+
|
| 43 |
+
response.raise_for_status()
|
| 44 |
+
|
| 45 |
+
for line in response.iter_lines():
|
| 46 |
+
if line.startswith('data: '):
|
| 47 |
+
data = line[6:] # Remove 'data: ' prefix
|
| 48 |
+
if data == '[DONE]':
|
| 49 |
+
break
|
| 50 |
+
try:
|
| 51 |
+
json_data = json.loads(data)
|
| 52 |
+
if 'choices' in json_data:
|
| 53 |
+
content = json_data['choices'][0].get('delta', {}).get('content')
|
| 54 |
+
if content:
|
| 55 |
+
yield f"data: {json.dumps(content)}\n\n"
|
| 56 |
+
except json.JSONDecodeError:
|
| 57 |
+
continue
|
| 58 |
+
|
| 59 |
+
yield "data: [DONE]\n\n"
|
| 60 |
+
|
| 61 |
+
except httpx.HTTPError as e:
|
| 62 |
+
yield f"data: Error: {str(e)}\n\n"
|
| 63 |
+
except Exception as e:
|
| 64 |
+
yield f"data: Unexpected error: {str(e)}\n\n"
|
| 65 |
|
| 66 |
@app.post("/ask")
|
| 67 |
def ask():
|
| 68 |
payload = request.get_json(silent=True) or {}
|
| 69 |
+
q = payload.get("question", "").strip()
|
| 70 |
if not q:
|
| 71 |
q = "Say hello in one short sentence."
|
| 72 |
+
|
| 73 |
+
return Response(
|
| 74 |
+
stream_with_context(stream_openrouter(q)),
|
| 75 |
+
mimetype="text/event-stream",
|
| 76 |
+
headers={
|
| 77 |
+
"Cache-Control": "no-cache",
|
| 78 |
+
"X-Accel-Buffering": "no"
|
| 79 |
+
}
|
| 80 |
+
)
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
+
app.run(host="0.0.0.0", port=7860, debug=False)
|