arpit13 commited on
Commit
46bfe87
·
verified ·
1 Parent(s): 0a7bc28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -63
app.py CHANGED
@@ -1,95 +1,124 @@
1
  import gradio as gr
2
  import openai
3
- import os
4
 
5
  # Set OpenAI API Key
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 = ""
12
- if category == "Stress Management":
13
- system_message = "Provide soothing advice and tips to help the user manage stress. Be calm and empathetic."
14
- elif category == "Career Advice":
15
- system_message = "Provide professional and constructive career advice. Be encouraging and helpful."
16
- elif category == "General":
17
- system_message = "Provide general conversation. Be friendly and easygoing."
18
- elif category == "Friendly Buddy":
19
- system_message = "Respond as a supportive and fun friend. Be informal and light-hearted."
20
 
 
 
21
  try:
22
  response = openai.ChatCompletion.create(
23
  model="llama-3.1-70b-versatile",
24
  messages=[
25
- {"role": "system", "content": system_message},
26
- {"role": "user", "content": message}
27
  ]
28
  )
29
  return response.choices[0].message["content"]
30
  except Exception as e:
31
  return f"Error: {str(e)}"
32
 
33
- # Function to recognize the category based on keywords
34
- def recognize_category(user_input):
35
- stress_keywords = ["stress", "anxiety", "overwhelmed", "burnout", "calm"]
36
- career_keywords = ["career", "job", "work", "interview", "skills"]
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):
 
43
  return "Career Advice"
44
- elif any(keyword in user_input.lower() for keyword in general_keywords):
45
- return "General"
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,
91
- inputs=[user_input, history],
92
- outputs=[stress_column, career_column, general_column, friendly_column, history]
93
  )
94
 
95
- demo.launch()
 
1
  import gradio as gr
2
  import openai
 
3
 
4
  # Set OpenAI API Key
5
+ openai.api_key = "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"
6
  openai.api_base = "https://api.groq.com/openai/v1"
7
 
8
+ # Dictionary to store categorized chats
9
+ saved_chats = {
10
+ "Stress Management": [],
11
+ "Career Advice": [],
12
+ "General": [],
13
+ "Suggestions": []
14
+ }
 
 
 
 
15
 
16
+ # Function to get response from GROQ API
17
+ def get_groq_response(message):
18
  try:
19
  response = openai.ChatCompletion.create(
20
  model="llama-3.1-70b-versatile",
21
  messages=[
22
+ {"role": "user", "content": message},
23
+ {"role": "system", "content": "You will talk like a Motivational Speaker to help people come out of stress."}
24
  ]
25
  )
26
  return response.choices[0].message["content"]
27
  except Exception as e:
28
  return f"Error: {str(e)}"
29
 
30
+ # Function to classify messages based on the topic
31
+ def classify_message(user_message, bot_response):
32
+ if "stress" in user_message.lower():
33
+ saved_chats["Stress Management"].append((user_message, bot_response))
 
 
 
 
34
  return "Stress Management"
35
+ elif "career" in user_message.lower():
36
+ saved_chats["Career Advice"].append((user_message, bot_response))
37
  return "Career Advice"
38
+ elif "suggestions" in user_message.lower():
39
+ saved_chats["Suggestions"].append((user_message, bot_response))
40
+ return "Suggestions"
 
41
  else:
42
+ saved_chats["General"].append((user_message, bot_response))
43
+ return "General"
 
 
 
 
 
44
 
45
+ # Chatbot function
46
+ def chatbot(user_input, history=[]):
47
+ bot_response = get_groq_response(user_input)
48
+ topic = classify_message(user_input, bot_response)
49
+ history.append((f"({topic}) You: {user_input}", f"Motivator Bot: {bot_response}"))
50
+ return history, saved_chats
51
 
52
+ # Function to display saved chats
53
+ def display_saved_chats():
54
+ def format_chats(category):
55
+ return "\n".join([f"**You**: {u}\n**Bot**: {b}" for u, b in saved_chats[category]]) or "No messages yet."
56
+
57
  return (
58
+ format_chats("Stress Management"),
59
+ format_chats("Career Advice"),
60
+ format_chats("General"),
61
+ format_chats("Suggestions")
 
62
  )
63
 
64
+ # Gradio Interface setup
65
+ chat_interface = gr.Blocks(css="""
66
+ body {
67
+ font-family: 'Poppins', sans-serif;
68
+ background: linear-gradient(45deg, #ff9a9e, #fad0c4, #fbc2eb, #a1c4fd, #c2e9fb);
69
+ background-size: 400% 400%;
70
+ animation: gradientBG 10s ease infinite;
71
+ margin: 0;
72
+ padding: 0;
73
+ color: #333;
74
+ }
75
 
76
+ @keyframes gradientBG {
77
+ 0% { background-position: 0% 50%; }
78
+ 50% { background-position: 100% 50%; }
79
+ 100% { background-position: 0% 50%; }
80
+ }
81
 
82
+ header, footer {
83
+ text-align: center;
84
+ background: linear-gradient(90deg, #ff758c, #ff7eb3);
85
+ color: white;
86
+ padding: 1rem;
87
+ border-radius: 15px;
88
+ margin-bottom: 1rem;
89
+ box-shadow: 0px 4px 15px rgba(0, 0, 0, 0.2);
90
+ }
91
+ """)
92
+
93
+ with chat_interface:
94
+ with gr.Row():
95
+ gr.Markdown("<h1 style='text-align:center;'>🌈 Vibrant Motivational Chatbot</h1>")
96
  with gr.Row():
97
+ user_input = gr.Textbox(label="Your Message", placeholder="Type something...")
98
+ send_button = gr.Button("Send")
99
+ with gr.Row():
100
+ chatbot_output = gr.Chatbot(label="Chat History")
101
 
102
+ with gr.Row():
103
+ with gr.Column():
104
+ stress_display = gr.Textbox(label="Stress Management", interactive=False, lines=10)
105
+ with gr.Column():
106
+ career_display = gr.Textbox(label="Career Advice", interactive=False, lines=10)
107
+ with gr.Column():
108
+ general_display = gr.Textbox(label="General", interactive=False, lines=10)
109
+ with gr.Column():
110
+ suggestions_display = gr.Textbox(label="Suggestions", interactive=False, lines=10)
111
+
112
+ def handle_interaction(user_input, history):
113
+ if not user_input.strip():
114
+ return history, *display_saved_chats()
115
+ updated_history, _ = chatbot(user_input, history)
116
+ return updated_history, *display_saved_chats()
117
 
118
+ send_button.click(
119
+ fn=handle_interaction,
120
+ inputs=[user_input, chatbot_output],
121
+ outputs=[chatbot_output, stress_display, career_display, general_display, suggestions_display]
122
  )
123
 
124
+ chat_interface.launch()