Spaces:
Sleeping
Sleeping
Download app.py from Ashvinbari/NLP_app: direct link, hf CLI and curl.
- Browser
- Download file 1.33 kB
-
https://huggingface.co/spaces/Ashvinbari/NLP_app/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ashvinbari/NLP_app/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ashvinbari/NLP_app/resolve/main/app.py
1.33 kB
| # app.py | |
| import gradio as gr | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| from sklearn.naive_bayes import MultinomialNB | |
| # Sample documents and labels | |
| documents = [ | |
| "I love data science and machine learning", # Positive | |
| "Data science is fun and exciting", # Positive | |
| "Machine learning is a part of data science", # Positive | |
| "I hate dealing with messy data", # Negative | |
| "Data preprocessing is so boring", # Negative | |
| "I dislike coding errors and bugs" # Negative | |
| ] | |
| labels = [1, 1, 1, 0, 0, 0] # 1 = Positive, 0 = Negative | |
| # Train model | |
| vectorizer = TfidfVectorizer() | |
| X = vectorizer.fit_transform(documents) | |
| model = MultinomialNB() | |
| model.fit(X, labels) | |
| # Prediction function | |
| def predict_sentiment(text): | |
| X_new = vectorizer.transform([text]) | |
| prediction = model.predict(X_new)[0] | |
| return "Positive ๐" if prediction == 1 else "Negative ๐ " | |
| # Gradio interface | |
| interface = gr.Interface( | |
| fn=predict_sentiment, | |
| inputs=gr.Textbox(lines=2, placeholder="Enter your sentence here..."), | |
| outputs="text", | |
| title="Simple Test Sentiment Classifier (Beta)", | |
| description="Enter a sentence to classify it as Positive ๐ or Negative ๐ ." | |
| ) | |
| # Run the app | |
| if __name__ == "__main__": | |
| interface.launch() | |