Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,39 @@
|
|
| 1 |
-
from
|
| 2 |
-
from
|
|
|
|
| 3 |
from text2generation import generate_long_answer
|
| 4 |
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
@app.
|
| 10 |
-
def health_check():
|
| 11 |
-
return
|
| 12 |
|
| 13 |
-
@app.
|
| 14 |
-
def ask_question():
|
| 15 |
try:
|
| 16 |
-
data = request.
|
| 17 |
question = data.get('question', '').strip()
|
| 18 |
-
|
| 19 |
if not question:
|
| 20 |
-
return
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
"question": question,
|
| 25 |
"answer": answer
|
| 26 |
-
|
| 27 |
-
|
| 28 |
})
|
| 29 |
-
|
| 30 |
except Exception as e:
|
| 31 |
-
return
|
| 32 |
|
| 33 |
-
if
|
| 34 |
-
app.run(host=
|
|
|
|
| 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)
|