sreepathi-ravikumar commited on
Commit
039db5e
·
verified ·
1 Parent(s): e25dbad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -27
app.py CHANGED
@@ -1,39 +1,31 @@
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)
 
1
+ from flask import Flask, request, jsonify
2
+ from flask_cors import CORS
 
3
  from text2generation import generate_long_answer
4
 
5
+ app = Flask(_name_)
6
+ CORS(app)
7
 
8
+ @app.route('/health')
9
+ def health_check():
10
+ return jsonify({"status": "ready"}), 200
 
 
 
11
 
12
+ @app.route('/ask', methods=['POST'])
13
+ def ask_question():
 
 
 
 
14
  try:
15
+ data = request.get_json()
16
  question = data.get('question', '').strip()
17
+
18
  if not question:
19
+ return jsonify({"error": "Question is required"}), 400
20
+
21
+ answer = generate_long_answer(question)
22
+ return jsonify({
 
23
  "question": question,
24
  "answer": answer
25
  })
26
+
27
  except Exception as e:
28
+ return jsonify({"error": str(e)}), 500
29
 
30
+ if _name_ == '_main_':
31
+ app.run(host='0.0.0.0', port=7860)