Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,6 @@ from sklearn.feature_extraction.text import CountVectorizer
|
|
| 9 |
from sklearn.naive_bayes import MultinomialNB
|
| 10 |
import asyncio
|
| 11 |
from crewai import Agent as CrewAgent, Task, Crew
|
| 12 |
-
import autogen
|
| 13 |
import openai
|
| 14 |
|
| 15 |
# Set up logging
|
|
@@ -32,6 +31,20 @@ except Exception as e:
|
|
| 32 |
logger.error(f"Failed to initialize InferenceClient: {e}")
|
| 33 |
sys.exit(1)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Guardrail functions
|
| 36 |
def sanitize_input(input_text):
|
| 37 |
return re.sub(r'[<>&\']', '', input_text)
|
|
@@ -65,23 +78,73 @@ def check_confidence(response):
|
|
| 65 |
uncertain_phrases = ["I'm not sure", "It's possible", "I don't have enough information"]
|
| 66 |
return not any(phrase.lower() in response.lower() for phrase in uncertain_phrases)
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
try:
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
-
logger.error(f"Error
|
| 73 |
-
return "I apologize, but
|
| 74 |
-
|
| 75 |
-
def post_process_response(response):
|
| 76 |
-
response = re.sub(r'\b(stupid|dumb|idiotic|foolish)\b', 'mistaken', response, flags=re.IGNORECASE)
|
| 77 |
-
|
| 78 |
-
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):
|
| 79 |
-
response += "\n\nIs there anything else I can help you with regarding Zerodha's services?"
|
| 80 |
-
|
| 81 |
-
if re.search(r'\b(invest|trade|buy|sell|market)\b', response, re.IGNORECASE):
|
| 82 |
-
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."
|
| 83 |
-
|
| 84 |
-
return response
|
| 85 |
|
| 86 |
# Gradio interface setup
|
| 87 |
demo = gr.ChatInterface(
|
|
|
|
| 9 |
from sklearn.naive_bayes import MultinomialNB
|
| 10 |
import asyncio
|
| 11 |
from crewai import Agent as CrewAgent, Task, Crew
|
|
|
|
| 12 |
import openai
|
| 13 |
|
| 14 |
# Set up logging
|
|
|
|
| 31 |
logger.error(f"Failed to initialize InferenceClient: {e}")
|
| 32 |
sys.exit(1)
|
| 33 |
|
| 34 |
+
# Shared context for agents
|
| 35 |
+
SHARED_CONTEXT = """You are part of a multi-agent system designed to provide respectful, empathetic, and accurate support for Zerodha, a leading Indian financial services company. Your role is crucial in ensuring all interactions uphold the highest standards of customer service while maintaining Zerodha's excellent reputation.
|
| 36 |
+
|
| 37 |
+
Key points about Zerodha:
|
| 38 |
+
1. India's largest discount broker, known for innovative technology and low-cost trading.
|
| 39 |
+
2. Flat fee structure: ₹20 per executed order for intraday and F&O trades, zero brokerage for delivery equity investments.
|
| 40 |
+
3. Main trading platform: Kite (web and mobile).
|
| 41 |
+
4. Coin platform for commission-free direct mutual fund investments.
|
| 42 |
+
5. Extensive educational resources through Varsity.
|
| 43 |
+
6. Additional tools: Sentinel (price alerts) and ChartIQ (advanced charting).
|
| 44 |
+
7. Console for account management and administrative tasks.
|
| 45 |
+
|
| 46 |
+
Always prioritize user safety, ethical investing practices, and transparent communication. Never provide information that could mislead users or bring disrepute to Zerodha."""
|
| 47 |
+
|
| 48 |
# Guardrail functions
|
| 49 |
def sanitize_input(input_text):
|
| 50 |
return re.sub(r'[<>&\']', '', input_text)
|
|
|
|
| 78 |
uncertain_phrases = ["I'm not sure", "It's possible", "I don't have enough information"]
|
| 79 |
return not any(phrase.lower() in response.lower() for phrase in uncertain_phrases)
|
| 80 |
|
| 81 |
+
# Agent setup using CrewAI only
|
| 82 |
+
communication_expert_crew = CrewAgent(
|
| 83 |
+
role='Communication Expert',
|
| 84 |
+
goal='Interpret and rephrase user queries with empathy and respect',
|
| 85 |
+
backstory=SHARED_CONTEXT,
|
| 86 |
+
verbose=True,
|
| 87 |
+
allow_delegation=False
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
response_expert_crew = CrewAgent(
|
| 91 |
+
role='Response Expert',
|
| 92 |
+
goal='Provide accurate, helpful, and emotionally intelligent responses to user queries',
|
| 93 |
+
backstory=SHARED_CONTEXT,
|
| 94 |
+
verbose=True,
|
| 95 |
+
allow_delegation=False
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# Main function for handling user queries
|
| 99 |
+
async def zerodha_support(message, history):
|
| 100 |
try:
|
| 101 |
+
sanitized_message = sanitize_input(message)
|
| 102 |
+
|
| 103 |
+
if not is_relevant_topic(sanitized_message):
|
| 104 |
+
return "I'm sorry, but I can only assist with queries related to Zerodha's services and trading. Could you please ask a question about your Zerodha account, trading, or our platforms?"
|
| 105 |
+
|
| 106 |
+
sanitized_message = redact_sensitive_info(sanitized_message)
|
| 107 |
+
|
| 108 |
+
# CrewAI for rephrasing query
|
| 109 |
+
rephrase_task = Task(
|
| 110 |
+
description=f"Rephrase the following user query with empathy and respect: '{sanitized_message}'",
|
| 111 |
+
agent=communication_expert_crew
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
crew = Crew(
|
| 115 |
+
agents=[communication_expert_crew],
|
| 116 |
+
tasks=[rephrase_task],
|
| 117 |
+
verbose=2
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
rephrased_query = await crew.kickoff()
|
| 121 |
+
|
| 122 |
+
# CrewAI for generating the response
|
| 123 |
+
response_task = Task(
|
| 124 |
+
description=f"Generate a comprehensive and empathetic response to: '{rephrased_query}'",
|
| 125 |
+
agent=response_expert_crew
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
response_crew = Crew(
|
| 129 |
+
agents=[response_expert_crew],
|
| 130 |
+
tasks=[response_task],
|
| 131 |
+
verbose=2
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
response = await response_crew.kickoff()
|
| 135 |
+
|
| 136 |
+
if not check_response_content(response):
|
| 137 |
+
response += "\n\nPlease note that I cannot provide specific investment advice or guarantee returns. For personalized guidance, please consult with a qualified financial advisor."
|
| 138 |
+
|
| 139 |
+
if not check_confidence(response):
|
| 140 |
+
return "I apologize, but I'm not confident in providing an accurate answer to this query. For the most up-to-date and accurate information, please contact Zerodha's customer support directly."
|
| 141 |
+
|
| 142 |
+
final_response = post_process_response(response)
|
| 143 |
+
|
| 144 |
+
return final_response
|
| 145 |
except Exception as e:
|
| 146 |
+
logger.error(f"Error in zerodha_support: {e}")
|
| 147 |
+
return "I apologize, but an error occurred while processing your request. Please try again later."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
# Gradio interface setup
|
| 150 |
demo = gr.ChatInterface(
|