Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from joblib import load | |
| # Define function to load SVM model | |
| def load_svm_model(model_url): | |
| return load(model_url) | |
| # Load the SVM model from Hugging Face | |
| svm_model = load_svm_model("https://huggingface.co/ankitdotpy/SVM_model_by_Group12") | |
| # Define function for Gradio interface using the loaded model | |
| def predict_sentiment(text): | |
| # Load the SVM model | |
| clf_svm = svm_model | |
| # Vectorize the input text (assuming you already have the vectorizer) | |
| text_vector = vectorizer.transform([text]) | |
| # Predict sentiment | |
| sentiment = clf_svm.predict(text_vector)[0] | |
| # Get probabilities for each class | |
| probabilities = clf_svm.predict_proba(text_vector)[0] | |
| # Convert probabilities to percentages | |
| percentages = [round(prob * 100, 2) for prob in probabilities] | |
| # Choose the sentiment label based on the predicted class | |
| if sentiment == "POSITIVE": | |
| return f"Positive ({percentages[1]}%)" | |
| elif sentiment == "NEUTRAL": | |
| return f"Neutral ({percentages[2]}%)" | |
| else: | |
| return f"Negative ({percentages[0]}%)" | |
| # Create Gradio interface | |
| iface = gr.Interface( | |
| fn=predict_sentiment, | |
| inputs=gr.Textbox(placeholder="Enter Text", lines=10, label="Enter your text here:"), | |
| outputs=gr.Textbox(label="Sentiment"), | |
| title="Sentiment Analysis using Hugging Face Model", | |
| description="Enter text and predict sentiment" | |
| ) | |
| iface.launch() | |