Madiharehan commited on
Commit
fa5913d
Β·
verified Β·
1 Parent(s): e9f4ddd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -32
app.py CHANGED
@@ -1,56 +1,36 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load the pre-trained model (using the provided model or custom trained model)
5
  def load_model():
6
- # Use Hugging Face's 'cardiffnlp/twitter-roberta-base-sentiment' or a custom model path
7
  return pipeline('sentiment-analysis', model='cardiffnlp/twitter-roberta-base-sentiment')
8
 
9
- # Initialize the model
10
  sentiment_model = load_model()
11
 
12
- # Function to analyze sentiment and provide motivational feedback
13
  def analyze_sentiment(user_input):
14
- # Get sentiment prediction
15
- result = sentiment_model(user_input)
16
-
17
- if not result:
18
- return "Could not determine sentiment. Please try again."
19
 
20
- sentiment = result[0]['label'].lower() # Extract sentiment label and convert to lowercase for comparison
21
- print(f"Sentiment Analysis Result: {result}") # Debug: Print model result for review
22
-
23
- # Analyze the mood and provide motivational messages accordingly
24
  if sentiment == 'negative':
25
- return (
26
- "Mood Detected: Negative πŸ˜”\n\n"
27
- "Stay positive! 🌟 Remember, tough times don't last, but tough people do!"
28
- )
29
  elif sentiment == 'neutral':
30
- return (
31
- "Mood Detected: Neutral 😐\n\n"
32
- "It's good to reflect on steady days. Keep your goals in mind, and stay motivated!"
33
- )
34
  elif sentiment == 'positive':
35
- return (
36
- "Mood Detected: Positive 😊\n\n"
37
- "You're on the right track! Keep shining! 🌞"
38
- )
39
  else:
40
- return (
41
- "Mood Detected: Unknown πŸ€”\n\n"
42
- "Keep going, you're doing great!"
43
- )
44
 
45
  # Gradio UI
46
  def chatbot_ui():
47
- # Define the Gradio interface
48
  interface = gr.Interface(
49
  fn=analyze_sentiment,
50
- inputs=gr.Textbox(label="Enter your text here:", placeholder="Type your feelings or thoughts..."),
51
  outputs=gr.Textbox(label="Motivational Message"),
52
  title="Student Sentiment Analysis Chatbot",
53
- description="This chatbot detects your mood and provides positive or motivational messages based on sentiment analysis."
54
  )
55
 
56
  return interface
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load the pre-trained model (cached for performance)
5
  def load_model():
 
6
  return pipeline('sentiment-analysis', model='cardiffnlp/twitter-roberta-base-sentiment')
7
 
 
8
  sentiment_model = load_model()
9
 
10
+ # Define the function to analyze sentiment
11
  def analyze_sentiment(user_input):
12
+ result = sentiment_model(user_input)[0]
13
+ sentiment = result['label'].lower() # Convert to lowercase for easier comparison
 
 
 
14
 
15
+ # Customize messages based on detected sentiment
 
 
 
16
  if sentiment == 'negative':
17
+ return "Mood Detected: Negative πŸ˜”\n\nStay positive! 🌟 Remember, tough times don't last, but tough people do!"
 
 
 
18
  elif sentiment == 'neutral':
19
+ return "Mood Detected: Neutral 😐\n\nIt's good to reflect on steady days. Keep your goals in mind, and stay motivated!"
 
 
 
20
  elif sentiment == 'positive':
21
+ return "Mood Detected: Positive 😊\n\nYou're on the right track! Keep shining! 🌞"
 
 
 
22
  else:
23
+ return "Mood Detected: Unknown πŸ€”\n\nKeep going, you're doing great!"
 
 
 
24
 
25
  # Gradio UI
26
  def chatbot_ui():
27
+ # Define the interface
28
  interface = gr.Interface(
29
  fn=analyze_sentiment,
30
+ inputs=gr.Textbox(label="Enter your text here:"),
31
  outputs=gr.Textbox(label="Motivational Message"),
32
  title="Student Sentiment Analysis Chatbot",
33
+ description="This chatbot detects your mood and provides positive or motivational messages."
34
  )
35
 
36
  return interface