maynaS commited on
Commit
106f27e
1 Parent(s): 7aa666d

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +83 -0
main.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from sklearn.model_selection import train_test_split
3
+ from request_body import request_body
4
+ from utilities import *
5
+ from classifier import Classifier
6
+
7
+ # Download necessary modules
8
+ nltk.download('punkt')
9
+
10
+ # Get dataset and convert it into suitable form of input for the classification model
11
+ filename = "airline_sentiment_analysis.csv"
12
+ raw_data = get_data_for_training(filename)
13
+ sentences, labels = get_data_and_labels(raw_data)
14
+ sentences = get_word_embeddings(sentences)
15
+
16
+ # Spliting the dataset into Training and Testing dataset to train the model
17
+ # Train Set = 50%
18
+ # Test Set = 50%
19
+ airline_train_data, airline_test_data, airline_train_labels, airline_test_labels = train_test_split(
20
+ sentences, labels, test_size=0.5, random_state=42)
21
+
22
+ # Vectorize the sequence for both train and test datasets
23
+ x_train = vectorize_sequence(airline_train_data, 20000)
24
+ x_test = vectorize_sequence(airline_test_data, 20000)
25
+
26
+ y_train = np.asarray(airline_train_labels).astype('float32')
27
+ y_test = np.asarray(airline_test_labels).astype('float32')
28
+
29
+ print(x_train.shape)
30
+ print(x_test.shape)
31
+ print(y_train.shape)
32
+ print(y_test.shape)
33
+
34
+ # # Naive Bayes
35
+ # classifier_mnb = Classifier("Naive Bayes")
36
+ # classifier_mnb.train(x_train, y_train)
37
+ # print("NAIVE BAYES")
38
+ # print("train shape: " + str(x_train.shape))
39
+ # print("score on test: " + str(classifier_mnb.score(x_test, y_test)))
40
+ # print("score on train: " + str(classifier_mnb.score(x_train, y_train)))
41
+
42
+ # Logistic Regression
43
+ classifier_lr = Classifier("Logistic Regression")
44
+ classifier_lr.train(x_train, y_train)
45
+ print("LOGISTIC REGRESSION")
46
+ print("train shape: " + str(x_train.shape))
47
+ print("score on test: " + str(classifier_lr.score(x_test, y_test)))
48
+ print("score on train: " + str(classifier_lr.score(x_train, y_train)))
49
+
50
+ # # KNN
51
+ # classifier_knn = Classifier("KNN")
52
+ # classifier_knn.train(x_train, y_train)
53
+
54
+ # print("KNN")
55
+ # print("train shape: " + str(x_train.shape))
56
+ # print("score on train: " + str(classifier_knn.score(x_train, y_train)))
57
+ # print("score on test: " + str(classifier_knn.score(x_test, y_test)))
58
+
59
+ # # Support Vector Machines
60
+ # classifier_svm = Classifier("SVM")
61
+ # classifier_svm.train(x_train, y_train)
62
+
63
+ # print("SUPPORT VECTOR MACHINE")
64
+ # print("train shape: " + str(x_train.shape))
65
+ # print("score on test: " + str(classifier_svm.score(x_test, y_test)))
66
+ # print("score on train: " + str(classifier_svm.score(x_train, y_train)))
67
+
68
+ app = FastAPI()
69
+
70
+ @app.post('/predict')
71
+ def predict(data: request_body):
72
+ text = data.text
73
+ sequence = get_sequence(text)
74
+ sequence = vectorize_sequence(sequence, 20000)
75
+ print(sequence)
76
+
77
+ class_idx = classifier_lr.classify(sequence)[0]
78
+ print(class_idx)
79
+ class_idx = (int)(class_idx)
80
+ if class_idx==1:
81
+ return {'sentiment': "positive"}
82
+ else:
83
+ return {'sentiment': "negative"}