walkermanj commited on
Commit
0f442d1
Β·
verified Β·
1 Parent(s): e7e78c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -31
app.py CHANGED
@@ -1,35 +1,14 @@
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 😠"
@@ -40,10 +19,9 @@ demo = gr.Interface(
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()
 
1
  import gradio as gr
2
+ from transformers import pipeline
 
3
 
4
+ # Load the pretrained sentiment pipeline
5
+ sentiment_pipeline = pipeline("sentiment-analysis")
 
 
 
 
 
 
 
 
 
 
 
6
 
 
 
 
 
 
 
 
 
 
 
7
  def predict_sentiment(text):
8
+ result = sentiment_pipeline(text)[0]
9
+ label = result["label"]
10
+
11
+ if label == "POSITIVE":
12
  return "βœ… POSITIVE 😊"
13
  else:
14
  return "❌ NEGATIVE 😠"
 
19
  inputs=gr.Textbox(lines=3, placeholder="Type your sentence here..."),
20
  outputs="text",
21
  title="πŸ’¬ LM Studios Sentiment Detector",
22
+ description="Now powered by a Hugging Face transformer model for smarter predictions.",
23
  theme="default",
24
+ flagging_mode="never"
 
25
  )
26
 
27
  demo.launch()