Spaces:
Running
Running
Commit
•
6fb2ead
1
Parent(s):
2fddf68
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,9 @@ from flask_limiter.util import get_remote_address
|
|
6 |
from routes.helpers import checkSignature, configFile
|
7 |
from routes import *
|
8 |
|
|
|
|
|
|
|
9 |
#initing
|
10 |
app = Flask(__name__)
|
11 |
VERSION = '1.0 build89'
|
@@ -20,7 +23,7 @@ def ip_whitelist():
|
|
20 |
# else: signature = request.args['signature']
|
21 |
# return checkSignature(signature)
|
22 |
#except: return False
|
23 |
-
return
|
24 |
#error pages
|
25 |
@app.errorhandler(429)
|
26 |
def ratelimit_handler(e): return render_template('ratelimit.html')
|
@@ -86,6 +89,47 @@ def getBMPreview(): return osuApi.getPreview(request)
|
|
86 |
@app.route('/osu/api/v1/get-full', methods=['GET', 'POST'])
|
87 |
def getBMFull(): return osuApi.getFull(request)
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
if __name__ == "__main__":
|
90 |
config = configFile()
|
91 |
with open(config['config-path'], "w") as outfile:
|
|
|
6 |
from routes.helpers import checkSignature, configFile
|
7 |
from routes import *
|
8 |
|
9 |
+
from random import randint
|
10 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
11 |
+
|
12 |
#initing
|
13 |
app = Flask(__name__)
|
14 |
VERSION = '1.0 build89'
|
|
|
23 |
# else: signature = request.args['signature']
|
24 |
# return checkSignature(signature)
|
25 |
#except: return False
|
26 |
+
return bool(randint(0,1))
|
27 |
#error pages
|
28 |
@app.errorhandler(429)
|
29 |
def ratelimit_handler(e): return render_template('ratelimit.html')
|
|
|
89 |
@app.route('/osu/api/v1/get-full', methods=['GET', 'POST'])
|
90 |
def getBMFull(): return osuApi.getFull(request)
|
91 |
|
92 |
+
###############
|
93 |
+
# LOAD MODELS
|
94 |
+
sa_t, sa_m = AutoTokenizer.from_pretrained("cardiffnlp/twitter-xlm-roberta-base-sentiment"), AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-xlm-roberta-base-sentiment")
|
95 |
+
ld_t, ld_m = AutoTokenizer.from_pretrained("papluca/xlm-roberta-base-language-detection"), AutoModelForSequenceClassification.from_pretrained("papluca/xlm-roberta-base-language-detection")
|
96 |
+
|
97 |
+
##############
|
98 |
+
# ANALYZE DATA API
|
99 |
+
# to understand which text is negative, positive or neutral
|
100 |
+
@app.route('/analyzeText/api/v1/sentiment', methods=['GET', 'POST'])
|
101 |
+
def sentimentAnalys():
|
102 |
+
try:
|
103 |
+
text = request.form.get('text') or request.args.get('url') or ""
|
104 |
+
if text == "": return {"status": "error", "details": { "error_code": 101, "error_details": "No text provided" }}
|
105 |
+
|
106 |
+
inputs = sa_t(text, return_tensors="pt")
|
107 |
+
|
108 |
+
# Предсказание тональности текста
|
109 |
+
outputs = sa_m(**inputs)
|
110 |
+
logits = outputs.logits
|
111 |
+
predicted_sentiment_index = logits.argmax(dim=1).item()
|
112 |
+
predicted_sentiment = model.config.id2label[predicted_sentiment_index]
|
113 |
+
|
114 |
+
return {"status": "pass", "predicted_sentiment": predicted_sentiment}
|
115 |
+
except: return {"status": "error", "details": { "error_code": 123, "error_details": str(e).replace("\n", " | ") }}
|
116 |
+
|
117 |
+
@app.route('/analyzeText/api/v1/detectLang')
|
118 |
+
def langDetect():
|
119 |
+
try:
|
120 |
+
text = request.form.get('text') or request.args.get('url') or ""
|
121 |
+
if text == "": return {"status": "error", "details": { "error_code": 101, "error_details": "No text provided" }}
|
122 |
+
|
123 |
+
inputs = ld_t(text, return_tensors="pt")
|
124 |
+
|
125 |
+
outputs = ld_m(**inputs)
|
126 |
+
logits = outputs.logits
|
127 |
+
predicted_language_index = logits.argmax(dim=1).item()
|
128 |
+
predicted_language = model.config.id2label[predicted_language_index]
|
129 |
+
|
130 |
+
return {"status": "pass", "predicted_language": predicted_language}
|
131 |
+
except: return {"status": "error", "details": { "error_code": 123, "error_details": str(e).replace("\n", " | ") }}
|
132 |
+
|
133 |
if __name__ == "__main__":
|
134 |
config = configFile()
|
135 |
with open(config['config-path'], "w") as outfile:
|