-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
75 lines (54 loc) · 2.22 KB
/
server.py
File metadata and controls
75 lines (54 loc) · 2.22 KB
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
from flask import Flask, request, jsonify, render_template
import tensorflow as tf
from transformers import BertTokenizer, TFBertForSequenceClassification
app = Flask(__name__)
path = "C:\\Users\\lenovo\\Desktop\\on-going projects\\BERTmodel2"
bert_tokenizer = BertTokenizer.from_pretrained(path + '\\Tokenizer')
bert_model = TFBertForSequenceClassification.from_pretrained(path + '\\Model')
label = {
1: 'positive',
0: 'Negative'
}
# Route for the home page
@app.route('/')
def home():
return render_template('index.html')
# ... (existing code)
def run_sentiment_analysis(text):
print("Input Text:", text)
Input_ids, Token_type_ids, Attention_mask = bert_tokenizer.batch_encode_plus(
[text],
padding=True,
truncation=True,
max_length=128,
return_tensors='tf'
).values()
prediction = bert_model.predict([Input_ids, Token_type_ids, Attention_mask])
pred_labels = tf.argmax(prediction.logits, axis=1).numpy().tolist()
result = label[pred_labels[0]]
print("Predicted Result:", result)
return result
# Route for emotion detection
@app.route('/emotionDetector', methods=['POST'])
def emotion_detector():
if request.method == 'POST':
data = request.get_json()
text_to_analyze = data.get('text')
result = run_sentiment_analysis(text_to_analyze)
return jsonify({"result": result})
# def get_sentiment(input, Tokenizer=bert_tokenizer, Model=bert_model):
# Convert input into a list if it's not already a list
# if not isinstance(input,list):
# input = [input]
# Input_ids, Token_type_ids, Attention_mask = Tokenizer.batch_encode_plus(input,padding=True,
# truncation=True,
# max_length=128,
# return_tensors='tf').values()
# prediction = Model.predict([Input_ids, Token_type_ids, Attention_mask])
# Use argmax along the appropriate axis to get the predicted labels
# pred_labels = tf.argmax(prediction.logits, axis=1)
# Convert the TensorFlow tensor to a NumPy array and then to a list to get the predicted sentiment labels
# pred_labels = [label[i] for i in pred_labels.numpy().tolist()]
# return pred_labels
if __name__ == '__main__':
app.run(debug=True)