karthik55 commited on
Commit
e8c8b7d
·
verified ·
1 Parent(s): e67baf5

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py CHANGED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
+
4
+ # Load models
5
+ models = {
6
+ "Logistic Regression": joblib.load("models/best_model.joblib"),
7
+ "Random Forest": joblib.load("models/random_forest_model.joblib"),
8
+ "SVM (Linear)": joblib.load("models/svm_model_linear.joblib"),
9
+ "SVM (Polynomial)": joblib.load("models/svm_model_polynomial.joblib"),
10
+ "SVM (RBF)": joblib.load("models/svm_model_rbf.joblib"),
11
+ "KNN": joblib.load("models/trained_knn_model.joblib"),
12
+ }
13
+
14
+ # Define prediction function
15
+ def predict(review, model_name):
16
+ model = models[model_name]
17
+ prediction = model.predict([review])[0]
18
+ probabilities = model.predict_proba([review])[0]
19
+ return {
20
+ "Predicted Class": str(prediction),
21
+ "Class Probabilities": {
22
+ "Class 0": probabilities[0],
23
+ "Class 1": probabilities[1],
24
+ },
25
+ }
26
+
27
+ # Create Gradio interface
28
+ interface = gr.Interface(
29
+ fn=predict,
30
+ inputs=[
31
+ gr.Textbox(label="Review Comment"),
32
+ gr.Dropdown(choices=list(models.keys()), label="Model"),
33
+ ],
34
+ outputs=gr.JSON(label="Prediction Results"),
35
+ title="Text Classification Models",
36
+ description="Choose a model and provide a review to see the predicted sentiment class.",
37
+ )
38
+
39
+ # Launch the Gradio app
40
+ if __name__ == "__main__":
41
+ interface.launch()