VaneshDev commited on
Commit
94b1f36
Β·
verified Β·
1 Parent(s): 51c091f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -20
app.py CHANGED
@@ -5,6 +5,8 @@ import uuid
5
  from pathlib import Path
6
  import requests
7
  import gradio as gr
 
 
8
  from simple_salesforce import Salesforce
9
 
10
  # Configure logging
@@ -20,7 +22,6 @@ SALESFORCE_USERNAME = "vaneshdevarapalli866@agentforce.com"
20
  SALESFORCE_PASSWORD = "vanesh@331"
21
  SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
22
 
23
- # Hugging Face API Endpoint (replace with your actual endpoint)
24
  HUGGINGFACE_API_URL = "https://your-huggingface-model-endpoint"
25
 
26
  # Connect to Salesforce
@@ -53,20 +54,25 @@ project_choices = [
53
 
54
  ai_suggestion_choices = ["Move", "Pause Rent", "Repair", "Replace"]
55
 
56
- # Generate report link (simulate report as HTML file)
57
- def generate_report_link(record_id, data_dict):
58
  report_id = str(uuid.uuid4())[:8]
59
- report_filename = f"report_{report_id}.html"
60
  report_path = Path(f"reports/{report_filename}")
61
  report_path.parent.mkdir(exist_ok=True)
62
 
63
- with open(report_path, "w") as f:
64
- f.write(f"<h1>Equipment Utilization Report</h1>")
65
- f.write(f"<p>Salesforce Record ID: {record_id}</p>")
66
- for key, val in data_dict.items():
67
- f.write(f"<p><strong>{key}</strong>: {val}</p>")
68
 
69
- return f"https://yourdomain.com/reports/{report_filename}"
 
 
 
 
 
 
70
 
71
  # Call Hugging Face AI model
72
  def call_ai_model(usage_hours, idle_hours, movement_frequency, cost_per_hour, last_maintenance_str):
@@ -99,7 +105,6 @@ def call_ai_model(usage_hours, idle_hours, movement_frequency, cost_per_hour, la
99
  return "No Action", 0.0, 0.0
100
 
101
  # Main process
102
-
103
  def process_equipment_utilization(equipment_name, project_name, usage_hours, idle_hours,
104
  movement_frequency, cost_per_hour, report_link, last_maintenance, ai_suggestion):
105
  last_maintenance_str = last_maintenance.strftime('%Y-%m-%d') if last_maintenance else None
@@ -120,7 +125,7 @@ def process_equipment_utilization(equipment_name, project_name, usage_hours, idl
120
  "AI_Suggestion__c": ai_suggestion,
121
  "Suggestion_Confidence__c": suggestion_confidence * 100,
122
  "Utilization_Score__c": utilization_score * 100,
123
- "Report_Link__c": report_link.strip() if report_link.strip() else "N/A",
124
  "Last_Maintenance__c": last_maintenance_str,
125
  "Dashboard_Flag__c": False
126
  }
@@ -130,7 +135,6 @@ def process_equipment_utilization(equipment_name, project_name, usage_hours, idl
130
  record_id = response.get("id")
131
  logger.info(f"Salesforce record created: {record_id}")
132
 
133
- # Now generate report and update the record with the actual link
134
  summary_data = {
135
  "Equipment Name": equipment_name,
136
  "Project": project_name,
@@ -140,9 +144,9 @@ def process_equipment_utilization(equipment_name, project_name, usage_hours, idl
140
  "Confidence": suggestion_confidence,
141
  "Utilization Score": utilization_score
142
  }
143
- generated_report_link = generate_report_link(record_id, summary_data)
144
 
145
- sf.Equipment_Utilization_Record__c.update(record_id, {"Report_Link__c": generated_report_link})
146
 
147
  return {
148
  "Salesforce_Record_Id": record_id,
@@ -150,7 +154,8 @@ def process_equipment_utilization(equipment_name, project_name, usage_hours, idl
150
  "AI_Suggestion": ai_suggestion,
151
  "Suggestion_Confidence": suggestion_confidence,
152
  "Utilization_Score": utilization_score,
153
- "Report_Link": generated_report_link
 
154
  }
155
  except Exception as e:
156
  logger.error(f"Error creating or updating Salesforce record: {e}")
@@ -166,14 +171,14 @@ def gradio_upload_process(equipment_name, project_name, usage_hours, idle_hours,
166
  movement_frequency = float(movement_frequency)
167
  cost_per_hour = float(cost_per_hour)
168
  except Exception as e:
169
- return {"error": f"Invalid numeric input: {str(e)}"}
170
 
171
  try:
172
  last_maintenance_dt = datetime.datetime.strptime(last_maintenance, "%Y-%m-%d") if last_maintenance else None
173
  except Exception as e:
174
- return {"error": f"Invalid date format for Last Maintenance (expected YYYY-MM-DD): {str(e)}"}
175
 
176
- return process_equipment_utilization(
177
  equipment_name,
178
  project_name,
179
  usage_hours,
@@ -184,6 +189,7 @@ def gradio_upload_process(equipment_name, project_name, usage_hours, idle_hours,
184
  last_maintenance_dt,
185
  ai_suggestion
186
  )
 
187
 
188
  # UI
189
  with gr.Blocks() as app:
@@ -215,6 +221,7 @@ with gr.Blocks() as app:
215
  with gr.Row():
216
  submit_button = gr.Button("πŸš€ Submit", variant="primary")
217
  output = gr.JSON(label="πŸ“„ Salesforce Record Creation Result")
 
218
 
219
  submit_button.click(
220
  fn=gradio_upload_process,
@@ -225,7 +232,7 @@ with gr.Blocks() as app:
225
  report_link, last_maintenance,
226
  ai_suggestion_dropdown
227
  ],
228
- outputs=output
229
  )
230
 
231
  app.css = """
 
5
  from pathlib import Path
6
  import requests
7
  import gradio as gr
8
+ from reportlab.lib.pagesizes import letter
9
+ from reportlab.pdfgen import canvas
10
  from simple_salesforce import Salesforce
11
 
12
  # Configure logging
 
22
  SALESFORCE_PASSWORD = "vanesh@331"
23
  SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
24
 
 
25
  HUGGINGFACE_API_URL = "https://your-huggingface-model-endpoint"
26
 
27
  # Connect to Salesforce
 
54
 
55
  ai_suggestion_choices = ["Move", "Pause Rent", "Repair", "Replace"]
56
 
57
+ # Generate PDF report
58
+ def generate_pdf_report(record_id, data_dict):
59
  report_id = str(uuid.uuid4())[:8]
60
+ report_filename = f"report_{report_id}.pdf"
61
  report_path = Path(f"reports/{report_filename}")
62
  report_path.parent.mkdir(exist_ok=True)
63
 
64
+ c = canvas.Canvas(str(report_path), pagesize=letter)
65
+ c.setFont("Helvetica", 12)
66
+ c.drawString(100, 750, f"Equipment Utilization Report")
67
+ c.drawString(100, 735, f"Salesforce Record ID: {record_id}")
 
68
 
69
+ y = 710
70
+ for key, val in data_dict.items():
71
+ c.drawString(100, y, f"{key}: {val}")
72
+ y -= 20
73
+
74
+ c.save()
75
+ return str(report_path)
76
 
77
  # Call Hugging Face AI model
78
  def call_ai_model(usage_hours, idle_hours, movement_frequency, cost_per_hour, last_maintenance_str):
 
105
  return "No Action", 0.0, 0.0
106
 
107
  # Main process
 
108
  def process_equipment_utilization(equipment_name, project_name, usage_hours, idle_hours,
109
  movement_frequency, cost_per_hour, report_link, last_maintenance, ai_suggestion):
110
  last_maintenance_str = last_maintenance.strftime('%Y-%m-%d') if last_maintenance else None
 
125
  "AI_Suggestion__c": ai_suggestion,
126
  "Suggestion_Confidence__c": suggestion_confidence * 100,
127
  "Utilization_Score__c": utilization_score * 100,
128
+ "Report_Link__c": "Pending",
129
  "Last_Maintenance__c": last_maintenance_str,
130
  "Dashboard_Flag__c": False
131
  }
 
135
  record_id = response.get("id")
136
  logger.info(f"Salesforce record created: {record_id}")
137
 
 
138
  summary_data = {
139
  "Equipment Name": equipment_name,
140
  "Project": project_name,
 
144
  "Confidence": suggestion_confidence,
145
  "Utilization Score": utilization_score
146
  }
147
+ report_file_path = generate_pdf_report(record_id, summary_data)
148
 
149
+ sf.Equipment_Utilization_Record__c.update(record_id, {"Report_Link__c": report_file_path})
150
 
151
  return {
152
  "Salesforce_Record_Id": record_id,
 
154
  "AI_Suggestion": ai_suggestion,
155
  "Suggestion_Confidence": suggestion_confidence,
156
  "Utilization_Score": utilization_score,
157
+ "Report_Link": report_file_path,
158
+ "report_file_path": report_file_path
159
  }
160
  except Exception as e:
161
  logger.error(f"Error creating or updating Salesforce record: {e}")
 
171
  movement_frequency = float(movement_frequency)
172
  cost_per_hour = float(cost_per_hour)
173
  except Exception as e:
174
+ return {"error": f"Invalid numeric input: {str(e)}"}, None
175
 
176
  try:
177
  last_maintenance_dt = datetime.datetime.strptime(last_maintenance, "%Y-%m-%d") if last_maintenance else None
178
  except Exception as e:
179
+ return {"error": f"Invalid date format for Last Maintenance (expected YYYY-MM-DD): {str(e)}"}, None
180
 
181
+ result = process_equipment_utilization(
182
  equipment_name,
183
  project_name,
184
  usage_hours,
 
189
  last_maintenance_dt,
190
  ai_suggestion
191
  )
192
+ return result, result.get("report_file_path")
193
 
194
  # UI
195
  with gr.Blocks() as app:
 
221
  with gr.Row():
222
  submit_button = gr.Button("πŸš€ Submit", variant="primary")
223
  output = gr.JSON(label="πŸ“„ Salesforce Record Creation Result")
224
+ report_file_output = gr.File(label="πŸ“ƒ Download PDF Report")
225
 
226
  submit_button.click(
227
  fn=gradio_upload_process,
 
232
  report_link, last_maintenance,
233
  ai_suggestion_dropdown
234
  ],
235
+ outputs=[output, report_file_output]
236
  )
237
 
238
  app.css = """