File size: 1,175 Bytes
51a42a9
053528a
c28fc08
d92a05d
c28fc08
7535cdd
51a42a9
74f810f
c28fc08
 
 
51a42a9
c28fc08
51a42a9
c28fc08
 
 
 
 
 
 
 
51a42a9
c28fc08
51a42a9
c28fc08
51a42a9
c28fc08
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import gradio as gr
import pickle
from sklearn.feature_extraction.text import TfidfVectorizer

# Load the model and vectorizer from the pickle file
filename = 'sentiment_model.pkl'

with open(filename, 'rb') as file:
    loaded_objects = pickle.load(file)
    nb_classifier = loaded_objects['model']  # Trained model
    vectorizer = loaded_objects['vectorizer']  # Pre-trained vectorizer

# Define the prediction function
def predict_sentiment(text_input):
    try:
        text_vector = vectorizer.transform([text_input])  # Transform input text
        prediction = nb_classifier.predict(text_vector)  # Predict sentiment
        return "Positive" if prediction[0] == 1 else "Negative"
    except Exception as e:
        return f"Error: {e}"

# Create the Gradio interface
with gr.Blocks(theme="compact") as demo:
    gr.Markdown("## Sentiment Analysis Predictor")
    with gr.Row():
        text_input = gr.Textbox(label="Write the Review", placeholder="Enter your sentiment")
        output_box = gr.Textbox(label="Sentiment Prediction")
    text_input.submit(fn=predict_sentiment, inputs=text_input, outputs=output_box)

# Launch the Gradio app
demo.launch(share=True)