Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import joblib | |
| # Load model and vectorizer | |
| model = joblib.load("sentiment_model.pkl") | |
| vectorizer = joblib.load("vectorizer.pkl") | |
| # Prediction function | |
| def predict_sentiment(text): | |
| text_vector = vectorizer.transform([text]) | |
| prediction = model.predict(text_vector)[0] | |
| proba = model.predict_proba(text_vector)[0] | |
| sentiment = "Positive" if prediction == 1 else "Negative" | |
| return { | |
| "Sentiment": sentiment, | |
| "Positive Probability": round(proba[1], 2), | |
| "Negative Probability": round(proba[0], 2), | |
| } | |
| # Gradio UI | |
| iface = gr.Interface( | |
| fn=predict_sentiment, | |
| inputs=gr.Textbox(label="Enter your review or message"), | |
| outputs=[ | |
| gr.Label(label="Sentiment"), | |
| gr.Label(label="Positive Probability"), | |
| gr.Label(label="Negative Probability"), | |
| ], | |
| title="Sentiment Analysis", | |
| description="Enter a sentence to check if it's Positive or Negative.", | |
| ) | |
| iface.launch() | |