Spaces:
Starting
Starting
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 3 |
+
from sklearn.naive_bayes import MultinomialNB
|
| 4 |
+
|
| 5 |
+
# Training data
|
| 6 |
+
data = [
|
| 7 |
+
("I love this movie!", "positive"),
|
| 8 |
+
("This is terrible.", "negative"),
|
| 9 |
+
("What a great experience!", "positive"),
|
| 10 |
+
("I hate waiting in line.", "negative"),
|
| 11 |
+
("The weather is nice today.", "positive"),
|
| 12 |
+
("I'm so disappointed.", "negative"),
|
| 13 |
+
("It was okay, not great.", "negative"),
|
| 14 |
+
("Fantastic service!", "positive"),
|
| 15 |
+
("Worst day ever.", "negative"),
|
| 16 |
+
("Such a beautiful moment.", "positive"),
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
X = [sentence for sentence, label in data]
|
| 20 |
+
y = [label for sentence, label in data]
|
| 21 |
+
|
| 22 |
+
vectorizer = TfidfVectorizer()
|
| 23 |
+
X_vectorized = vectorizer.fit_transform(X)
|
| 24 |
+
|
| 25 |
+
model = MultinomialNB()
|
| 26 |
+
model.fit(X_vectorized, y)
|
| 27 |
+
|
| 28 |
+
# Prediction function
|
| 29 |
+
def predict_sentiment(text):
|
| 30 |
+
vector = vectorizer.transform([text])
|
| 31 |
+
prediction = model.predict(vector)[0]
|
| 32 |
+
if prediction == "positive":
|
| 33 |
+
return "β
POSITIVE π"
|
| 34 |
+
else:
|
| 35 |
+
return "β NEGATIVE π "
|
| 36 |
+
|
| 37 |
+
# Gradio Interface
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=predict_sentiment,
|
| 40 |
+
inputs=gr.Textbox(lines=3, placeholder="Type your sentence here..."),
|
| 41 |
+
outputs="text",
|
| 42 |
+
title="π¬ LM Studios Sentiment Detector",
|
| 43 |
+
description="Type something and see how it *feels*. This AI knows the tone of your message.",
|
| 44 |
+
theme="default",
|
| 45 |
+
flagging_mode="never",
|
| 46 |
+
live=False
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
demo.launch()
|