geethareddy commited on
Commit
d6f27d0
·
verified ·
1 Parent(s): e6f6322

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -51
app.py CHANGED
@@ -18,62 +18,26 @@ sf = Salesforce(
18
  security_token=os.getenv("SF_SECURITY_TOKEN")
19
  )
20
 
21
- def fetch_salesforce_data(supervisor_id, project_id):
22
  """
23
- Fetch supervisor and project data from Salesforce based on provided IDs.
24
-
25
- Args:
26
- supervisor_id (str): ID of the supervisor from Supervisor_Profile__c
27
- project_id (str): ID of the project from Project_Details__c
28
-
29
- Returns:
30
- tuple: (supervisor_data, project_data) as dictionaries
31
- """
32
- try:
33
- # Fetch supervisor data
34
- supervisor_query = f"SELECT Id, Role__c, Location__c FROM Supervisor_Profile__c WHERE Id = '{supervisor_id}'"
35
- supervisor_result = sf.query(supervisor_query)
36
- if not supervisor_result['records']:
37
- raise ValueError(f"No supervisor found with ID: {supervisor_id}")
38
- supervisor_data = supervisor_result['records'][0]
39
-
40
- # Fetch project data
41
- project_query = f"SELECT Id, Name, Start_Date__c, End_Date__c, Milestones__c, Project_Schedule__c FROM Project_Details__c WHERE Id = '{project_id}'"
42
- project_result = sf.query(project_query)
43
- if not project_result['records']:
44
- raise ValueError(f"No project found with ID: {project_id}")
45
- project_data = project_result['records'][0]
46
-
47
- # Remove Salesforce attributes we don't need
48
- supervisor_data = {k: v for k, v in supervisor_data.items() if not k.startswith('attributes')}
49
- project_data = {k: v for k, v in project_data.items() if not k.startswith('attributes')}
50
-
51
- return supervisor_data, project_data
52
-
53
- except Exception as e:
54
- raise Exception(f"Error fetching data from Salesforce: {str(e)}")
55
-
56
- def generate_ai_data(supervisor_id, project_id):
57
- """
58
- Generate AI coaching data and reports based on supervisor and project IDs.
59
 
60
  Args:
61
  supervisor_id (str): ID of the supervisor from Supervisor_Profile__c
62
  project_id (str): ID of the project from Project_Details__c
 
 
63
 
64
  Returns:
65
  dict: Status and generated data
66
  """
67
  try:
68
- # Fetch data from Salesforce
69
- supervisor_data, project_data = fetch_salesforce_data(supervisor_id, project_id)
70
-
71
  # Construct prompt for AI generation
72
  prompt = (
73
  f"Generate daily checklist, tips, risk alerts, upcoming milestones, and performance trends for a "
74
  f"{supervisor_data['Role__c']} at {supervisor_data['Location__c']} working on project "
75
- f"{project_data['Name']}. Project runs from {project_data['Start_Date__c']} to {project_data['End_Date__c']} "
76
- f"with milestones {project_data['Milestones__c']} and schedule {project_data['Project_Schedule__c']}."
77
  )
78
 
79
  # Generate AI output
@@ -92,7 +56,7 @@ def generate_ai_data(supervisor_id, project_id):
92
  "3. Schedule a team review."
93
  )
94
  risk_alerts = "Risk of delay: Rain expected on May 22, 2025."
95
- upcoming_milestones = project_data['Milestones__c'].split(';')[0] if project_data['Milestones__c'] else "No upcoming milestones"
96
  performance_trends = "Task completion rate: 75% this week (initial estimate)."
97
 
98
  # Save AI data to AI_Coaching_Data__c
@@ -122,8 +86,6 @@ def generate_ai_data(supervisor_id, project_id):
122
  return {
123
  "status": "success",
124
  "message": "AI data and report generated successfully",
125
- "supervisor_data": supervisor_data,
126
- "project_data": project_data,
127
  "ai_data": ai_data,
128
  "report_data": report_data
129
  }
@@ -138,15 +100,14 @@ def generate_ai_data(supervisor_id, project_id):
138
  iface = gr.Interface(
139
  fn=generate_ai_data,
140
  inputs=[
141
- gr.Textbox(label="Supervisor ID (from Supervisor_Profile__c)"),
142
- gr.Textbox(label="Project ID (from Project_Details__c)")
 
 
143
  ],
144
  outputs=gr.JSON(label="Result"),
145
  title="AI Coach Data Generator",
146
- description=(
147
- "Generate AI coaching data and reports based on supervisor and project details. "
148
- "Enter the Supervisor ID from Supervisor_Profile__c and Project ID from Project_Details__c."
149
- )
150
  )
151
 
152
  # Launch the Gradio app
 
18
  security_token=os.getenv("SF_SECURITY_TOKEN")
19
  )
20
 
21
+ def generate_ai_data(supervisor_id, project_id, supervisor_data, project_data):
22
  """
23
+ Generate AI coaching data and reports based on supervisor and project data.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  Args:
26
  supervisor_id (str): ID of the supervisor from Supervisor_Profile__c
27
  project_id (str): ID of the project from Project_Details__c
28
+ supervisor_data (dict): Contains Role__c, Location__c
29
+ project_data (dict): Contains Name, Start_Date__c, End_Date__c, Milestones__c, Project_Schedule__c
30
 
31
  Returns:
32
  dict: Status and generated data
33
  """
34
  try:
 
 
 
35
  # Construct prompt for AI generation
36
  prompt = (
37
  f"Generate daily checklist, tips, risk alerts, upcoming milestones, and performance trends for a "
38
  f"{supervisor_data['Role__c']} at {supervisor_data['Location__c']} working on project "
39
+ f"{project_data['Name']} with milestones {project_data['Milestones__c']} and schedule "
40
+ f"{project_data['Project_Schedule__c']}."
41
  )
42
 
43
  # Generate AI output
 
56
  "3. Schedule a team review."
57
  )
58
  risk_alerts = "Risk of delay: Rain expected on May 22, 2025."
59
+ upcoming_milestones = project_data['Milestones__c'].split(';')[0] # Take the first milestone
60
  performance_trends = "Task completion rate: 75% this week (initial estimate)."
61
 
62
  # Save AI data to AI_Coaching_Data__c
 
86
  return {
87
  "status": "success",
88
  "message": "AI data and report generated successfully",
 
 
89
  "ai_data": ai_data,
90
  "report_data": report_data
91
  }
 
100
  iface = gr.Interface(
101
  fn=generate_ai_data,
102
  inputs=[
103
+ gr.Textbox(label="Supervisor ID"),
104
+ gr.Textbox(label="Project ID"),
105
+ gr.JSON(label="Supervisor Data"),
106
+ gr.JSON(label="Project Data")
107
  ],
108
  outputs=gr.JSON(label="Result"),
109
  title="AI Coach Data Generator",
110
+ description="Generate AI coaching data and reports based on supervisor and project details."
 
 
 
111
  )
112
 
113
  # Launch the Gradio app