Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,18 +4,30 @@ import os
|
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
|
|
|
|
|
|
7 |
@app.route("/ask", methods=["POST"])
|
8 |
def ask_question():
|
|
|
9 |
try:
|
10 |
-
client
|
|
|
|
|
|
|
11 |
input_text = request.json.get("text")
|
12 |
if not input_text:
|
13 |
return jsonify({"error": "Missing 'text'"}), 400
|
|
|
14 |
result = client.predict(input_text, api_name="/predict")
|
15 |
return jsonify({"question": result})
|
16 |
except Exception as e:
|
17 |
return jsonify({"error": str(e)}), 500
|
18 |
|
|
|
|
|
|
|
|
|
19 |
if __name__ == "__main__":
|
20 |
port = int(os.environ.get("PORT", 7860))
|
21 |
app.run(host="0.0.0.0", port=port)
|
|
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") # token env üzerinden alınıyor
|
8 |
+
client = None # Global cache – sadece 1 kez başlatılır
|
9 |
+
|
10 |
@app.route("/ask", methods=["POST"])
|
11 |
def ask_question():
|
12 |
+
global client
|
13 |
try:
|
14 |
+
# Sadece ilk istek için client nesnesini oluştur
|
15 |
+
if client is None:
|
16 |
+
client = Client("memorease/flan5_memorease", hf_token=HF_TOKEN)
|
17 |
+
|
18 |
input_text = request.json.get("text")
|
19 |
if not input_text:
|
20 |
return jsonify({"error": "Missing 'text'"}), 400
|
21 |
+
|
22 |
result = client.predict(input_text, api_name="/predict")
|
23 |
return jsonify({"question": result})
|
24 |
except Exception as e:
|
25 |
return jsonify({"error": str(e)}), 500
|
26 |
|
27 |
+
@app.route("/", methods=["GET"])
|
28 |
+
def root_check():
|
29 |
+
return jsonify({"status": "running"})
|
30 |
+
|
31 |
if __name__ == "__main__":
|
32 |
port = int(os.environ.get("PORT", 7860))
|
33 |
app.run(host="0.0.0.0", port=port)
|