File size: 5,205 Bytes
66ca64a
 
 
367be45
66ca64a
 
367be45
6fb2ead
3f48fa2
66ca64a
367be45
66ca64a
367be45
66ca64a
 
 
 
 
 
 
dbec2e2
 
 
 
66ca64a
 
367be45
 
 
66ca64a
 
367be45
 
 
66ca64a
f02b1de
66ca64a
 
367be45
66ca64a
 
47ca12e
 
 
 
367be45
89d38c5
66ca64a
 
 
367be45
66ca64a
367be45
66ca64a
367be45
66ca64a
 
 
 
367be45
66ca64a
367be45
66ca64a
367be45
66ca64a
367be45
66ca64a
 
6fb2ead
 
8fffd9d
c7dfb4e
6fb2ead
 
 
 
367be45
6fb2ead
 
20c986e
 
 
 
 
6fb2ead
 
 
c7dfb4e
6fb2ead
 
 
f942112
6fb2ead
 
b1b7ebb
367be45
c7dfb4e
 
20c986e
 
 
 
 
8357c1f
c7dfb4e
 
 
 
 
 
 
 
 
8357c1f
6fb2ead
66ca64a
f0b744f
 
 
 
29f3438
f0b744f
 
 
 
 
dbec2e2
66ca64a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
from flask import *

from routes.helpers import configFile
from routes import *

from transformers import AutoTokenizer, AutoModelForSequenceClassification

#initing
app = Flask(__name__)
VERSION = '1.0 build120'
app.config['JSON_AS_ASCII'] = False

#error pages
@app.errorhandler(429)
def ratelimit_handler(e): return render_template('ratelimit.html')
@app.errorhandler(403)
def forbidden_handler(e): return render_template('forbidden.html')
@app.errorhandler(404)
def ratelimit_handler(e): return render_template('notfound.html')
@app.errorhandler(500)
def ratelimit_handler(e): return render_template('intervalservererror.html')
@app.errorhandler(502)
def ratelimit_handler(e): return render_template('badgateway.html')

#empty routes
@app.route('/yt/api/v1', methods=['POST'])
@app.route('/recognize/api/v1', methods=['POST'])
@app.route('/osu/api/v1', methods=['POST'])
def emptyPath(): return {} 

@app.route('/yt/api/v1/<path:path>', methods=['POST'])
@app.route('/recognize/api/v1/<path:path>', methods=['POST'])
@app.route('/osu/api/v1/<path:path>', methods=['POST'])
def emptyApiWA(path):  return {"status": "error", "error_code": 100, "error_details": "No method like that found"}
    
#icon
@app.route('/favicon.ico')

def favicon(): return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')

###############
#SITE ROUTES
@app.route('/')
def index(): return render_template('index.html') 
@app.route('/signatures/api/v1/get', methods=['POST'])
def systemInfo(): return siteRoutes.systemInfo()

###############
#YT SOUND API
@app.route('/yt/api/v1/search', methods=['POST'])
def search(): return ytApi.search(request)
@app.route('/yt/api/v1/get-full', methods=['POST'])
def getFull(): return ytApi.getFull(request)
@app.route('/yt/api/v1/get-preview', methods=['POST'])
def getPreview(): return ytApi.getPreview(request)

###############
#OSU API
@app.route('/osu/api/v1/find-song', methods=['POST'])
def findSong(): return osuApi.findSong(request)
@app.route('/osu/api/v1/get-beatmap', methods=['POST'])
def getBeatmap(): return osuApi.getBeatmap(request)
@app.route('/osu/api/v1/get-preview', methods=['POST'])
def getBMPreview(): return osuApi.getPreview(request)
@app.route('/osu/api/v1/get-full', methods=['POST'])
def getBMFull(): return osuApi.getFull(request)

###############
# LOAD MODELS
sa_t, sa_m = AutoTokenizer.from_pretrained("cardiffnlp/twitter-xlm-roberta-base-sentiment"), AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-xlm-roberta-base-sentiment")
tc_t, tc_m = AutoTokenizer.from_pretrained("EIStakovskii/xlm_roberta_base_multilingual_toxicity_classifier_plus"), AutoModelForSequenceClassification.from_pretrained("EIStakovskii/xlm_roberta_base_multilingual_toxicity_classifier_plus")

##############
# ANALYZE DATA API
# to understand which text is negative, positive or neutral
@app.route('/analyzeText/api/v1/sentiment', methods=['POST'])
def sentimentAnalys():
    try:
        text = request.form.get('text') or request.args.get('text') or request.values.get('text') or ""
        if text == "": 
            try: text = request.json.get('text') or ""
            except: pass
                
        if text == "": return {"status": "error", "details": { "error_code": 101, "error_details": "No text provided" }}
        
        inputs = sa_t(text, return_tensors="pt")

        outputs = sa_m(**inputs)
        logits = outputs.logits
        predicted_sentiment_index = logits.argmax(dim=1).item()
        predicted_sentiment = sa_m.config.id2label[predicted_sentiment_index]
    
        return {"status": "pass", "predicted_sentiment": predicted_sentiment}
    except Exception as e: return {"status": "error", "details": { "error_code": 123, "error_details": str(e).replace("\n", " | ") }}
@app.route('/analyzeText/api/v1/toxicity', methods=['POST'])
def toxicityAnalys():
    try:
        text = request.form.get('text') or request.args.get('text') or request.values.get('text') or ""
        if text == "": 
            try: text = request.json.get('text') or ""
            except: pass
                
        if text == "": return {"status": "error", "details": { "error_code": 101, "error_details": "No text provided" }} , 400

        inputs = tc_t(text, return_tensors="pt")
        
        outputs = tc_m(**inputs)
        logits = outputs.logits
        predicted_class = logits.argmax(dim=1).item()
        predicted_sentiment = True if str(tc_m.config.id2label[predicted_class]) == "LABEL_1" else False
    
        return {"status": "pass", "toxicity": predicted_sentiment}
    except Exception as e: return {"status": "error", "details": { "error_code": 123, "error_details": str(e).replace("\n", " | ") }} , 400

if __name__ == "__main__":
    config = configFile()
    with open(config['config-path'], "w") as outfile:
        config['buildVersion'] = VERSION
        json.dump(config, outfile)
    with open(config['openapi-yaml-path'], "r+") as outfile:
        info = outfile.read()
        outfile.seek(0)
        outfile.write(info.replace('$VERSION_VARIABLE$', VERSION))
        outfile.truncate()
    
    app.run(host="0.0.0.0", port=7860)