arpit13 commited on
Commit
0a7bc28
·
verified ·
1 Parent(s): 91166b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -48
app.py CHANGED
@@ -8,7 +8,6 @@ openai.api_base = "https://api.groq.com/openai/v1"
8
 
9
  # Function to get response from GROQ API based on category keywords
10
  def get_groq_response(message, category):
11
- # System message to tailor responses based on the category
12
  system_message = ""
13
  if category == "Stress Management":
14
  system_message = "Provide soothing advice and tips to help the user manage stress. Be calm and empathetic."
@@ -38,7 +37,6 @@ def recognize_category(user_input):
38
  general_keywords = ["hello", "hi", "how", "what", "why", "chat", "talk"]
39
  friendly_keywords = ["friend", "buddy", "hangout", "fun", "relax"]
40
 
41
- # Check for category based on keywords
42
  if any(keyword in user_input.lower() for keyword in stress_keywords):
43
  return "Stress Management"
44
  elif any(keyword in user_input.lower() for keyword in career_keywords):
@@ -48,62 +46,45 @@ def recognize_category(user_input):
48
  elif any(keyword in user_input.lower() for keyword in friendly_keywords):
49
  return "Friendly Buddy"
50
  else:
51
- return "General" # Default category if no keywords match
52
 
53
- # Chatbot function
54
- def chatbot(user_input, history=[]):
55
- category = recognize_category(user_input) # Determine the category based on user input
56
  bot_response = get_groq_response(user_input, category)
57
- history.append((category, user_input, bot_response)) # Save category, user input, and bot response
58
- return history
59
 
60
- # Gradio Interface setup with Blocks for custom layout
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  with gr.Blocks() as demo:
62
- gr.Markdown("## Meet your Personal Assistant: Your Helpful and Caring Chatbot")
63
- gr.Markdown("This chatbot is here to help you with anything you need, whether it’s answering questions, offering support, or guiding you through tasks. With a friendly and empathetic approach, it listens carefully to your concerns and provides thoughtful, understanding responses.")
64
 
65
  with gr.Row():
66
  user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
67
  submit_button = gr.Button("Submit")
68
-
69
- history = gr.State([])
70
- stress_column = gr.Column()
71
- career_column = gr.Column()
72
- general_column = gr.Column()
73
- friendly_column = gr.Column()
74
-
75
- output_area = gr.Row([stress_column, career_column, general_column, friendly_column])
76
 
77
- def process_input(user_input, history):
78
- category = recognize_category(user_input)
79
- bot_response = get_groq_response(user_input, category)
80
- history.append((category, user_input, bot_response))
81
-
82
- # Organize responses by category
83
- stress_responses = []
84
- career_responses = []
85
- general_responses = []
86
- friendly_responses = []
87
-
88
- for cat, user_msg, bot_msg in history:
89
- response = f"**User**: {user_msg}\n**Bot**: {bot_msg}"
90
- if cat == "Stress Management":
91
- stress_responses.append(response)
92
- elif cat == "Career Advice":
93
- career_responses.append(response)
94
- elif cat == "General":
95
- general_responses.append(response)
96
- elif cat == "Friendly Buddy":
97
- friendly_responses.append(response)
98
 
99
- # Update the columns
100
- return {
101
- stress_column: "\n\n".join(stress_responses) or "No responses yet.",
102
- career_column: "\n\n".join(career_responses) or "No responses yet.",
103
- general_column: "\n\n".join(general_responses) or "No responses yet.",
104
- friendly_column: "\n\n".join(friendly_responses) or "No responses yet.",
105
- history: history
106
- }
107
 
108
  submit_button.click(
109
  fn=process_input,
 
8
 
9
  # Function to get response from GROQ API based on category keywords
10
  def get_groq_response(message, category):
 
11
  system_message = ""
12
  if category == "Stress Management":
13
  system_message = "Provide soothing advice and tips to help the user manage stress. Be calm and empathetic."
 
37
  general_keywords = ["hello", "hi", "how", "what", "why", "chat", "talk"]
38
  friendly_keywords = ["friend", "buddy", "hangout", "fun", "relax"]
39
 
 
40
  if any(keyword in user_input.lower() for keyword in stress_keywords):
41
  return "Stress Management"
42
  elif any(keyword in user_input.lower() for keyword in career_keywords):
 
46
  elif any(keyword in user_input.lower() for keyword in friendly_keywords):
47
  return "Friendly Buddy"
48
  else:
49
+ return "General" # Default category
50
 
51
+ # Function to process input and generate category-specific responses
52
+ def process_input(user_input, history):
53
+ category = recognize_category(user_input)
54
  bot_response = get_groq_response(user_input, category)
55
+ history.append((category, user_input, bot_response))
 
56
 
57
+ # Group responses by category
58
+ stress_responses = [f"**User**: {u}\n**Bot**: {b}" for c, u, b in history if c == "Stress Management"]
59
+ career_responses = [f"**User**: {u}\n**Bot**: {b}" for c, u, b in history if c == "Career Advice"]
60
+ general_responses = [f"**User**: {u}\n**Bot**: {b}" for c, u, b in history if c == "General"]
61
+ friendly_responses = [f"**User**: {u}\n**Bot**: {b}" for c, u, b in history if c == "Friendly Buddy"]
62
+
63
+ # Return grouped responses for display
64
+ return (
65
+ "\n\n".join(stress_responses) or "No responses yet.",
66
+ "\n\n".join(career_responses) or "No responses yet.",
67
+ "\n\n".join(general_responses) or "No responses yet.",
68
+ "\n\n".join(friendly_responses) or "No responses yet.",
69
+ history
70
+ )
71
+
72
+ # Gradio UI with Blocks
73
  with gr.Blocks() as demo:
74
+ gr.Markdown("## Meet Your Personal Assistant: A Caring Chatbot")
75
+ gr.Markdown("This chatbot helps with stress management, career advice, general queries, or just being a friendly buddy!")
76
 
77
  with gr.Row():
78
  user_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
79
  submit_button = gr.Button("Submit")
 
 
 
 
 
 
 
 
80
 
81
+ with gr.Row():
82
+ stress_column = gr.Textbox(label="Stress Management", interactive=False)
83
+ career_column = gr.Textbox(label="Career Advice", interactive=False)
84
+ general_column = gr.Textbox(label="General", interactive=False)
85
+ friendly_column = gr.Textbox(label="Friendly Buddy", interactive=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
+ history = gr.State([])
 
 
 
 
 
 
 
88
 
89
  submit_button.click(
90
  fn=process_input,