Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 2 |
from flask_cors import CORS
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
CORS(app)
|
|
@@ -9,23 +10,38 @@ CORS(app)
|
|
| 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 |
-
|
| 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)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, Response
|
| 2 |
from flask_cors import CORS
|
| 3 |
+
import time
|
| 4 |
+
from text2generation import generate_long_answer # Your own function
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
CORS(app)
|
|
|
|
| 10 |
def health_check():
|
| 11 |
return jsonify({"status": "ready"}), 200
|
| 12 |
|
| 13 |
+
# Standard answer route
|
| 14 |
@app.route('/ask', methods=['POST'])
|
| 15 |
def ask_question():
|
| 16 |
try:
|
| 17 |
data = request.get_json()
|
| 18 |
question = data.get('question', '').strip()
|
| 19 |
+
|
| 20 |
if not question:
|
| 21 |
return jsonify({"error": "Question is required"}), 400
|
| 22 |
+
|
| 23 |
answer = generate_long_answer(question)
|
| 24 |
+
return jsonify({"question": question, "answer": answer})
|
| 25 |
+
|
|
|
|
|
|
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
return jsonify({"error": str(e)}), 500
|
| 28 |
|
| 29 |
+
# Streaming route
|
| 30 |
+
@app.route('/stream', methods=['POST'])
|
| 31 |
+
def stream_answer():
|
| 32 |
+
data = request.get_json()
|
| 33 |
+
question = data.get('question', '').strip()
|
| 34 |
+
|
| 35 |
+
if not question:
|
| 36 |
+
return jsonify({"error": "Question is required"}), 400
|
| 37 |
+
|
| 38 |
+
def generate():
|
| 39 |
+
answer = generate_long_answer(question)
|
| 40 |
+
for word in answer.split():
|
| 41 |
+
yield f"data: {word}\n\n"
|
| 42 |
+
time.sleep(0.05) # 50ms delay per word
|
| 43 |
+
|
| 44 |
+
return Response(generate(), mimetype='text/event-stream')
|
| 45 |
+
|
| 46 |
if __name__ == '__main__':
|
| 47 |
app.run(host='0.0.0.0', port=7860)
|