Will Phoenix commited on
Commit
2d064d4
·
1 Parent(s): d410d6f
Homework2_WillPhoenix.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib, numpy as np
3
+ from pathlib import Path
4
+
5
+ VECTORIZER = joblib.load("vectorizer.joblib")
6
+ CANDIDATES = ["knn_model", "svm_model_rbf", "random_forest_model", "logistic_model"]
7
+ models = {}
8
+
9
+ for name in CANDIDATES:
10
+ p = Path(f"/content/{name}.joblib")
11
+ if p.exists():
12
+ models[name] = joblib.load(p)
13
+
14
+ if not models:
15
+ raise RuntimeError("No models found. Put joblib models in ./content/")
16
+
17
+ LABELS = ["Negative Comment", "Positive Comment"]
18
+
19
+ def predict(text, model_name):
20
+ if not text.strip():
21
+ return ""
22
+
23
+ X = VECTORIZER.transform([text])
24
+ model = models[model_name]
25
+ y_pred = model.predict(X)[0]
26
+
27
+ if y_pred == 1:
28
+ return "Positive Feedback"
29
+ else:
30
+ return "Negative Feedback"
31
+
32
+ with gr.Blocks() as demo:
33
+ gr.Markdown("## Sentiment Demo")
34
+ with gr.Row():
35
+ with gr.Column():
36
+ txt = gr.Textbox(label="Review Comment", lines=6)
37
+ mdl = gr.Dropdown(
38
+ choices=list(models.keys()),
39
+ value=next(iter(models.keys())),
40
+ label="method"
41
+ )
42
+ btn = gr.Button("Submit")
43
+ with gr.Column():
44
+ pred = gr.Textbox(label="Predicted Sentiment Class")
45
+
46
+ btn.click(predict, inputs=[txt, mdl], outputs=[pred])
47
+
48
+ if __name__ == "__main__":
49
+ demo.launch()
knn_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ff17ffa4dd20a6fa3d54fb3c57d35c4416d10a2789cb3ce520757e5539cae48e
3
+ size 54420
logistic_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:99d29d5a0903db0be1467ccbf1d82a1ae320d09a9077c1bfd2b0408a3d3fac7a
3
+ size 14079
random_forest_model.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b378fee1ae527962bcdfe467cf6a264590463928b966cedd56015c650cfd5ca2
3
+ size 11940329
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio>=4.0
2
+ scikit-learn
3
+ pandas
4
+ numpy
5
+ joblib
svm_model_rbf.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f938b74161c76d3b641eb5e86ca42b38cf4d252b340b6282b508a394b1d684a2
3
+ size 60875
vectorizer.joblib ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fce7f3eb812e116442d2ca3fa8a18d962f0fdb03406a62843af9f8b0dc8d45b9
3
+ size 20032