Moditha24 commited on
Commit
413d647
·
verified ·
1 Parent(s): 806a282

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pickle
3
+ import numpy as np
4
+
5
+ # Load all the models
6
+ with open('random_forest_model.pkl', 'rb') as file:
7
+ random_forest_model = pickle.load(file)
8
+
9
+ with open('logistic_model.pkl', 'rb') as file:
10
+ logistic_model = pickle.load(file)
11
+
12
+ with open('knn_yelp_model.pkl', 'rb') as file:
13
+ knn_yelp_model = pickle.load(file)
14
+
15
+ with open('svm_linear.pkl', 'rb') as file:
16
+ svm_linear = pickle.load(file)
17
+
18
+ with open('svm_poly.pkl', 'rb') as file:
19
+ svm_poly = pickle.load(file)
20
+
21
+ with open('svm_rbf.pkl', 'rb') as file:
22
+ svm_rbf = pickle.load(file)
23
+
24
+ # Store models in a dictionary for easy access
25
+ models = {
26
+ "Random Forest": random_forest_model,
27
+ "Logistic Regression": logistic_model,
28
+ "KNN": knn_yelp_model,
29
+ "SVM Linear": svm_linear,
30
+ "SVM Polynomial": svm_poly,
31
+ "SVM RBF": svm_rbf
32
+ }
33
+
34
+ # Function to predict class probabilities
35
+ def predict_class_probabilities(review_text, model_name):
36
+ # Convert review text to the format the model expects (using vectorizer)
37
+ # Assuming you have a vectorizer stored as 'vectorizer.pkl'
38
+ with open('vectorizer.pkl', 'rb') as file:
39
+ vectorizer = pickle.load(file)
40
+
41
+ # Transform the review text into a vector
42
+ review_vector = vectorizer.transform([review_text])
43
+
44
+ # Select the model based on user input
45
+ model = models.get(model_name)
46
+
47
+ if model:
48
+ # Predict class probabilities
49
+ prob = model.predict_proba(review_vector)
50
+ return prob
51
+ else:
52
+ return "Model not found."
53
+
54
+ # Gradio Interface
55
+ interface = gr.Interface(
56
+ fn=predict_class_probabilities,
57
+ inputs=[
58
+ gr.Textbox(label="Enter your review", placeholder="Type a review here..."),
59
+ gr.Dropdown(
60
+ label="Select a Model",
61
+ choices=["Random Forest", "Logistic Regression", "KNN", "SVM Linear", "SVM Polynomial", "SVM RBF"]
62
+ )
63
+ ],
64
+ outputs="json",
65
+ live=True
66
+ )
67
+
68
+ # Launch the interface
69
+ interface.launch()