Spaces:
Sleeping
Sleeping
| 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) | |