Moditha24 commited on
Commit
48d8352
·
verified ·
1 Parent(s): ffa6264

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -46
app.py CHANGED
@@ -2,68 +2,64 @@ 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()
 
2
  import pickle
3
  import numpy as np
4
 
5
+ # Load all models and the vectorizer
6
+ with open("random_forest_model.pkl", "rb") as f:
7
+ random_forest_model = pickle.load(f)
8
 
9
+ with open("logistic_model.pkl", "rb") as f:
10
+ logistic_model = pickle.load(f)
11
 
12
+ with open("knn_yelp_model.pkl", "rb") as f:
13
+ knn_yelp_model = pickle.load(f)
14
 
15
+ with open("svm_linear.pkl", "rb") as f:
16
+ svm_linear = pickle.load(f)
17
 
18
+ with open("svm_poly.pkl", "rb") as f:
19
+ svm_poly = pickle.load(f)
20
 
21
+ with open("svm_rbf.pkl", "rb") as f:
22
+ svm_rbf = pickle.load(f)
23
 
24
+ with open("vectorizer.pkl", "rb") as f:
25
+ vectorizer = pickle.load(f)
 
 
 
 
 
 
 
26
 
27
  # Function to predict class probabilities
28
+ def predict_sentiment(review, model_name):
29
+ # Vectorize the input review
30
+ review_vec = vectorizer.transform([review])
 
 
 
 
 
31
 
32
  # Select the model based on user input
33
+ if model_name == 'Random Forest':
34
+ model = random_forest_model
35
+ elif model_name == 'Logistic Regression':
36
+ model = logistic_model
37
+ elif model_name == 'KNN':
38
+ model = knn_yelp_model
39
+ elif model_name == 'SVM Linear':
40
+ model = svm_linear
41
+ elif model_name == 'SVM Poly':
42
+ model = svm_poly
43
+ elif model_name == 'SVM RBF':
44
+ model = svm_rbf
45
 
46
+ # Predict probabilities for each class (assuming binary classification)
47
+ probabilities = model.predict_proba(review_vec)[0]
48
+ return {"Negative": probabilities[0], "Positive": probabilities[1]}
 
 
 
49
 
50
+ # Set up the Gradio interface
51
+ iface = gr.Interface(
52
+ fn=predict_sentiment,
53
  inputs=[
54
+ gr.Textbox(label="Enter Your Review"),
55
  gr.Dropdown(
56
+ choices=["Random Forest", "Logistic Regression", "KNN", "SVM Linear", "SVM Poly", "SVM RBF"],
57
+ label="Select Model"
58
+ ),
59
  ],
60
+ outputs=gr.Label(num_top_classes=2),
61
  live=True
62
  )
63
 
64
  # Launch the interface
65
+ iface.launch()