-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
94 lines (59 loc) · 1.77 KB
/
app.py
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
from scipy.special import softmax
from transformers import AutoModelForSequenceClassification
from transformers import AutoTokenizer
from flask import Flask, jsonify, request
import json
import pandas as pd
import numpy as np
import seaborn as sns
import nltk
#path = './Reviews.csv'
#df = pd.read_csv(path)
# df = df.head(500)
def analyse(example):
Tokens = nltk.word_tokenize(example)
Tokens[:25]
# nltk.download('averaged_perceptron_tagger')
Tagged = nltk.pos_tag(Tokens)
Tagged[:25]
# nltk.download('maxent_ne_chunker')
# nltk.download('words')
entities = nltk.chunk.ne_chunk(Tagged)
entities.pprint()
MODEL = f"cardiffnlp/twitter-roberta-base-sentiment"
tokenizer = AutoTokenizer.from_pretrained(MODEL)
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
encoded_text = tokenizer(example, return_tensors='pt')
output = model(**encoded_text)
scores = output[0][0].detach().numpy()
scores = softmax(scores)
return {
'roberta_neg': float(scores[0]),
'roberta_neu': float(scores[1]),
'roberta_pos': float(scores[2])
}
# nltk.download('punkt')
# example = 'This has an awesome taste'
api = Flask(__name__)
example =""
@api.route('/d', methods=['GET', 'POST'])
def d():
body=request.json
example= str(body['text'])
dict=analyse(example)
print (dict)
response_body = {
'Your Sentence': body['text'],
'roberta_neg': dict['roberta_neg'],
'roberta_neu': dict['roberta_neu'],
'roberta_pos': dict['roberta_pos']
}
return response_body
@api.route('/')
def roberta():
response_body = {
'name': 'roberta',
}
return response_body
if __name__ =='__main__':
api.run(host="0.0.0.0", port=5000)