Rammohan0504 commited on
Commit
4677555
·
verified ·
1 Parent(s): f456834

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -16
app.py CHANGED
@@ -10,7 +10,6 @@ import json
10
  import os
11
  from dotenv import load_dotenv
12
 
13
-
14
  # Load environment variables from .env file
15
  load_dotenv()
16
 
@@ -88,42 +87,56 @@ def create_and_upload_pdf(dpr_content):
88
 
89
  # Function to generate the daily progress report (DPR) text
90
  def generate_dpr(files):
91
- dpr_text = []
92
- current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
93
 
94
- # Add header to the DPR
95
- dpr_text.append(f"Daily Progress Report\nGenerated on: {current_time}\n")
 
 
 
 
 
 
96
 
 
 
 
 
 
 
 
97
  # Process each uploaded file (image)
98
  for file in files:
99
- # Open the image from file path
100
  image = Image.open(file.name) # Using file.name for filepath
101
 
102
  if image.mode != "RGB":
103
  image = image.convert("RGB")
104
 
105
- # Dynamically generate a caption based on the image
106
  caption = generate_captions_from_image(image)
107
 
108
- # Generate DPR section for this image with dynamic caption
109
  dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
110
  dpr_text.append(dpr_section)
111
 
112
- # Join the DPR sections to form the complete report
113
  dpr_content = "\n".join(dpr_text)
114
 
115
- # Create and upload the PDF for this report in Salesforce
116
  pdf_url = create_and_upload_pdf(dpr_content)
117
 
118
- # Create a new Salesforce record for this report
119
  new_report_data = {
120
- "Report_Date__c": current_time, # Set Report Date to current time
 
121
  "Detected_Activities__c": dpr_content, # Set the activities field to the generated text
122
- "Photo_Uploads__c": ", ".join([file.name for file in files]), # Upload file names as Photo Uploads
123
  "PDF_URL__c": pdf_url # Set the PDF URL field with the generated PDF link
124
  }
125
 
126
- # Insert new Daily Progress Report record into Salesforce
127
  new_report = sf.Daily_Progress_Reports__c.create(new_report_data)
128
 
129
  return f"New report created in Salesforce with PDF URL: {pdf_url}"
@@ -139,5 +152,3 @@ iface = gr.Interface(
139
  )
140
 
141
  iface.launch()
142
-
143
-
 
10
  import os
11
  from dotenv import load_dotenv
12
 
 
13
  # Load environment variables from .env file
14
  load_dotenv()
15
 
 
87
 
88
  # Function to generate the daily progress report (DPR) text
89
  def generate_dpr(files):
90
+ # Query the latest Daily Progress Report records to get the highest number
91
+ last_report = sf.query("SELECT Daily_Progress_Reports_Name__c FROM Daily_Progress_Reports__c ORDER BY CreatedDate DESC LIMIT 1")
92
 
93
+ # Generate the new report name
94
+ if last_report['totalSize'] > 0:
95
+ last_name = last_report['records'][0]['Daily_Progress_Reports_Name__c']
96
+ # Extract the number from the last report name, assuming format "Daily Progress Report X"
97
+ report_number = int(last_name.split()[-1]) + 1 # Increment the number
98
+ else:
99
+ # If no records exist, start with "1"
100
+ report_number = 1
101
 
102
+ # Generate the new report name dynamically
103
+ new_report_name = f"Daily Progress Report {report_number}"
104
+
105
+ # Prepare the current time and start the DPR text
106
+ current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
107
+ dpr_text = [f"Daily Progress Report\nGenerated on: {current_time}\n"]
108
+
109
  # Process each uploaded file (image)
110
  for file in files:
111
+ # Open the image from the file path
112
  image = Image.open(file.name) # Using file.name for filepath
113
 
114
  if image.mode != "RGB":
115
  image = image.convert("RGB")
116
 
117
+ # Generate a caption for the image
118
  caption = generate_captions_from_image(image)
119
 
120
+ # Generate a section of the DPR for this image
121
  dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
122
  dpr_text.append(dpr_section)
123
 
124
+ # Join the DPR sections into one complete string
125
  dpr_content = "\n".join(dpr_text)
126
 
127
+ # Create and upload the PDF for this report to Salesforce
128
  pdf_url = create_and_upload_pdf(dpr_content)
129
 
130
+ # Create the new Daily Progress Report record in Salesforce
131
  new_report_data = {
132
+ "Daily_Progress_Reports_Name__c": new_report_name, # Set the dynamic name
133
+ "Report_Date__c": current_time, # Set the report date to the current time
134
  "Detected_Activities__c": dpr_content, # Set the activities field to the generated text
135
+ "Photo_Uploads__c": ", ".join([file.name for file in files]), # Upload the image names as Photo Uploads
136
  "PDF_URL__c": pdf_url # Set the PDF URL field with the generated PDF link
137
  }
138
 
139
+ # Insert the new report into Salesforce
140
  new_report = sf.Daily_Progress_Reports__c.create(new_report_data)
141
 
142
  return f"New report created in Salesforce with PDF URL: {pdf_url}"
 
152
  )
153
 
154
  iface.launch()