sreepathi-ravikumar commited on
Commit
3272a52
·
verified ·
1 Parent(s): 250e906

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -27
app.py CHANGED
@@ -1,42 +1,108 @@
1
- from flask import Flask, request, jsonify, Response,redirect
 
 
2
  from flask_cors import CORS
3
- import time
4
- import random
5
- from text2generation import generate_long_answer # Your own function
6
 
7
  app = Flask(__name__)
8
  CORS(app)
9
 
10
- @app.route('/health')
11
- def health_check():
 
 
12
  return jsonify({"status": "ready"}), 200
13
 
14
- # Standard answer route
15
- @app.route('/ask', methods=['POST'])
16
- def ask_question():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  try:
18
- data = request.get_json()
19
- question = data.get('question', '').strip()
20
 
 
 
 
21
  if not question:
22
  return jsonify({"error": "Question is required"}), 400
23
 
24
- answer = generate_long_answer(question)
25
- return jsonify({"question": question, "answer": answer})
26
-
 
 
 
 
 
 
 
 
 
 
 
 
27
  except Exception as e:
28
  return jsonify({"error": str(e)}), 500
29
 
30
- @app.route('/get_video')
31
- def get_video():
32
- video_lst=[
33
- "https://videos.pexels.com/video-files/3571264/3571264-sd_640_360_30fps.mp4",
34
- "https://videos.pexels.com/video-files/3052446/3052446-hd_1920_1080_30fps.mp4",
35
- "https://videos.pexels.com/video-files/6394054/6394054-sd_960_480_24fps.mp4",
36
-
37
- ]
38
- return redirect(random.choice(video_lst))
39
-
40
-
41
- if __name__ == '__main__':
42
- app.run(host='0.0.0.0', port=7860)
 
1
+ import os
2
+ import json
3
+ from flask import Flask, request, jsonify, Response, stream_with_context
4
  from flask_cors import CORS
5
+ import httpx
6
+
 
7
 
8
  app = Flask(__name__)
9
  CORS(app)
10
 
11
+ OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_NEW")
12
+
13
+ @app.route("/health", methods=["GET"])
14
+ def health():
15
  return jsonify({"status": "ready"}), 200
16
 
17
+ def openrouter_headers():
18
+ return {
19
+ "Authorization": f"Bearer {OPENROUTER_API_KEY}",
20
+ "Content-Type": "application/json",
21
+ "HTTP-Referer": "https://sreepathi-ravikumar-sample1.hf.space",
22
+ "X-Title": "Educational AI Assistant",
23
+ }
24
+
25
+ def build_payload(question: str) -> dict:
26
+ return {
27
+ "model": "deepseek/deepseek-chat-v3-0324:free",
28
+ "stream": True,
29
+ "messages": [{
30
+ "role": "user",
31
+ "content": "condition every topics or heading only must startswith # and endswith :\n\n" + question,
32
+ }],
33
+ "temperature": 0.3,
34
+ "max_tokens": 599,
35
+ }
36
+
37
+ def stream_openrouter(question: str):
38
+ with httpx.Client(timeout=None) as client:
39
+ with client.stream(
40
+ "POST",
41
+ "https://openrouter.ai/api/v1/chat/completions",
42
+ headers=openrouter_headers(),
43
+ json=build_payload(question),
44
+ ) as r:
45
+ r.raise_for_status()
46
+ # Optional start signal
47
+ yield "event: start\ndata: stream\n\n"
48
+ for raw in r.iter_lines():
49
+ if not raw:
50
+ continue
51
+ # OpenRouter uses SSE-like "data:" lines
52
+ if raw.startswith(b"data:"):
53
+ data = raw[5:].strip()
54
+ else:
55
+ data = raw.strip()
56
+
57
+ if data == b"[DONE]":
58
+ yield "event: done\ndata: [DONE]\n\n"
59
+ break
60
+
61
+ try:
62
+ obj = json.loads(data.decode("utf-8"))
63
+ delta = obj.get("choices", [{}])[0].get("delta", {}).get("content")
64
+ if delta:
65
+ # Wrap each token in JSON so the frontend can parse safely
66
+ yield f"data: {json.dumps({'text': delta})}\n\n"
67
+ except Exception:
68
+ # Fallback: forward as plain text
69
+ try:
70
+ txt = data.decode("utf-8")
71
+ except Exception:
72
+ txt = ""
73
+ if txt:
74
+ yield f"data: {json.dumps({'text': txt})}\n\n"
75
+
76
+ @app.route("/ask", methods=["POST"])
77
+ def ask():
78
  try:
79
+ if not OPENROUTER_API_KEY:
80
+ return jsonify({"error": "OPENROUTER_API_NEW is not set"}), 500
81
 
82
+ data = request.get_json(silent=True) or {}
83
+ # Accept both keys for compatibility
84
+ question = (data.get("question") or data.get("userPrompt") or "").strip()
85
  if not question:
86
  return jsonify({"error": "Question is required"}), 400
87
 
88
+ # Stream back as SSE
89
+ resp = Response(
90
+ stream_with_context(stream_openrouter(question)),
91
+ mimetype="text/event-stream",
92
+ )
93
+ # Important headers to avoid buffering by proxies/CDNs
94
+ resp.headers["Cache-Control"] = "no-cache"
95
+ resp.headers["X-Accel-Buffering"] = "no"
96
+ resp.headers["Connection"] = "keep-alive"
97
+ # CORS for SSE: allow the local frontend origins
98
+ origin = request.headers.get("Origin")
99
+ if origin in ALLOWED_ORIGINS:
100
+ resp.headers["Access-Control-Allow-Origin"] = origin
101
+ resp.headers["Vary"] = "Origin"
102
+ return resp
103
  except Exception as e:
104
  return jsonify({"error": str(e)}), 500
105
 
106
+ if __name__ == "__main__":
107
+ # In Spaces, use the default port; locally, 7860 is fine.
108
+ app.run(host="0.0.0.0", port=7860, debug=True, threaded=True)