Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -95,7 +95,7 @@ def save_report_as_pdf(role, supervisor_name, project_id, checklist, suggestions
|
|
95 |
return temp_pdf_path, filename
|
96 |
|
97 |
# Upload to Salesforce and create a new record with the generated URL
|
98 |
-
def upload_pdf_to_salesforce_and_create_new_record(role, supervisor_name, project_id, pdf_path, pdf_name):
|
99 |
try:
|
100 |
# Connecting to Salesforce
|
101 |
sf = Salesforce(
|
@@ -195,6 +195,51 @@ def open_dashboard(role, supervisor_name, project_id):
|
|
195 |
url = generate_salesforce_dashboard_url(supervisor_name, project_id)
|
196 |
return f'<a href="{url}" target="_blank">Open Salesforce Dashboard</a>'
|
197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
# Gradio Interface
|
199 |
def create_interface():
|
200 |
roles = get_roles_from_salesforce()
|
|
|
95 |
return temp_pdf_path, filename
|
96 |
|
97 |
# Upload to Salesforce and create a new record with the generated URL
|
98 |
+
def upload_pdf_to_salesforce_and_create_new_record(role, supervisor_name, project_id, pdf_path, pdf_name, checklist, suggestions):
|
99 |
try:
|
100 |
# Connecting to Salesforce
|
101 |
sf = Salesforce(
|
|
|
195 |
url = generate_salesforce_dashboard_url(supervisor_name, project_id)
|
196 |
return f'<a href="{url}" target="_blank">Open Salesforce Dashboard</a>'
|
197 |
|
198 |
+
# Generate AI output and return the correct file for download
|
199 |
+
def generate_outputs(role, supervisor_name, project_id, milestones, reflection):
|
200 |
+
if not all([role, supervisor_name, project_id, milestones, reflection]):
|
201 |
+
return "❗ Please fill all fields.", "", None, ""
|
202 |
+
|
203 |
+
prompt = PROMPT_TEMPLATE.format(role=role, project_id=project_id, milestones=milestones, reflection=reflection)
|
204 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
205 |
+
try:
|
206 |
+
with torch.no_grad():
|
207 |
+
outputs = model.generate(
|
208 |
+
inputs['input_ids'],
|
209 |
+
max_new_tokens=150,
|
210 |
+
no_repeat_ngram_size=2,
|
211 |
+
do_sample=True,
|
212 |
+
top_p=0.9,
|
213 |
+
temperature=0.7,
|
214 |
+
pad_token_id=tokenizer.pad_token_id
|
215 |
+
)
|
216 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
217 |
+
except Exception as e:
|
218 |
+
print(f"⚠️ Generation error: {e}")
|
219 |
+
return "", "", None, ""
|
220 |
+
|
221 |
+
def extract_between(text, start, end):
|
222 |
+
s = text.find(start)
|
223 |
+
e = text.find(end, s) if end else len(text)
|
224 |
+
return text[s + len(start):e].strip() if s != -1 else ""
|
225 |
+
|
226 |
+
checklist = extract_between(result, "Checklist:\n", "Suggestions:")
|
227 |
+
suggestions = extract_between(result, "Suggestions:\n", None)
|
228 |
+
|
229 |
+
if not checklist.strip():
|
230 |
+
checklist = "- Perform daily safety inspection"
|
231 |
+
if not suggestions.strip():
|
232 |
+
suggestions = "- Monitor team coordination\n- Review safety protocols with the team"
|
233 |
+
|
234 |
+
pdf_path, pdf_name = save_report_as_pdf(role, supervisor_name, project_id, checklist, suggestions)
|
235 |
+
pdf_url = upload_pdf_to_salesforce_and_create_new_record(supervisor_name, project_id, pdf_path, pdf_name, checklist, suggestions)
|
236 |
+
|
237 |
+
if pdf_url:
|
238 |
+
suggestions += f"\n\n🔗 [Download PDF Report]({pdf_url})"
|
239 |
+
|
240 |
+
# Return the generated checklist, suggestions, and the path to the PDF
|
241 |
+
return checklist, suggestions, pdf_path, pdf_name
|
242 |
+
|
243 |
# Gradio Interface
|
244 |
def create_interface():
|
245 |
roles = get_roles_from_salesforce()
|