sreepathi-ravikumar commited on
Commit
b10bd1b
·
verified ·
1 Parent(s): 73364a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -29
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) # simple: allow all origins for testing
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 OPENROUTER_API_NEW\n\n"; return
35
- with httpx.Client(timeout=None) as client:
36
- with client.stream("POST", "https://openrouter.ai/api/v1/chat/completions",
37
- headers=_headers(), json=_payload(q)) as r:
38
- r.raise_for_status()
39
- for raw in r.iter_lines():
40
- if not raw:
41
- continue
42
- data = raw[5:].strip() if raw.startswith(b"data:") else raw.strip()
43
- if data == b"[DONE]":
44
- yield "data: [DONE]\n\n"
45
- break
46
- try:
47
- obj = json.loads(data.decode("utf-8"))
48
- delta = obj.get("choices", [{}])[0].get("delta", {}).get("content")
49
- if delta:
50
- # Send plain text chunks (still framed as SSE data)
51
- yield f"data: {json.dumps(delta)}\n\n"
52
- except Exception:
53
- pass
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  @app.post("/ask")
56
  def ask():
57
  payload = request.get_json(silent=True) or {}
58
- q = (payload.get("question") or payload.get("userPrompt") or "").strip()
59
  if not q:
60
  q = "Say hello in one short sentence."
61
- resp = Response(stream_with_context(stream_openrouter(q)), mimetype="text/event-stream")
62
- resp.headers["Cache-Control"] = "no-cache"
63
- resp.headers["X-Accel-Buffering"] = "no"
64
- resp.headers["Connection"] = "keep-alive"
65
- return resp
 
 
 
 
66
 
67
  if __name__ == "__main__":
68
- app.run(host="0.0.0.0", port=7860, debug=False, threaded=True)
 
 
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)