Spaces:
Running
Running
| import gradio as gr | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| from sklearn.naive_bayes import MultinomialNB | |
| # Training data | |
| data = [ | |
| ("I love this movie!", "positive"), | |
| ("This is terrible.", "negative"), | |
| ("What a great experience!", "positive"), | |
| ("I hate waiting in line.", "negative"), | |
| ("The weather is nice today.", "positive"), | |
| ("I'm so disappointed.", "negative"), | |
| ("It was okay, not great.", "negative"), | |
| ("Fantastic service!", "positive"), | |
| ("Worst day ever.", "negative"), | |
| ("Such a beautiful moment.", "positive"), | |
| ] | |
| X = [sentence for sentence, label in data] | |
| y = [label for sentence, label in data] | |
| vectorizer = TfidfVectorizer() | |
| X_vectorized = vectorizer.fit_transform(X) | |
| model = MultinomialNB() | |
| model.fit(X_vectorized, y) | |
| # Prediction function | |
| def predict_sentiment(text): | |
| vector = vectorizer.transform([text]) | |
| prediction = model.predict(vector)[0] | |
| if prediction == "positive": | |
| return "β POSITIVE π" | |
| else: | |
| return "β NEGATIVE π " | |
| # Gradio Interface | |
| demo = gr.Interface( | |
| fn=predict_sentiment, | |
| inputs=gr.Textbox(lines=3, placeholder="Type your sentence here..."), | |
| outputs="text", | |
| title="π¬ LM Studios Sentiment Detector", | |
| description="Type something and see how it *feels*. This AI knows the tone of your message.", | |
| theme="default", | |
| flagging_mode="never", | |
| live=False | |
| ) | |
| demo.launch() | |