Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,54 @@
|
|
1 |
-
from flask import Flask, request, jsonify
|
2 |
-
from classify_model import model, tokenizer
|
3 |
-
import torch
|
4 |
-
import numpy as np
|
5 |
-
import os
|
6 |
-
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
7 |
-
import gdown
|
8 |
-
from underthesea import word_tokenize
|
9 |
-
|
10 |
-
|
11 |
-
if not os.path.exists("save_weights.pt"):
|
12 |
-
file_id = "1JeQ100QELbCCjCozF5SsHT1ca08Vvfuw"
|
13 |
-
url = f"https://drive.google.com/uc?id={file_id}"
|
14 |
-
print("Downloading save_weights.pt from Google Drive...")
|
15 |
-
gdown.download(url, "save_weights.pt", quiet=False)
|
16 |
-
if not os.path.exists("save_weights.pt"):
|
17 |
-
raise FileNotFoundError("Failed to download save_weights.pt")
|
18 |
-
|
19 |
-
model.load_state_dict(torch.load("save_weights.pt"))
|
20 |
-
model.eval()
|
21 |
-
|
22 |
-
app = Flask(__name__)
|
23 |
-
|
24 |
-
def predict_toxic(sentence):
|
25 |
-
# tokens = rdrsegmenter.tokenize(sentence)
|
26 |
-
# statement = ""
|
27 |
-
# for token in tokens:
|
28 |
-
# statement += " ".join(token)
|
29 |
-
# sentence = statement
|
30 |
-
sentence = word_tokenize(sentence, format="text")
|
31 |
-
sequence = tokenizer.encode(sentence)
|
32 |
-
while len(sequence) == 20:
|
33 |
-
sequence.insert(0, 0)
|
34 |
-
padded = torch.tensor([sequence])
|
35 |
-
with torch.no_grad():
|
36 |
-
preds = model(padded)
|
37 |
-
preds = np.argmax(preds.cpu().numpy(), axis=1)
|
38 |
-
return preds[0]
|
39 |
-
|
40 |
-
|
41 |
-
@app.route('/predict', methods=['POST'])
|
42 |
-
def predict():
|
43 |
-
data = request.json
|
44 |
-
sentence = data.get('sentence', '')
|
45 |
-
if not sentence:
|
46 |
-
return jsonify({'error': 'No sentence provided'}), 400
|
47 |
-
result = predict_toxic(sentence)
|
48 |
-
print("Dự đoán:", result, type(result))
|
49 |
-
return jsonify({'toxic': int(result)})
|
50 |
-
|
51 |
-
if __name__ == "__main__":
|
52 |
-
port = int(os.environ.get("PORT",
|
53 |
-
app.run(host="0.0.0.0", port=port)
|
54 |
# app.run(host="0.0.0.0", port=5001)
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from classify_model import model, tokenizer
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
import os
|
6 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
7 |
+
import gdown
|
8 |
+
from underthesea import word_tokenize
|
9 |
+
|
10 |
+
|
11 |
+
if not os.path.exists("save_weights.pt"):
|
12 |
+
file_id = "1JeQ100QELbCCjCozF5SsHT1ca08Vvfuw"
|
13 |
+
url = f"https://drive.google.com/uc?id={file_id}"
|
14 |
+
print("Downloading save_weights.pt from Google Drive...")
|
15 |
+
gdown.download(url, "save_weights.pt", quiet=False)
|
16 |
+
if not os.path.exists("save_weights.pt"):
|
17 |
+
raise FileNotFoundError("Failed to download save_weights.pt")
|
18 |
+
|
19 |
+
model.load_state_dict(torch.load("save_weights.pt"))
|
20 |
+
model.eval()
|
21 |
+
|
22 |
+
app = Flask(__name__)
|
23 |
+
|
24 |
+
def predict_toxic(sentence):
|
25 |
+
# tokens = rdrsegmenter.tokenize(sentence)
|
26 |
+
# statement = ""
|
27 |
+
# for token in tokens:
|
28 |
+
# statement += " ".join(token)
|
29 |
+
# sentence = statement
|
30 |
+
sentence = word_tokenize(sentence, format="text")
|
31 |
+
sequence = tokenizer.encode(sentence)
|
32 |
+
while len(sequence) == 20:
|
33 |
+
sequence.insert(0, 0)
|
34 |
+
padded = torch.tensor([sequence])
|
35 |
+
with torch.no_grad():
|
36 |
+
preds = model(padded)
|
37 |
+
preds = np.argmax(preds.cpu().numpy(), axis=1)
|
38 |
+
return preds[0]
|
39 |
+
|
40 |
+
|
41 |
+
@app.route('/predict', methods=['POST'])
|
42 |
+
def predict():
|
43 |
+
data = request.json
|
44 |
+
sentence = data.get('sentence', '')
|
45 |
+
if not sentence:
|
46 |
+
return jsonify({'error': 'No sentence provided'}), 400
|
47 |
+
result = predict_toxic(sentence)
|
48 |
+
print("Dự đoán:", result, type(result))
|
49 |
+
return jsonify({'toxic': int(result)})
|
50 |
+
|
51 |
+
if __name__ == "__main__":
|
52 |
+
port = int(os.environ.get("PORT", 5001))
|
53 |
+
app.run(host="0.0.0.0", port=port)
|
54 |
# app.run(host="0.0.0.0", port=5001)
|