Aktualizovat z1/backend/app.py
This commit is contained in:
parent
1e283bdb67
commit
edec51f193
@ -2,8 +2,22 @@ from flask import Flask, request, jsonify
|
|||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
||||||
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
CORS(app) # Разрешаем CORS для фронтенда
|
CORS(app)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
model_path = "./hate_speech_model/final_model"
|
||||||
|
|
||||||
|
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
||||||
|
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
||||||
|
model.eval()
|
||||||
|
|
||||||
@app.route("/api/predict", methods=["POST"])
|
@app.route("/api/predict", methods=["POST"])
|
||||||
def predict():
|
def predict():
|
||||||
@ -11,15 +25,28 @@ def predict():
|
|||||||
data = request.json
|
data = request.json
|
||||||
text = data.get("text", "")
|
text = data.get("text", "")
|
||||||
|
|
||||||
# Простая логика анализа текста
|
|
||||||
prediction = "Neutrálny text" if "dobry" in text else "Pravdepodobne toxický"
|
|
||||||
|
|
||||||
# ✅ Правильный способ вернуть JSON в UTF-8
|
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
||||||
|
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
outputs = model(**inputs)
|
||||||
|
predictions = torch.argmax(outputs.logits, dim=1).item()
|
||||||
|
|
||||||
|
prediction_label = "Pravdepodobne toxický" if predictions == 1 else "Neutrálny text"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
response = app.response_class(
|
response = app.response_class(
|
||||||
response=json.dumps({"prediction": prediction}, ensure_ascii=False),
|
response=json.dumps({"prediction": prediction_label}, ensure_ascii=False),
|
||||||
status=200,
|
status=200,
|
||||||
mimetype="application/json"
|
mimetype="application/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return response
|
return response
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user