Spaces:
Sleeping
Sleeping
Download app.py from ksnkumar/nflredditdata: direct link, hf CLI and curl.
- Browser
- Download file 1.97 kB
-
https://huggingface.co/spaces/ksnkumar/nflredditdata/resolve/main/app.py
- Command line
-
hf download hf://spaces/ksnkumar/nflredditdata/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ksnkumar/nflredditdata/resolve/main/app.py
1.97 kB
| import pandas as pd | |
| import re | |
| import string | |
| import nltk | |
| import gradio as gr | |
| from nltk.corpus import stopwords | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| from sklearn.linear_model import LogisticRegression | |
| # Download resources | |
| nltk.download('stopwords') | |
| # Load data | |
| df = pd.read_csv("NFL_reddit_data_2021.csv") | |
| from textblob import TextBlob | |
| def get_sentiment(text): | |
| if not isinstance(text, str): | |
| text = str(text) | |
| return "positive" if TextBlob(text).sentiment.polarity > 0 else "negative" | |
| df["text"] = df["text"].astype("object").astype(str) | |
| df["sentiment"] = df["text"].apply(get_sentiment) | |
| df = df.dropna(subset=["text", "sentiment"]) | |
| # Text preprocessing | |
| stop_words = set(stopwords.words("english")) | |
| def clean_text(text): | |
| text = text.lower() | |
| text = re.sub(r"http\S+", "", text) | |
| text = re.sub(r"\d+", "", text) | |
| text = text.translate(str.maketrans("", "", string.punctuation)) | |
| words = text.split() | |
| words = [w for w in words if w not in stop_words] | |
| return " ".join(words) | |
| df["clean_text"] = df["text"].apply(clean_text) | |
| # Feature extraction | |
| vectorizer = TfidfVectorizer(max_features=5000) | |
| X = vectorizer.fit_transform(df["clean_text"]) | |
| y = df["sentiment"] | |
| # Train model | |
| model = LogisticRegression(max_iter=1000) | |
| model.fit(X, y) | |
| # Prediction function for Gradio | |
| def predict_sentiment(user_input): | |
| cleaned = clean_text(user_input) | |
| vectorized = vectorizer.transform([cleaned]) | |
| prediction = model.predict(vectorized)[0] | |
| return f"Predicted Sentiment: {prediction}" | |
| # Gradio Interface | |
| interface = gr.Interface( | |
| fn=predict_sentiment, | |
| inputs=gr.Textbox(lines=4, placeholder="Enter an NFL Reddit comment..."), | |
| outputs="text", | |
| title="NFL Reddit Sentiment Analyzer", | |
| description=( | |
| "Analyze sentiment of NFL-related Reddit comments using NLP. " | |
| "This tool demonstrates how sentiment analysis can support NFL Draft decisions." | |
| ) | |
| ) | |
| interface.launch() | |