Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,6 +22,7 @@ SALESFORCE_USERNAME = "vaneshdevarapalli866@agentforce.com"
|
|
| 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
|
|
@@ -41,7 +42,6 @@ def connect_to_salesforce():
|
|
| 41 |
|
| 42 |
sf = connect_to_salesforce()
|
| 43 |
|
| 44 |
-
# Dropdown options
|
| 45 |
equipment_choices = [
|
| 46 |
"Bulldozer", "Crane", "Excavator", "Loader", "Forklift",
|
| 47 |
"Backhoe", "Grader", "Scraper", "Dump Truck", "Roller"
|
|
@@ -54,12 +54,12 @@ project_choices = [
|
|
| 54 |
|
| 55 |
ai_suggestion_choices = ["Move", "Pause Rent", "Repair", "Replace"]
|
| 56 |
|
| 57 |
-
# Generate PDF
|
| 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)
|
|
@@ -72,9 +72,8 @@ def generate_pdf_report(record_id, data_dict):
|
|
| 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):
|
| 79 |
try:
|
| 80 |
payload = {
|
|
@@ -104,7 +103,6 @@ def call_ai_model(usage_hours, idle_hours, movement_frequency, cost_per_hour, la
|
|
| 104 |
logger.error(f"Error calling AI model: {e}")
|
| 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
|
|
@@ -144,9 +142,10 @@ def process_equipment_utilization(equipment_name, project_name, usage_hours, idl
|
|
| 144 |
"Confidence": suggestion_confidence,
|
| 145 |
"Utilization Score": utilization_score
|
| 146 |
}
|
| 147 |
-
report_file_path = generate_pdf_report(record_id, summary_data)
|
| 148 |
|
| 149 |
-
|
|
|
|
|
|
|
| 150 |
|
| 151 |
return {
|
| 152 |
"Salesforce_Record_Id": record_id,
|
|
@@ -154,14 +153,13 @@ def process_equipment_utilization(equipment_name, project_name, usage_hours, idl
|
|
| 154 |
"AI_Suggestion": ai_suggestion,
|
| 155 |
"Suggestion_Confidence": suggestion_confidence,
|
| 156 |
"Utilization_Score": utilization_score,
|
| 157 |
-
"Report_Link":
|
| 158 |
-
"report_file_path":
|
| 159 |
}
|
| 160 |
except Exception as e:
|
| 161 |
logger.error(f"Error creating or updating Salesforce record: {e}")
|
| 162 |
return {"error": str(e)}
|
| 163 |
|
| 164 |
-
# Gradio function
|
| 165 |
def gradio_upload_process(equipment_name, project_name, usage_hours, idle_hours,
|
| 166 |
movement_frequency, cost_per_hour,
|
| 167 |
report_link, last_maintenance, ai_suggestion):
|
|
@@ -191,7 +189,6 @@ def gradio_upload_process(equipment_name, project_name, usage_hours, idle_hours,
|
|
| 191 |
)
|
| 192 |
return result, result.get("report_file_path")
|
| 193 |
|
| 194 |
-
# UI
|
| 195 |
with gr.Blocks() as app:
|
| 196 |
gr.Markdown("## 📋 Equipment Utilization Record Uploader")
|
| 197 |
gr.Markdown("Fill in the details below to generate AI suggestions and save them to Salesforce.")
|
|
|
|
| 22 |
SALESFORCE_PASSWORD = "vanesh@331"
|
| 23 |
SALESFORCE_SECURITY_TOKEN = "VRUVbBOdG0s9Q4xy0W6DB1Y6b"
|
| 24 |
|
| 25 |
+
HUGGINGFACE_SPACE_URL = "https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME"
|
| 26 |
HUGGINGFACE_API_URL = "https://your-huggingface-model-endpoint"
|
| 27 |
|
| 28 |
# Connect to Salesforce
|
|
|
|
| 42 |
|
| 43 |
sf = connect_to_salesforce()
|
| 44 |
|
|
|
|
| 45 |
equipment_choices = [
|
| 46 |
"Bulldozer", "Crane", "Excavator", "Loader", "Forklift",
|
| 47 |
"Backhoe", "Grader", "Scraper", "Dump Truck", "Roller"
|
|
|
|
| 54 |
|
| 55 |
ai_suggestion_choices = ["Move", "Pause Rent", "Repair", "Replace"]
|
| 56 |
|
| 57 |
+
# Generate PDF and return public Hugging Face URL
|
| 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"static/reports/{report_filename}")
|
| 62 |
+
report_path.parent.mkdir(parents=True, exist_ok=True)
|
| 63 |
|
| 64 |
c = canvas.Canvas(str(report_path), pagesize=letter)
|
| 65 |
c.setFont("Helvetica", 12)
|
|
|
|
| 72 |
y -= 20
|
| 73 |
|
| 74 |
c.save()
|
| 75 |
+
return f"{HUGGINGFACE_SPACE_URL}/file/static/reports/{report_filename}", str(report_path)
|
| 76 |
|
|
|
|
| 77 |
def call_ai_model(usage_hours, idle_hours, movement_frequency, cost_per_hour, last_maintenance_str):
|
| 78 |
try:
|
| 79 |
payload = {
|
|
|
|
| 103 |
logger.error(f"Error calling AI model: {e}")
|
| 104 |
return "No Action", 0.0, 0.0
|
| 105 |
|
|
|
|
| 106 |
def process_equipment_utilization(equipment_name, project_name, usage_hours, idle_hours,
|
| 107 |
movement_frequency, cost_per_hour, report_link, last_maintenance, ai_suggestion):
|
| 108 |
last_maintenance_str = last_maintenance.strftime('%Y-%m-%d') if last_maintenance else None
|
|
|
|
| 142 |
"Confidence": suggestion_confidence,
|
| 143 |
"Utilization Score": utilization_score
|
| 144 |
}
|
|
|
|
| 145 |
|
| 146 |
+
public_url, file_path = generate_pdf_report(record_id, summary_data)
|
| 147 |
+
|
| 148 |
+
sf.Equipment_Utilization_Record__c.update(record_id, {"Report_Link__c": public_url})
|
| 149 |
|
| 150 |
return {
|
| 151 |
"Salesforce_Record_Id": record_id,
|
|
|
|
| 153 |
"AI_Suggestion": ai_suggestion,
|
| 154 |
"Suggestion_Confidence": suggestion_confidence,
|
| 155 |
"Utilization_Score": utilization_score,
|
| 156 |
+
"Report_Link": public_url,
|
| 157 |
+
"report_file_path": file_path
|
| 158 |
}
|
| 159 |
except Exception as e:
|
| 160 |
logger.error(f"Error creating or updating Salesforce record: {e}")
|
| 161 |
return {"error": str(e)}
|
| 162 |
|
|
|
|
| 163 |
def gradio_upload_process(equipment_name, project_name, usage_hours, idle_hours,
|
| 164 |
movement_frequency, cost_per_hour,
|
| 165 |
report_link, last_maintenance, ai_suggestion):
|
|
|
|
| 189 |
)
|
| 190 |
return result, result.get("report_file_path")
|
| 191 |
|
|
|
|
| 192 |
with gr.Blocks() as app:
|
| 193 |
gr.Markdown("## 📋 Equipment Utilization Record Uploader")
|
| 194 |
gr.Markdown("Fill in the details below to generate AI suggestions and save them to Salesforce.")
|