Aktualizovat sk1/backend/app.py
This commit is contained in:
parent
0ec9a0efb6
commit
ad05bc1fe2
@ -6,10 +6,11 @@ import torch
|
|||||||
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
||||||
from flask_caching import Cache
|
from flask_caching import Cache
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import os
|
import os
|
||||||
from flask import Response
|
from flask import Response
|
||||||
|
import pytz
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
CORS(app)
|
CORS(app)
|
||||||
@ -28,11 +29,16 @@ model.eval()
|
|||||||
def generate_text_hash(text):
|
def generate_text_hash(text):
|
||||||
return hashlib.md5(text.encode('utf-8')).hexdigest()
|
return hashlib.md5(text.encode('utf-8')).hexdigest()
|
||||||
|
|
||||||
|
def get_current_time():
|
||||||
|
tz = pytz.timezone('Europe/Bratislava')
|
||||||
|
now = datetime.now(tz)
|
||||||
|
return now.strftime("%d.%m.%Y %H:%M:%S")
|
||||||
|
|
||||||
def save_to_history(text, prediction_label):
|
def save_to_history(text, prediction_label):
|
||||||
entry = {
|
entry = {
|
||||||
"text": text,
|
"text": text,
|
||||||
"prediction": prediction_label,
|
"prediction": prediction_label,
|
||||||
"timestamp": datetime.now().strftime("%d.%m.%Y %H:%M:%S")
|
"timestamp": get_current_time()
|
||||||
}
|
}
|
||||||
|
|
||||||
if os.path.exists(HISTORY_FILE):
|
if os.path.exists(HISTORY_FILE):
|
||||||
@ -51,6 +57,14 @@ def predict():
|
|||||||
data = request.json
|
data = request.json
|
||||||
text = data.get("text", "")
|
text = data.get("text", "")
|
||||||
|
|
||||||
|
if not text:
|
||||||
|
return jsonify({"error": "Text nesmie byť prázdny."}), 400
|
||||||
|
if len(text) > 512:
|
||||||
|
return jsonify({"error": "Text je príliš dlhý. Maximálne 512 znakov."}), 400
|
||||||
|
if re.search(r"[а-яА-ЯёЁ]", text):
|
||||||
|
return jsonify({"error": "Text nesmie obsahovať azbuku (cyriliku)."}), 400
|
||||||
|
|
||||||
|
|
||||||
text_hash = generate_text_hash(text)
|
text_hash = generate_text_hash(text)
|
||||||
cached_result = cache.get(text_hash)
|
cached_result = cache.get(text_hash)
|
||||||
if cached_result:
|
if cached_result:
|
||||||
@ -90,6 +104,29 @@ def get_history():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/history/raw", methods=["GET"])
|
||||||
|
def get_raw_history():
|
||||||
|
try:
|
||||||
|
if os.path.exists(HISTORY_FILE):
|
||||||
|
with open(HISTORY_FILE, "r", encoding="utf-8") as f:
|
||||||
|
content = f.read()
|
||||||
|
return Response(content, mimetype="application/json")
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "history.json not found"}), 404
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/history/reset", methods=["POST"])
|
||||||
|
def reset_history():
|
||||||
|
try:
|
||||||
|
with open(HISTORY_FILE, "w", encoding="utf-8") as f:
|
||||||
|
json.dump([], f, ensure_ascii=False, indent=2)
|
||||||
|
return jsonify({"message": "History reset successful."}), 200
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
port = int(os.environ.get("PORT", 5000))
|
port = int(os.environ.get("PORT", 5000))
|
||||||
app.run(host="0.0.0.0", port=port)
|
app.run(host="0.0.0.0", port=port)
|
||||||
Loading…
Reference in New Issue
Block a user