bhjaswanthreddy commited on
Commit
d48676d
·
verified ·
1 Parent(s): 43e0764

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from sklearn.model_selection import train_test_split
3
+ from sklearn.feature_extraction.text import CountVectorizer
4
+ from sklearn.neighbors import KNeighborsClassifier
5
+ from sklearn.linear_model import LogisticRegression
6
+ import joblib
7
+ import gradio as gr
8
+
9
+ # Load the dataset
10
+ data_df = pd.read_csv('homework01_text_data_group13.csv')
11
+
12
+ # Separate features and labels
13
+ X = data_df['reviews']
14
+ y = data_df['class']
15
+
16
+ # Split the data into train and test sets
17
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
18
+
19
+ # Create bag-of-words representations
20
+ vectorizer = CountVectorizer()
21
+ X_train_counts = vectorizer.fit_transform(X_train)
22
+ X_test_counts = vectorizer.transform(X_test)
23
+
24
+ # Train the KNN model
25
+ knn_model = KNeighborsClassifier(n_neighbors=2, metric='euclidean')
26
+ knn_model.fit(X_train_counts, y_train)
27
+
28
+ # Train the Logistic Regression model
29
+ logistic_model = LogisticRegression(penalty='l2', C=1, random_state=0)
30
+ logistic_model.fit(X_train_counts, y_train)
31
+
32
+ # Save the trained models
33
+ joblib.dump(knn_model, 'best_knn_model.pkl')
34
+ joblib.dump(logistic_model, 'best_logistic_regression_model.pkl')
35
+
36
+ def predict_knn(review_text, model=knn_model):
37
+ X_test = vectorizer.transform([review_text])
38
+ y_pred = model.predict(X_test)
39
+ y_pred_proba = model.predict_proba(X_test)[0]
40
+ return {'Positive': y_pred_proba[1], 'Negative': y_pred_proba[0]}
41
+
42
+ def predict_logistic(review_text, model=logistic_model):
43
+ X_test = vectorizer.transform([review_text])
44
+ y_pred = model.predict(X_test)
45
+ y_pred_proba = model.predict_proba(X_test)[0]
46
+ return {'Positive': y_pred_proba[1], 'Negative': y_pred_proba[0]}
47
+
48
+ models = ["KNN", "Logistic Regression"]
49
+
50
+ def predict(review_text, model):
51
+ if model == "KNN":
52
+ output = predict_knn(review_text)
53
+ else:
54
+ output = predict_logistic(review_text)
55
+
56
+ if output['Positive'] > output['Negative']:
57
+ sentiment = "Positive Feedback"
58
+ else:
59
+ sentiment = "Negative Feedback"
60
+
61
+ return sentiment, output
62
+
63
+ demo = gr.Interface(
64
+ fn=predict,
65
+ inputs=[
66
+ gr.Textbox(lines=2, placeholder="Enter your review comment...", label="Review Comment"),
67
+ gr.Dropdown(choices=models, label="Select Model")
68
+ ],
69
+ outputs=[
70
+ gr.Textbox(label="Predicted Sentiment Class"),
71
+ gr.Label(num_top_classes=2, label="Predicted Probability")
72
+ ],
73
+ examples=[
74
+ ["This Food is interesting, I need a second Plate", "KNN"],
75
+ ["This Food is interesting, I need a second Plate", "Logistic Regression"],
76
+ ["The food was terrible, and the service was worse.", "KNN"],
77
+ ["The food was terrible, and the service was worse.", "Logistic Regression"]
78
+ ]
79
+ )
80
+
81
+ demo.launch()