Spaces:
Sleeping
Sleeping
Download app.py from ksnkumar/sentimentanalysis: direct link, hf CLI and curl.
- Browser
- Download file 5.29 kB
-
https://huggingface.co/spaces/ksnkumar/sentimentanalysis/resolve/main/app.py
- Command line
-
hf download hf://spaces/ksnkumar/sentimentanalysis/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ksnkumar/sentimentanalysis/resolve/main/app.py
5.29 kB
| # gradio_sentiment_app.py | |
| import gradio as gr | |
| import pandas as pd | |
| from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer | |
| from sklearn.svm import SVC | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.model_selection import train_test_split | |
| from transformers import pipeline | |
| # ------------------------------ | |
| # Prepare Dataset | |
| # ------------------------------ | |
| data = [ | |
| ("I love this product, it's amazing!", "positive"), | |
| ("This is the worst experience ever.", "negative"), | |
| ("Absolutely fantastic! Highly recommend.", "positive"), | |
| ("Not good, really disappointing.", "negative"), | |
| ("I am extremely happy with this.", "positive"), | |
| ("Would never buy this again.", "negative"), | |
| ("It's okay, nothing special.", "negative"), | |
| ("Could be better but not bad.", "negative"), | |
| ("Oh wow, this product is just *so* amazing. Totally worth every penny... NOT!", "negative"), | |
| ("Yeah, sure, this was the best purchase of my life... said no one ever.", "negative"), | |
| ("If disappointment had a face, it would look like this product.", "negative"), | |
| ("Wow! I didn’t expect it to be this great. Oh wait, I did. It’s terrible!", "negative"), | |
| ("Oh sure, because everyone loves wasting money on garbage, right?", "negative"), | |
| ("Well, it works, I guess... if you're into that sort of thing.", "negative"), | |
| ("I can't believe how much I love this! Just kidding, it's awful.", "negative"), | |
| ("This product is life-changing! I can't imagine living without it!", "positive"), | |
| ("Absolutely horrible, I wouldn't even gift this to my worst enemy.", "negative"), | |
| ("Surprisingly good! Didn’t expect to love it this much.", "positive"), | |
| ("I regret every penny I spent on this.", "negative"), | |
| ("If you enjoy wasting money, this is perfect for you!", "negative"), | |
| ("It does the job. Nothing more, nothing less.", "positive"), | |
| ("Fantastic! Way beyond my expectations!", "positive"), | |
| ("I wouldn’t recommend this to my worst enemy.", "negative"), | |
| ("Superb quality, really impressed!", "positive"), | |
| ("How do I return this?", "negative"), | |
| ("Game-changer! I absolutely love it!", "positive"), | |
| ("Wow, this exceeded all my expectations!", "positive"), | |
| ("Disappointed but not surprised.", "negative"), | |
| ("They really outdid themselves. This is amazing!", "positive"), | |
| ("This is what I call a waste of money.", "negative"), | |
| ("Couldn’t be happier with my purchase!", "positive"), | |
| ("Just another product that failed to impress.", "negative"), | |
| ("Best decision I ever made!", "positive"), | |
| ("Literally the worst thing I’ve ever bought.", "negative"), | |
| ("Incredible value for money!", "positive"), | |
| ("One word: trash.", "negative"), | |
| ("I didn’t expect much, and I still got disappointed.", "negative") | |
| ] | |
| df = pd.DataFrame(data, columns=['text', 'sentiment']) | |
| # Split data | |
| X_train, X_test, y_train, y_test = train_test_split(df['text'], df['sentiment'], test_size=0.2, random_state=42) | |
| # ------------------------------ | |
| # Train Traditional ML Models | |
| # ------------------------------ | |
| # BoW + SVM | |
| vectorizer_bow = CountVectorizer() | |
| X_train_bow = vectorizer_bow.fit_transform(X_train) | |
| X_test_bow = vectorizer_bow.transform(X_test) | |
| svm_model = SVC(kernel='linear') | |
| svm_model.fit(X_train_bow, y_train) | |
| # TF-IDF + Logistic Regression | |
| vectorizer_tfidf = TfidfVectorizer() | |
| X_train_tfidf = vectorizer_tfidf.fit_transform(X_train) | |
| X_test_tfidf = vectorizer_tfidf.transform(X_test) | |
| lr_model = LogisticRegression() | |
| lr_model.fit(X_train_tfidf, y_train) | |
| # Transformer Model | |
| transformer_model = pipeline( | |
| "sentiment-analysis", | |
| model="distilbert-base-uncased-finetuned-sst-2-english", | |
| device=-1 # use CPU | |
| ) | |
| # ------------------------------ | |
| # Gradio Prediction Function | |
| # ------------------------------ | |
| def predict_sentiment(text, model_type): | |
| model_type = model_type.lower() | |
| if model_type == "svm": | |
| X_input = vectorizer_bow.transform([text]) | |
| pred = svm_model.predict(X_input)[0] | |
| elif model_type == "tfidf": | |
| X_input = vectorizer_tfidf.transform([text]) | |
| pred = lr_model.predict(X_input)[0] | |
| elif model_type == "transformer": | |
| result = transformer_model(text)[0] | |
| pred = 'positive' if result['label'] == 'POSITIVE' else 'negative' | |
| elif model_type == "prompt": | |
| prompt = f"Classify the sentiment of the following review as positive or negative: '{text}'" | |
| result = transformer_model(prompt)[0] | |
| pred = 'positive' if result['label'] == 'POSITIVE' else 'negative' | |
| else: | |
| pred = "Invalid model type" | |
| return pred | |
| # ------------------------------ | |
| # Gradio Interface | |
| # ------------------------------ | |
| model_choices = ["SVM", "TFIDF", "Transformer", "Prompt"] | |
| demo = gr.Interface( | |
| fn=predict_sentiment, | |
| inputs=[ | |
| gr.Textbox(label="Enter Product Review", placeholder="Type your review here..."), | |
| gr.Dropdown(label="Select Model", choices=model_choices) | |
| ], | |
| outputs=gr.Label(label="Predicted Sentiment"), | |
| title="Product Sentiment Analysis", | |
| description="A Gradio app to classify product reviews as positive or negative using different ML and Transformer models." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(share=True) | |