PD03 commited on
Commit
02bfbfc
·
verified ·
1 Parent(s): b7b1f98

Create agent/rica_agent.py

Browse files
Files changed (1) hide show
  1. agent/rica_agent.py +72 -0
agent/rica_agent.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ RICA Agent optimized for Hugging Face Spaces
3
+ """
4
+
5
+ import os
6
+ from smolagents import CodeAgent
7
+ from openai import OpenAI
8
+ from agent_tools.ml_tools import predict_customer_churn_hf, get_model_status
9
+
10
+ def create_rica_agent_hf():
11
+ """Create RICA agent optimized for HF Spaces"""
12
+
13
+ # HF Spaces optimized tools
14
+ hf_tools = [
15
+ predict_customer_churn_hf,
16
+ get_model_status
17
+ ]
18
+
19
+ try:
20
+ agent = CodeAgent(
21
+ tools=hf_tools,
22
+ model=OpenAI(api_key=os.getenv("OPENAI_API_KEY")),
23
+ add_base_tools=False,
24
+ max_iterations=3 # Reduced for HF Spaces performance
25
+ )
26
+ return agent
27
+ except Exception as e:
28
+ raise Exception(f"Agent creation failed: {str(e)}")
29
+
30
+ def execute_rica_analysis_hf(analysis_type: str, parameters: dict = None):
31
+ """Execute RICA analysis optimized for HF Spaces"""
32
+
33
+ agent = create_rica_agent_hf()
34
+
35
+ # Simplified goals for HF Spaces
36
+ hf_goals = {
37
+ "comprehensive": f"""
38
+ Execute business intelligence analysis:
39
+ 1) Check model status with get_model_status()
40
+ 2) Predict customer churn with predict_customer_churn_hf()
41
+ 3) Provide executive summary with key insights and recommendations
42
+
43
+ Focus on actionable insights for business decision-making.
44
+ Parameters: {parameters}
45
+ """,
46
+
47
+ "churn_focus": f"""
48
+ Focus on customer churn analysis:
49
+ 1) Predict customer churn with predict_customer_churn_hf(risk_threshold={parameters.get('risk_threshold', 0.6)})
50
+ 2) Identify high-risk customers requiring immediate attention
51
+ 3) Provide specific intervention strategies
52
+
53
+ Parameters: {parameters}
54
+ """,
55
+
56
+ "quick_insights": f"""
57
+ Provide quick business insights:
58
+ 1) Get model status with get_model_status()
59
+ 2) Run limited churn analysis with predict_customer_churn_hf()
60
+ 3) Summarize top 3 business priorities
61
+
62
+ Parameters: {parameters}
63
+ """
64
+ }
65
+
66
+ goal = hf_goals.get(analysis_type, hf_goals["comprehensive"])
67
+
68
+ try:
69
+ result = agent.run(goal)
70
+ return result
71
+ except Exception as e:
72
+ return f"Analysis failed: {str(e)}. Please check your API key and try again."