Rekham1110 commited on
Commit
841d345
·
verified ·
1 Parent(s): b105689

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -70,7 +70,7 @@ def process_image(image, project_name):
70
  if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
71
  return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
72
 
73
- # Save image to public folder
74
  upload_dir = "public_uploads"
75
  os.makedirs(upload_dir, exist_ok=True)
76
  unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
@@ -78,14 +78,23 @@ def process_image(image, project_name):
78
  saved_image_path = os.path.join(upload_dir, image_filename)
79
  shutil.copy(image, saved_image_path)
80
 
81
- # Corrected public URL logic
82
- if os.getenv("GRADIO_SERVER_NAME"):
83
- public_url_base = f"https://{os.getenv('GRADIO_SERVER_NAME')}/file"
84
- else:
85
- public_url_base = "http://localhost:7860/file"
86
-
87
- image_url = f"{public_url_base}/{upload_dir}/{image_filename}"
 
 
 
 
 
 
 
 
88
 
 
89
  milestone, percent_complete, confidence_score = mock_ai_model(img)
90
 
91
  record = {
@@ -95,9 +104,10 @@ def process_image(image, project_name):
95
  "Last_Updated_On__c": datetime.now().isoformat(),
96
  "Upload_Status__c": "Success",
97
  "Comments__c": f"AI Prediction: {milestone} with {confidence_score*100}% confidence",
98
- "Last_Updated_Image__c": image_url
99
  }
100
 
 
101
  try:
102
  sf.Construction__c.create(record)
103
  except Exception as e:
 
70
  if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
71
  return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
72
 
73
+ # Save image to a temporary file
74
  upload_dir = "public_uploads"
75
  os.makedirs(upload_dir, exist_ok=True)
76
  unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
 
78
  saved_image_path = os.path.join(upload_dir, image_filename)
79
  shutil.copy(image, saved_image_path)
80
 
81
+ # Upload to Salesforce as a ContentVersion
82
+ try:
83
+ with open(saved_image_path, 'rb') as file:
84
+ content_version_data = {
85
+ 'Title': image_filename,
86
+ 'PathOnClient': image_filename,
87
+ 'VersionData': file,
88
+ 'FirstPublishLocationId': 'your_sobject_id', # Replace with the appropriate Salesforce object ID (e.g., Project or Construction)
89
+ }
90
+ content_version = sf.ContentVersion.create(content_version_data)
91
+
92
+ # Generate download URL for the uploaded image
93
+ file_url = f"https://{sf.sf_instance}/sfc/servlet.shepherd/version/download/{content_version['id']}"
94
+ except Exception as e:
95
+ return f"Error: Failed to upload image to Salesforce - {str(e)}", "Failure", "", "", 0
96
 
97
+ # AI prediction
98
  milestone, percent_complete, confidence_score = mock_ai_model(img)
99
 
100
  record = {
 
104
  "Last_Updated_On__c": datetime.now().isoformat(),
105
  "Upload_Status__c": "Success",
106
  "Comments__c": f"AI Prediction: {milestone} with {confidence_score*100}% confidence",
107
+ "Last_Updated_Image__c": file_url
108
  }
109
 
110
+ # Insert record into Salesforce
111
  try:
112
  sf.Construction__c.create(record)
113
  except Exception as e: