slyskawa commited on
Commit
6ae6997
1 Parent(s): 55022a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -19
app.py CHANGED
@@ -1,24 +1,24 @@
1
  import gradio as gr
2
  import joblib
3
 
4
- def predict_sentiment(review, model_type):
5
- if model_type == "Logistic Regression":
6
- model = joblib.load("logistic_pipeline.pkl")
7
- else: # KNN
8
- model = joblib.load("knn_pipeline.pkl")
9
-
10
- prediction = model.predict([review])[0]
11
- probabilities = model.predict_proba([review])[0]
12
- proba_positive = probabilities[1]
13
- result = f"Sentiment: {'Positive' if prediction else 'Negative'} (Confidence: {proba_positive:.2f} for Positive)"
14
- return result
15
 
16
- iface = gr.Interface(fn=predict_sentiment,
17
- inputs=[gr.Textbox(lines=2, label="Enter Review"),
18
- gr.Dropdown(choices=["Logistic Regression", "KNN"], label="Model Type")],
19
- outputs=gr.Textbox(label="Prediction"),
20
- title="Sentiment Analysis",
21
- description="Select a model to predict the sentiment of your review.")
22
 
23
- if __name__ == "__main__":
24
- iface.launch()
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import joblib
3
 
4
+ logistic_pipeline = joblib.load('logistic_pipeline.pkl')
5
+ knn_pipeline = joblib.load('knn_pipeline.pkl')
 
 
 
 
 
 
 
 
 
6
 
7
+ def predict_class_probabilities(text, model_choice):
8
+ if model_choice == "Logistic Regression":
9
+ probabilities = logistic_pipeline.predict_proba([text])[0]
10
+ else:
11
+ probabilities = knn_pipeline.predict_proba([text])[0]
12
+ return {'Positive': probabilities[1], 'Negative': probabilities[0]}
13
 
14
+ #Gradio interface
15
+ input_text = gr.Textbox(lines=5, label="Enter your review comment")
16
+ model_choice = gr.Dropdown(choices=["Logistic Regression", "KNN"], label="Choose a model")
17
+ output_probs = gr.Label()
18
+
19
+ iface = gr.Interface(fn=predict_class_probabilities,
20
+ inputs=[input_text, model_choice],
21
+ outputs=output_probs,
22
+ title="Sentiment Emotion Predictor",
23
+ description="Predict sentiment of review (positive or negative).")
24
+ iface.launch()