sreepathi-ravikumar commited on
Commit
24d1f39
·
verified ·
1 Parent(s): 273ca2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -1,34 +1,39 @@
1
- from flask import Flask, request, jsonify
2
- from flask_cors import CORS
 
3
  from text2generation import generate_long_answer
4
 
 
 
5
 
6
- app = Flask(__name__)
7
- CORS(app)
 
 
 
 
8
 
9
- @app.route('/health')
10
- def health_check():
11
- return jsonify({"status": "ready"}), 200
12
 
13
- @app.route('/ask', methods=['POST'])
14
- def ask_question():
15
  try:
16
- data = request.get_json()
17
  question = data.get('question', '').strip()
18
-
19
  if not question:
20
- return jsonify({"error": "Question is required"}), 400
21
-
22
- answer = generate_long_answer(question)
23
- return jsonify({
 
24
  "question": question,
25
  "answer": answer
26
-
27
-
28
  })
29
-
30
  except Exception as e:
31
- return jsonify({"error": str(e)}), 500
32
 
33
- if __name__ == '__main__':
34
- app.run(host='0.0.0.0', port=7860)
 
1
+ from sanic import Sanic
2
+ from sanic.response import json
3
+ from sanic_ext import Extend
4
  from text2generation import generate_long_answer
5
 
6
+ app = Sanic("UltraFastSanicApp")
7
+ Extend(app) # Enables CORS and other extras
8
 
9
+ # Enable CORS globally
10
+ @app.middleware("response")
11
+ async def enable_cors(request, response):
12
+ response.headers["Access-Control-Allow-Origin"] = "*"
13
+ response.headers["Access-Control-Allow-Headers"] = "*"
14
+ response.headers["Access-Control-Allow-Methods"] = "*"
15
 
16
+ @app.get("/health")
17
+ async def health_check(request):
18
+ return json({"status": "ready"}, status=200)
19
 
20
+ @app.post("/ask")
21
+ async def ask_question(request):
22
  try:
23
+ data = request.json
24
  question = data.get('question', '').strip()
25
+
26
  if not question:
27
+ return json({"error": "Question is required"}, status=400)
28
+
29
+ # generate_long_answer() can be async if needed, here kept sync for simplicity
30
+ answer =await generate_long_answer(question)
31
+ return json({
32
  "question": question,
33
  "answer": answer
 
 
34
  })
 
35
  except Exception as e:
36
+ return json({"error": str(e)}, status=500)
37
 
38
+ if _name_ == "_main_":
39
+ app.run(host="0.0.0.0", port=7860, fast=True, access_log=False)