geethareddy commited on
Commit
7a86e2f
·
verified ·
1 Parent(s): 8af7dc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -85
app.py CHANGED
@@ -111,75 +111,17 @@ def field_exists(sf, object_name, field_name):
111
  print(f"⚠️ Error checking field {field_name}: {e}")
112
  return False
113
 
114
- def save_to_salesforce(role, project_id, milestones, reflection, checklist, suggestions, supervisor_name):
115
- try:
116
- sf = Salesforce(
117
- username=os.getenv('SF_USERNAME'),
118
- password=os.getenv('SF_PASSWORD'),
119
- security_token=os.getenv('SF_SECURITY_TOKEN'),
120
- domain=os.getenv('SF_DOMAIN', 'login')
121
- )
122
- supervisor_result = sf.query(f"SELECT Id FROM Supervisor__c WHERE Name = '{supervisor_name}' LIMIT 1")
123
- if supervisor_result['totalSize'] == 0:
124
- print("❌ No supervisor found.")
125
- return
126
- supervisor_id = supervisor_result['records'][0]['Id']
127
- project_result = sf.query(f"SELECT Id FROM Project__c WHERE Name = '{project_id}' LIMIT 1")
128
- if project_result['totalSize'] == 0:
129
- print("❌ No project found.")
130
- return
131
- project_record_id = project_result['records'][0]['Id']
132
-
133
- data = {
134
- 'Supervisor_ID__c': supervisor_id,
135
- 'Project_ID__c': project_record_id,
136
- 'Daily_Checklist__c': checklist[:255],
137
- 'Suggested_Tips__c': suggestions[:255],
138
- 'Reflection_Log__c': reflection[:255],
139
- 'KPI_Flag__c': KPI_FLAG_DEFAULT,
140
- 'Engagement_Score__c': ENGAGEMENT_SCORE_DEFAULT
141
- }
142
-
143
- if field_exists(sf, 'Supervisor_AI_Coaching__c', 'Milestones_KPIs__c'):
144
- data['Milestones_KPIs__c'] = milestones[:255]
145
-
146
- sf.Supervisor_AI_Coaching__c.create(data)
147
- print("✅ Record created in Salesforce.")
148
- except Exception as e:
149
- print(f"❌ Error saving to Salesforce: {e}")
150
-
151
- # Fallback generation for checklist and suggestions
152
- def generate_fallback_checklist(role, milestones):
153
- checklist_items = []
154
- if role.lower() == "safety officer":
155
- checklist_items.append("- Conduct morning safety briefing")
156
- checklist_items.append("- Review incident reports from previous day")
157
-
158
- if milestones and milestones.strip():
159
- kpis = [kpi.strip() for kpi in milestones.split(",")]
160
- for kpi in kpis:
161
- checklist_items.append(f"- Ensure progress on {kpi}")
162
- else:
163
- checklist_items.append("- Perform daily safety inspection")
164
-
165
- return "\n".join(checklist_items)
166
-
167
- def generate_fallback_suggestions(reflection):
168
- suggestions_items = []
169
- reflection_lower = reflection.lower()
170
- if "student" in reflection_lower or "learning" in reflection_lower:
171
- suggestions_items.append("- Ensure students are logging incidents consistently")
172
- suggestions_items.append("- Provide guidance on timely incident recording")
173
- if "incident" in reflection_lower:
174
- suggestions_items.append("- Follow up on reported incidents with corrective actions")
175
-
176
- if not suggestions_items:
177
- suggestions_items.append("- Monitor team coordination")
178
- suggestions_items.append("- Review safety protocols with the team")
179
-
180
- return "\n".join(suggestions_items)
181
-
182
- # Generate Function
183
  def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
184
  if not all([role, supervisor_name, project_id, milestones, reflection]):
185
  return "❗ Please fill all fields.", ""
@@ -206,10 +148,7 @@ def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
206
  result = tokenizer.decode(outputs[0], skip_special_tokens=True)
207
  except Exception as e:
208
  print(f"⚠️ Generation error: {e}")
209
- checklist = generate_fallback_checklist(role, milestones)
210
- suggestions = generate_fallback_suggestions(reflection)
211
- save_to_salesforce(role, project_id, milestones, reflection, checklist, suggestions, supervisor_name)
212
- return checklist, suggestions
213
 
214
  def extract_between(text, start, end):
215
  s = text.find(start)
@@ -219,19 +158,8 @@ def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
219
  checklist = extract_between(result, "Checklist:\n", "Suggestions:")
220
  suggestions = extract_between(result, "Suggestions:\n", None)
221
 
222
- if not checklist.strip():
223
- checklist = generate_fallback_checklist(role, milestones)
224
- if not suggestions.strip():
225
- suggestions = generate_fallback_suggestions(reflection)
226
-
227
- save_to_salesforce(role, project_id, milestones, reflection, checklist, suggestions, supervisor_name)
228
  return checklist, suggestions
229
 
230
- # Dashboard button function
231
- def open_dashboard():
232
- salesforce_dashboard_url = "https://your-salesforce-instance.lightning.force.com/lightning/page/home"
233
- return f'<a href="{salesforce_dashboard_url}" target="_blank" rel="noopener noreferrer" style="font-size:16px;">Open Salesforce Dashboard</a>'
234
-
235
  # Interface
236
  def create_interface():
237
  roles = get_roles_from_salesforce()
@@ -268,7 +196,7 @@ def create_interface():
268
 
269
  refresh.click(fn=lambda: gr.update(choices=get_roles_from_salesforce()), outputs=role)
270
 
271
- dashboard_btn.click(fn=open_dashboard, inputs=None, outputs=dashboard_link)
272
 
273
  return demo
274
 
 
111
  print(f"⚠️ Error checking field {field_name}: {e}")
112
  return False
113
 
114
+ # New function to generate Salesforce dashboard URL
115
+ def generate_salesforce_dashboard_url(supervisor_name, project_id):
116
+ return f"https://your-salesforce-instance.lightning.force.com/lightning/r/Project__c/{project_id}/view?supervisorName={supervisor_name}"
117
+
118
+ # Dashboard button function
119
+ def open_dashboard(role, supervisor_name, project_id):
120
+ # Generate dynamic URL based on supervisor and project
121
+ dashboard_url = generate_salesforce_dashboard_url(supervisor_name, project_id)
122
+ return f'<a href="{dashboard_url}" target="_blank" rel="noopener noreferrer" style="font-size:16px;">Open Salesforce Dashboard</a>'
123
+
124
+ # Generate function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
126
  if not all([role, supervisor_name, project_id, milestones, reflection]):
127
  return "❗ Please fill all fields.", ""
 
148
  result = tokenizer.decode(outputs[0], skip_special_tokens=True)
149
  except Exception as e:
150
  print(f"⚠️ Generation error: {e}")
151
+ return "", ""
 
 
 
152
 
153
  def extract_between(text, start, end):
154
  s = text.find(start)
 
158
  checklist = extract_between(result, "Checklist:\n", "Suggestions:")
159
  suggestions = extract_between(result, "Suggestions:\n", None)
160
 
 
 
 
 
 
 
161
  return checklist, suggestions
162
 
 
 
 
 
 
163
  # Interface
164
  def create_interface():
165
  roles = get_roles_from_salesforce()
 
196
 
197
  refresh.click(fn=lambda: gr.update(choices=get_roles_from_salesforce()), outputs=role)
198
 
199
+ dashboard_btn.click(fn=open_dashboard, inputs=[role, supervisor_name, project_id], outputs=dashboard_link)
200
 
201
  return demo
202