NLP_app / app.py
Ashvinbari's picture
Update app.py
adc8d92 verified
Raw History Blame Contribute Delete
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()