arpit13 commited on
Commit
db6c1b8
·
verified ·
1 Parent(s): 84eaa5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -8
app.py CHANGED
@@ -6,7 +6,7 @@ import os
6
  openai.api_key = os.getenv("TRY_NEW_THINGS")
7
  openai.api_base = "https://api.groq.com/openai/v1"
8
 
9
- # Function to get response from GROQ API
10
  def get_groq_response(message, category):
11
  # System message to tailor responses based on the category
12
  system_message = ""
@@ -31,24 +31,68 @@ def get_groq_response(message, category):
31
  except Exception as e:
32
  return f"Error: {str(e)}"
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  # Chatbot function
35
- def chatbot(user_input, category, history=[]):
 
36
  bot_response = get_groq_response(user_input, category)
37
- history.append((user_input, bot_response))
38
  return history, history
39
 
40
- # Gradio Interface setup with a vertical layout for category
41
  chat_interface = gr.Interface(
42
  fn=chatbot,
43
  inputs=[
44
- gr.Dropdown(choices=["Stress Management", "Career Advice", "General", "Friendly Buddy"], label="Choose Chat Category"),
45
- gr.Textbox(label="Your Message"), # Text input for user message
46
  "state" # History
47
  ],
48
  outputs=["chatbot", "state"],
49
- live=False,
50
  title="Meet your Personal Assistant: Your Helpful and Caring Chatbot",
51
- description="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. Whether you’re seeking information or just someone to chat with, our chatbot is designed to make you feel heard and supported. It’s more than just a tool—it’s a companion dedicated to making your day easier and more enjoyable."
 
52
  )
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  chat_interface.launch()
 
6
  openai.api_key = os.getenv("TRY_NEW_THINGS")
7
  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 = ""
 
31
  except Exception as e:
32
  return f"Error: {str(e)}"
33
 
34
+ # Function to recognize the category based on keywords
35
+ def recognize_category(user_input):
36
+ stress_keywords = ["stress", "anxiety", "overwhelmed", "burnout", "calm"]
37
+ career_keywords = ["career", "job", "work", "interview", "skills"]
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):
45
+ return "Career Advice"
46
+ elif any(keyword in user_input.lower() for keyword in general_keywords):
47
+ return "General"
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, history
59
 
60
+ # Gradio Interface setup with a vertical layout for category responses
61
  chat_interface = gr.Interface(
62
  fn=chatbot,
63
  inputs=[
64
+ gr.Textbox(label="Your Message"), # User input
 
65
  "state" # History
66
  ],
67
  outputs=["chatbot", "state"],
68
+ live=True,
69
  title="Meet your Personal Assistant: Your Helpful and Caring Chatbot",
70
+ description="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. Whether you’re seeking information or just someone to chat with, our chatbot is designed to make you feel heard and supported. It’s more than just a tool—it’s a companion dedicated to making your day easier and more enjoyable.",
71
+ layout="vertical"
72
  )
73
 
74
+ # Custom layout to show the outputs under different vertical columns based on categories
75
+ def custom_output(history):
76
+ stress_responses = []
77
+ career_responses = []
78
+ general_responses = []
79
+ friendly_responses = []
80
+
81
+ for category, user_input, bot_response in history:
82
+ if category == "Stress Management":
83
+ stress_responses.append(f"**User**: {user_input}\n**Bot**: {bot_response}")
84
+ elif category == "Career Advice":
85
+ career_responses.append(f"**User**: {user_input}\n**Bot**: {bot_response}")
86
+ elif category == "General":
87
+ general_responses.append(f"**User**: {user_input}\n**Bot**: {bot_response}")
88
+ elif category == "Friendly Buddy":
89
+ friendly_responses.append(f"**User**: {user_input}\n**Bot**: {bot_response}")
90
+
91
+ return [
92
+ gr.Column([gr.Markdown(f"### Stress Management\n{response}") for response in stress_responses]),
93
+ gr.Column([gr.Markdown(f"### Career Advice\n{response}") for response in career_responses]),
94
+ gr.Column([gr.Markdown(f"### General\n{response}") for response in general_responses]),
95
+ gr.Column([gr.Markdown(f"### Friendly Buddy\n{response}") for response in friendly_responses])
96
+ ]
97
+
98
  chat_interface.launch()