invincible-jha commited on
Commit
c100279
·
verified ·
1 Parent(s): fef1896

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -18
app.py CHANGED
@@ -9,7 +9,6 @@ from sklearn.naive_bayes import MultinomialNB
9
  import asyncio
10
  from crewai import Agent
11
  from huggingface_hub import InferenceClient
12
- import random
13
  import json
14
  import warnings
15
  from typing import Literal
@@ -63,11 +62,20 @@ class CommunicationExpertAgent(Agent):
63
  sanitized_query = re.sub(r'[<>&\']', '', query)
64
  topic_relevance = classifier.predict(vectorizer.transform([sanitized_query]))[0] in range(len(approved_topics))
65
  if not topic_relevance:
66
- return "Query not relevant to our services."
67
- emotional_context = "Identified emotional context"
68
- rephrased_query = f"Rephrased with empathy: {sanitized_query} - {emotional_context}"
69
  return rephrased_query
70
 
 
 
 
 
 
 
 
 
 
71
  class ResponseExpertAgent(Agent):
72
  role: Literal["Response Expert"] = "Response Expert"
73
  goal: Literal["To provide accurate, helpful, and emotionally intelligent responses to user queries"] = "To provide accurate, helpful, and emotionally intelligent responses to user queries"
@@ -77,14 +85,11 @@ class ResponseExpertAgent(Agent):
77
  async def run(self, rephrased_query):
78
  try:
79
  logger.info(f"Sending query for generation: {rephrased_query}")
80
- response = await hf_client.text_generation(rephrased_query, max_new_tokens=500, temperature=0.7)
81
- if 'generated_text' in response:
82
- return response['generated_text']
83
- else:
84
- return "Failed to retrieve generated text from response."
85
  except Exception as e:
86
  logger.error(f"Failed to generate text due to: {str(e)}")
87
- return "Error in generating response. Please try again."
88
 
89
  class PostprocessingAgent(Agent):
90
  role: Literal["Postprocessing Expert"] = "Postprocessing Expert"
@@ -93,7 +98,18 @@ class PostprocessingAgent(Agent):
93
  "You are responsible for finalizing communications, adding polite terminations, and ensuring that the responses meet the quality standards expected in customer interactions."
94
 
95
  def run(self, response):
96
- response += "\n\nThank you for contacting Zerodha. Is there anything else we can help with?"
 
 
 
 
 
 
 
 
 
 
 
97
  return response
98
 
99
  # Instantiate agents
@@ -102,26 +118,50 @@ response_expert = ResponseExpertAgent()
102
  postprocessing_agent = PostprocessingAgent()
103
 
104
  async def handle_query(query):
105
- rephrased_query = await communication_expert.run(query)
106
- response = await response_expert.run(rephrased_query)
107
- final_response = postprocessing_agent.run(response)
108
- return final_response
 
 
 
 
 
 
 
109
 
110
  # Gradio interface setup
111
  def setup_interface():
112
  with gr.Blocks() as app:
 
 
 
113
  with gr.Row():
114
- query_input = gr.Textbox(label="Enter your query")
115
  submit_button = gr.Button("Submit")
116
- response_output = gr.Textbox(label="Response")
 
 
117
  submit_button.click(
118
  fn=lambda x: asyncio.run(handle_query(x)),
119
  inputs=[query_input],
120
  outputs=[response_output]
121
  )
 
 
 
 
 
 
 
 
 
 
 
 
122
  return app
123
 
124
  app = setup_interface()
125
 
126
  if __name__ == "__main__":
127
- app.launch()
 
9
  import asyncio
10
  from crewai import Agent
11
  from huggingface_hub import InferenceClient
 
12
  import json
13
  import warnings
14
  from typing import Literal
 
62
  sanitized_query = re.sub(r'[<>&\']', '', query)
63
  topic_relevance = classifier.predict(vectorizer.transform([sanitized_query]))[0] in range(len(approved_topics))
64
  if not topic_relevance:
65
+ return "I apologize, but your query doesn't seem to be related to Zerodha's services. Could you please ask about account opening, trading, fees, our platforms, funds, regulations, or customer support?"
66
+ emotional_context = self.analyze_emotional_context(sanitized_query)
67
+ rephrased_query = f"Rephrased query with empathy: {sanitized_query}\nEmotional context: {emotional_context}"
68
  return rephrased_query
69
 
70
+ def analyze_emotional_context(self, query):
71
+ # This is a placeholder. In a real scenario, you'd use sentiment analysis or a more sophisticated method.
72
+ if any(word in query.lower() for word in ['frustrated', 'angry', 'upset']):
73
+ return "The user seems frustrated or upset."
74
+ elif any(word in query.lower() for word in ['confused', 'unclear', 'don\'t understand']):
75
+ return "The user seems confused or seeking clarification."
76
+ else:
77
+ return "The user's emotional state is neutral or unclear."
78
+
79
  class ResponseExpertAgent(Agent):
80
  role: Literal["Response Expert"] = "Response Expert"
81
  goal: Literal["To provide accurate, helpful, and emotionally intelligent responses to user queries"] = "To provide accurate, helpful, and emotionally intelligent responses to user queries"
 
85
  async def run(self, rephrased_query):
86
  try:
87
  logger.info(f"Sending query for generation: {rephrased_query}")
88
+ response = hf_client.text_generation(rephrased_query, max_new_tokens=500, temperature=0.7)
89
+ return response
 
 
 
90
  except Exception as e:
91
  logger.error(f"Failed to generate text due to: {str(e)}")
92
+ return "I apologize, but I'm having trouble generating a response at the moment. Please try again or contact Zerodha support directly if the issue persists."
93
 
94
  class PostprocessingAgent(Agent):
95
  role: Literal["Postprocessing Expert"] = "Postprocessing Expert"
 
98
  "You are responsible for finalizing communications, adding polite terminations, and ensuring that the responses meet the quality standards expected in customer interactions."
99
 
100
  def run(self, response):
101
+ processed_response = self.add_disclaimers(response)
102
+ processed_response = self.ensure_politeness(processed_response)
103
+ return processed_response
104
+
105
+ def add_disclaimers(self, response):
106
+ if any(word in response.lower() for word in ['invest', 'trade', 'buy', 'sell', 'market']):
107
+ response += "\n\nPlease note that this information is for educational purposes only and should not be considered as financial advice. Always do your own research and consider consulting with a qualified financial advisor before making investment decisions."
108
+ return response
109
+
110
+ def ensure_politeness(self, response):
111
+ if not re.search(r'(Thank you|Is there anything else|Hope this helps|Let me know if you need more information)\s*$', response, re.IGNORECASE):
112
+ response += "\n\nThank you for choosing Zerodha. Is there anything else I can assist you with today?"
113
  return response
114
 
115
  # Instantiate agents
 
118
  postprocessing_agent = PostprocessingAgent()
119
 
120
  async def handle_query(query):
121
+ try:
122
+ if not query.strip():
123
+ return "I'm sorry, but I didn't receive any query. Could you please ask a question about Zerodha's services?"
124
+
125
+ rephrased_query = await communication_expert.run(query)
126
+ response = await response_expert.run(rephrased_query)
127
+ final_response = postprocessing_agent.run(response)
128
+ return final_response
129
+ except Exception as e:
130
+ logger.error(f"Error in handle_query: {str(e)}")
131
+ return "I apologize, but an error occurred while processing your request. Please try again or contact Zerodha support if the issue persists."
132
 
133
  # Gradio interface setup
134
  def setup_interface():
135
  with gr.Blocks() as app:
136
+ gr.Markdown("# Zerodha Support Chatbot")
137
+ gr.Markdown("Ask questions about Zerodha's services, trading, account management, and more.")
138
+
139
  with gr.Row():
140
+ query_input = gr.Textbox(label="Enter your query", placeholder="Type your question here...")
141
  submit_button = gr.Button("Submit")
142
+
143
+ response_output = gr.Textbox(label="Response", lines=10)
144
+
145
  submit_button.click(
146
  fn=lambda x: asyncio.run(handle_query(x)),
147
  inputs=[query_input],
148
  outputs=[response_output]
149
  )
150
+
151
+ gr.Examples(
152
+ examples=[
153
+ "How do I open a Zerodha account?",
154
+ "What are the brokerage charges for intraday trading?",
155
+ "Can you explain how to use the Kite platform?",
156
+ "I'm having trouble logging into my account. What should I do?",
157
+ "What are the margin requirements for F&O trading?"
158
+ ],
159
+ inputs=[query_input]
160
+ )
161
+
162
  return app
163
 
164
  app = setup_interface()
165
 
166
  if __name__ == "__main__":
167
+ app.launch()