Komal133 commited on
Commit
00507bf
·
verified ·
1 Parent(s): 8ea616c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -143
app.py CHANGED
@@ -1,89 +1,14 @@
1
- import gradio as gr
2
- from PIL import Image, ImageDraw
3
- import torch
4
- from torchvision import models, transforms
5
- from simple_salesforce import Salesforce
6
- import base64
7
- from io import BytesIO
8
- import json
9
- from datetime import datetime
10
  import logging
11
  import requests
12
- from fpdf import FPDF
13
 
14
  # Setup logging
15
  logging.basicConfig(level=logging.INFO)
16
 
17
- # Salesforce Credentials
18
- SALESFORCE_USERNAME = "drone@sathkrutha.com"
19
- SALESFORCE_PASSWORD = "Komal1303@"
20
- SALESFORCE_SECURITY_TOKEN = "53AWRskW9EjWUsSL5LU6nFTy3"
21
- SALESFORCE_INSTANCE_URL = "https://sathikrutha-a-dev-ed.my.salesforce.com"
22
-
23
- # Replace with a valid Site__c record ID from your Salesforce org
24
- SITE_RECORD_ID = "a003000000xxxxx" # TODO: Update with actual ID from Site__c
25
-
26
- # Connect to Salesforce
27
- try:
28
- sf = Salesforce(
29
- username=SALESFORCE_USERNAME,
30
- password=SALESFORCE_PASSWORD,
31
- security_token=SALESFORCE_SECURITY_TOKEN,
32
- instance_url=SALESFORCE_INSTANCE_URL
33
- )
34
- logging.info("Salesforce connection established.")
35
- except Exception as e:
36
- logging.error(f"Failed to connect to Salesforce: {str(e)}")
37
- raise Exception(f"Failed to connect to Salesforce: {str(e)}")
38
-
39
- # Load model from Hugging Face (Assuming you have a YOLOv8 model hosted)
40
  MODEL_URL = "https://huggingface.co/your_huggingface_model_path" # Replace with actual model URL
41
  HEADERS = {
42
  "Authorization": "Bearer YOUR_HUGGINGFACE_API_KEY" # Replace with your Hugging Face API key
43
  }
44
 
45
- # Image transformations
46
- transform = transforms.Compose([
47
- transforms.ToTensor(),
48
- ])
49
-
50
- # Function to generate PDF report
51
- def generate_pdf_report(faults):
52
- pdf = FPDF()
53
- pdf.set_auto_page_break(auto=True, margin=15)
54
- pdf.add_page()
55
-
56
- pdf.set_font("Arial", size=12)
57
- pdf.cell(200, 10, txt="Drone Fault Detection Report", ln=True, align='C')
58
-
59
- pdf.ln(10)
60
- pdf.cell(200, 10, txt="Faults Detected:", ln=True)
61
-
62
- for fault in faults:
63
- pdf.cell(200, 10, txt=f"Defect: {fault['type']}, Severity: {fault['severity']}, Confidence: {fault['confidence']}", ln=True)
64
-
65
- return pdf
66
-
67
- # Function to upload image to Salesforce as ContentVersion
68
- def upload_image_to_salesforce(image, filename="detected_image.jpg", record_id=None):
69
- try:
70
- buffered = BytesIO()
71
- image.save(buffered, format="JPEG")
72
- img_data = base64.b64encode(buffered.getvalue()).decode("utf-8")
73
-
74
- content_version = sf.ContentVersion.create({
75
- "Title": filename,
76
- "PathOnClient": filename,
77
- "VersionData": img_data,
78
- "FirstPublishLocationId": record_id if record_id else None
79
- })
80
- logging.info(f"Image uploaded to Salesforce with ContentVersion ID: {content_version['id']}")
81
- return content_version["id"]
82
- except Exception as e:
83
- logging.error(f"Failed to upload image to Salesforce: {str(e)}")
84
- raise Exception(f"Failed to upload image to Salesforce: {str(e)}")
85
-
86
- # Detect defects and integrate with Salesforce
87
  def detect_defects(image):
88
  if not image:
89
  return None, "No image provided"
@@ -96,7 +21,22 @@ def detect_defects(image):
96
 
97
  # Send image to Hugging Face hosted model for detection (assuming YOLOv8 model)
98
  response = requests.post(MODEL_URL, headers=HEADERS, json={"inputs": img_data})
99
- response_data = response.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
  # Assuming response is a list of detected objects with bounding boxes, labels, and scores
102
  faults = []
@@ -119,79 +59,13 @@ def detect_defects(image):
119
  draw.rectangle(box, outline="red", width=3)
120
  draw.text((box[0], box[1]), f"{fault['type']}: {fault['severity']}", fill="red")
121
 
122
- # Create Salesforce record if detections exist
123
- if faults:
124
- try:
125
- current_date = datetime.now().strftime("%Y-%m-%d")
126
- inspection_name = f"Inspection-{current_date}-{len(faults):03d}"
127
-
128
- # Creating the Salesforce record with updated fields
129
- inspection_record = sf.Drone_Structure_Inspection__c.create({
130
- "Inspection_Date__c": current_date,
131
- "Fault_Type__c": faults[0]["type"], # Mapping defect type
132
- "Severity__c": faults[0]["severity"], # Mapping severity
133
- "Fault_Summary__c": "\n".join([f"{fault['type']} (Confidence: {fault['confidence']})" for fault in faults]), # Summarizing the defects
134
- "Status__c": "New", # Default status
135
- "Annotated_Image_URL__c": "", # Placeholder for image URL
136
- "Report_PDF__c": "" # Placeholder for report PDF URL
137
- })
138
-
139
- record_id = inspection_record.get("id")
140
- content_version_id = upload_image_to_salesforce(
141
- result_image,
142
- filename=f"detected_defect_{record_id}.jpg",
143
- record_id=record_id
144
- )
145
-
146
- # Generate and upload the PDF report
147
- pdf = generate_pdf_report(faults)
148
- pdf_output = "/tmp/report.pdf"
149
- pdf.output(pdf_output)
150
-
151
- # Upload the PDF report to Salesforce (ContentVersion)
152
- with open(pdf_output, "rb") as f:
153
- report_data = f.read()
154
-
155
- pdf_data = base64.b64encode(report_data).decode("utf-8")
156
- content_version = sf.ContentVersion.create({
157
- "Title": f"Report_{record_id}.pdf",
158
- "PathOnClient": f"Report_{record_id}.pdf",
159
- "VersionData": pdf_data,
160
- "FirstPublishLocationId": record_id
161
- })
162
-
163
- if content_version:
164
- sf.Drone_Structure_Inspection__c.update(record_id, {
165
- "Report_PDF__c": f"/sfc/servlet.shepherd/version/download/{content_version['id']}",
166
- "Annotated_Image_URL__c": f"/sfc/servlet.shepherd/version/download/{content_version_id}"
167
- })
168
-
169
- faults.append(f"Salesforce record ID: {record_id}")
170
- except Exception as e:
171
- faults.append(f"Failed to create Salesforce record: {str(e)}")
172
-
173
  # Return a more readable, text-based format instead of JSON
174
  result_text = "\nDetected Faults and Severities:\n"
175
  for fault in faults:
176
- result_text += f"- {fault}\n"
177
 
178
  return result_image, result_text
179
 
180
  except Exception as e:
181
  logging.error(f"Processing failed: {str(e)}")
182
  return None, f"Processing failed: {str(e)}"
183
-
184
- # Gradio Interface
185
- demo = gr.Interface(
186
- fn=detect_defects,
187
- inputs=gr.Image(type="pil", label="Upload Drone Image"),
188
- outputs=[
189
- gr.Image(label="Detection Result"),
190
- gr.Textbox(label="Detected Faults with Severity", lines=10) # Updated to use text output
191
- ],
192
- title="Structural Defect Detection with Salesforce Integration",
193
- description="Detects objects using YOLOv8 or Faster R-CNN hosted on Hugging Face and stores results in Salesforce. Fine-tune the model for structural defects like cracks, rust, and spalling."
194
- )
195
-
196
- if __name__ == "__main__":
197
- demo.launch()
 
 
 
 
 
 
 
 
 
 
1
  import logging
2
  import requests
 
3
 
4
  # Setup logging
5
  logging.basicConfig(level=logging.INFO)
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  MODEL_URL = "https://huggingface.co/your_huggingface_model_path" # Replace with actual model URL
8
  HEADERS = {
9
  "Authorization": "Bearer YOUR_HUGGINGFACE_API_KEY" # Replace with your Hugging Face API key
10
  }
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def detect_defects(image):
13
  if not image:
14
  return None, "No image provided"
 
21
 
22
  # Send image to Hugging Face hosted model for detection (assuming YOLOv8 model)
23
  response = requests.post(MODEL_URL, headers=HEADERS, json={"inputs": img_data})
24
+
25
+ # Debugging the raw response
26
+ logging.info(f"Response Status Code: {response.status_code}")
27
+ logging.info(f"Response Text: {response.text}") # Log the raw response
28
+
29
+ # Check for valid response
30
+ if response.status_code != 200:
31
+ logging.error(f"Error in API request: {response.status_code}")
32
+ return None, f"API Error: {response.status_code}"
33
+
34
+ # Attempt to parse the JSON response
35
+ try:
36
+ response_data = response.json()
37
+ except ValueError:
38
+ logging.error("API response is not valid JSON")
39
+ return None, "API response is not valid JSON"
40
 
41
  # Assuming response is a list of detected objects with bounding boxes, labels, and scores
42
  faults = []
 
59
  draw.rectangle(box, outline="red", width=3)
60
  draw.text((box[0], box[1]), f"{fault['type']}: {fault['severity']}", fill="red")
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  # Return a more readable, text-based format instead of JSON
63
  result_text = "\nDetected Faults and Severities:\n"
64
  for fault in faults:
65
+ result_text += f"- {fault['type']} (Confidence: {fault['confidence']}, Severity: {fault['severity']})\n"
66
 
67
  return result_image, result_text
68
 
69
  except Exception as e:
70
  logging.error(f"Processing failed: {str(e)}")
71
  return None, f"Processing failed: {str(e)}"