Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,31 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pickle
|
| 3 |
-
|
| 4 |
-
import pandas as pd
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# Specify the filename for your PKL file
|
| 10 |
filename = 'sentiment_model.pkl'
|
| 11 |
|
| 12 |
-
# Open the file in write binary ('wb') mode and save the model using pickle.dump()
|
| 13 |
with open(filename, 'rb') as file:
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
|
|
|
| 16 |
def predict_sentiment(text_input):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
with gr.Blocks(theme="compact") as demo:
|
| 24 |
-
gr.Markdown("
|
| 25 |
-
#chatbot_window = gr.Chatbot(label="")
|
| 26 |
with gr.Row():
|
| 27 |
-
|
| 28 |
-
text_input = gr.Textbox(label="Write the Review", placeholder="Enter your sentiment", show_label=False)
|
| 29 |
-
# Create a Textbox to display the output
|
| 30 |
output_box = gr.Textbox(label="Sentiment Prediction")
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pickle
|
| 3 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
|
|
| 4 |
|
| 5 |
+
# Load the model and vectorizer from the pickle file
|
|
|
|
|
|
|
|
|
|
| 6 |
filename = 'sentiment_model.pkl'
|
| 7 |
|
|
|
|
| 8 |
with open(filename, 'rb') as file:
|
| 9 |
+
loaded_objects = pickle.load(file)
|
| 10 |
+
nb_classifier = loaded_objects['model'] # Trained model
|
| 11 |
+
vectorizer = loaded_objects['vectorizer'] # Pre-trained vectorizer
|
| 12 |
|
| 13 |
+
# Define the prediction function
|
| 14 |
def predict_sentiment(text_input):
|
| 15 |
+
try:
|
| 16 |
+
text_vector = vectorizer.transform([text_input]) # Transform input text
|
| 17 |
+
prediction = nb_classifier.predict(text_vector) # Predict sentiment
|
| 18 |
+
return "Positive" if prediction[0] == 1 else "Negative"
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"Error: {e}"
|
| 21 |
+
|
| 22 |
+
# Create the Gradio interface
|
| 23 |
with gr.Blocks(theme="compact") as demo:
|
| 24 |
+
gr.Markdown("## Sentiment Analysis Predictor")
|
|
|
|
| 25 |
with gr.Row():
|
| 26 |
+
text_input = gr.Textbox(label="Write the Review", placeholder="Enter your sentiment")
|
|
|
|
|
|
|
| 27 |
output_box = gr.Textbox(label="Sentiment Prediction")
|
| 28 |
+
text_input.submit(fn=predict_sentiment, inputs=text_input, outputs=output_box)
|
| 29 |
+
|
| 30 |
+
# Launch the Gradio app
|
| 31 |
+
demo.launch(share=True)
|