Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -186,6 +186,58 @@ def get_projects_for_supervisor(supervisor_name):
|
|
186 |
print(f"⚠️ Error fetching project: {e}")
|
187 |
return ""
|
188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
# Gradio Interface
|
190 |
def create_interface():
|
191 |
roles = get_roles_from_salesforce()
|
|
|
186 |
print(f"⚠️ Error fetching project: {e}")
|
187 |
return ""
|
188 |
|
189 |
+
# Generate Salesforce dashboard URL
|
190 |
+
def generate_salesforce_dashboard_url(supervisor_name, project_id):
|
191 |
+
return f"https://aicoachforsitesupervisors-dev-ed--c.develop.vf.force.com/apex/DashboardPage?supervisorName={supervisor_name}&projectId={project_id}"
|
192 |
+
|
193 |
+
def open_dashboard(role, supervisor_name, project_id):
|
194 |
+
url = generate_salesforce_dashboard_url(supervisor_name, project_id)
|
195 |
+
return f'<a href="{url}" target="_blank">Open Salesforce Dashboard</a>'
|
196 |
+
|
197 |
+
# Generate AI output
|
198 |
+
def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
|
199 |
+
if not all([role, supervisor_name, project_id, milestones, reflection]):
|
200 |
+
return "❗ Please fill all fields.", "", None, ""
|
201 |
+
|
202 |
+
prompt = PROMPT_TEMPLATE.format(role=role, project_id=project_id, milestones=milestones, reflection=reflection)
|
203 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
204 |
+
try:
|
205 |
+
with torch.no_grad():
|
206 |
+
outputs = model.generate(
|
207 |
+
inputs['input_ids'],
|
208 |
+
max_new_tokens=150,
|
209 |
+
no_repeat_ngram_size=2,
|
210 |
+
do_sample=True,
|
211 |
+
top_p=0.9,
|
212 |
+
temperature=0.7,
|
213 |
+
pad_token_id=tokenizer.pad_token_id
|
214 |
+
)
|
215 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
216 |
+
except Exception as e:
|
217 |
+
print(f"⚠️ Generation error: {e}")
|
218 |
+
return "", "", None, ""
|
219 |
+
|
220 |
+
def extract_between(text, start, end):
|
221 |
+
s = text.find(start)
|
222 |
+
e = text.find(end, s) if end else len(text)
|
223 |
+
return text[s + len(start):e].strip() if s != -1 else ""
|
224 |
+
|
225 |
+
checklist = extract_between(result, "Checklist:\n", "Suggestions:")
|
226 |
+
suggestions = extract_between(result, "Suggestions:\n", None)
|
227 |
+
|
228 |
+
if not checklist.strip():
|
229 |
+
checklist = "- Perform daily safety inspection"
|
230 |
+
if not suggestions.strip():
|
231 |
+
suggestions = "- Monitor team coordination\n- Review safety protocols with the team"
|
232 |
+
|
233 |
+
pdf_path, pdf_name = save_report_as_pdf(role, supervisor_name, project_id, checklist, suggestions)
|
234 |
+
pdf_url = upload_pdf_to_salesforce_and_update_link(supervisor_name, project_id, pdf_path, pdf_name)
|
235 |
+
if pdf_url:
|
236 |
+
suggestions += f"\n\n🔗 [Download PDF Report]({pdf_url})"
|
237 |
+
|
238 |
+
# Ensure that pdf_path is being passed correctly to gr.File component
|
239 |
+
return checklist, suggestions, pdf_path, f'<a href="{pdf_url}" target="_blank">Download PDF</a>' if pdf_url else ""
|
240 |
+
|
241 |
# Gradio Interface
|
242 |
def create_interface():
|
243 |
roles = get_roles_from_salesforce()
|