Rekham1110 commited on
Commit
7b362b3
·
verified ·
1 Parent(s): aaf09b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -11
app.py CHANGED
@@ -32,20 +32,18 @@ except Exception as e:
32
  # Valid milestones
33
  VALID_MILESTONES = ["Foundation", "Walls Erected", "Planning", "Completed"]
34
 
35
- # Updated deterministic AI prediction with accurate completion percentages
36
  def mock_ai_model(image):
37
  img = image.convert("RGB")
38
  max_size = 1024
39
  img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
40
 
41
- # Hash image bytes for deterministic output
42
  img_bytes = img.tobytes()
43
  img_hash = int(hashlib.sha256(img_bytes).hexdigest(), 16)
44
 
45
  milestone_index = img_hash % len(VALID_MILESTONES)
46
  milestone = VALID_MILESTONES[milestone_index]
47
 
48
- # Map milestones to fixed completion percentages
49
  milestone_completion_map = {
50
  "Planning": 10,
51
  "Foundation": 30,
@@ -54,13 +52,12 @@ def mock_ai_model(image):
54
  }
55
  completion_percent = milestone_completion_map.get(milestone, 0)
56
 
57
- # Deterministic confidence score between 0.85 and 0.95
58
  confidence_raw = 0.85 + ((img_hash % 1000) / 1000) * (0.95 - 0.85)
59
  confidence_score = round(confidence_raw, 2)
60
 
61
  return milestone, completion_percent, confidence_score
62
 
63
- # Gradio function
64
  def process_image(image, project_name):
65
  try:
66
  if image is None:
@@ -73,7 +70,7 @@ def process_image(image, project_name):
73
  if not str(image).lower().endswith(('.jpg', '.jpeg', '.png')):
74
  return "Error: Only JPG/PNG images are supported.", "Failure", "", "", 0
75
 
76
- # Save image to public folder for URL generation
77
  upload_dir = "public_uploads"
78
  os.makedirs(upload_dir, exist_ok=True)
79
  unique_id = datetime.now().strftime("%Y%m%d%H%M%S")
@@ -81,14 +78,16 @@ def process_image(image, project_name):
81
  saved_image_path = os.path.join(upload_dir, image_filename)
82
  shutil.copy(image, saved_image_path)
83
 
84
- # Create public URL assuming you're serving /public_uploads/ via static web server (e.g., on localhost or external host)
85
- public_url_base = os.getenv("PUBLIC_URL_BASE", "http://localhost:7860/public_uploads")
86
- image_url = f"{public_url_base}/{image_filename}"
 
 
 
 
87
 
88
- # Predict
89
  milestone, percent_complete, confidence_score = mock_ai_model(img)
90
 
91
- # Construct Salesforce record
92
  record = {
93
  "Name__c": project_name,
94
  "Current_Milestone__c": milestone,
 
32
  # Valid milestones
33
  VALID_MILESTONES = ["Foundation", "Walls Erected", "Planning", "Completed"]
34
 
35
+ # Deterministic AI prediction with fixed confidence and percent
36
  def mock_ai_model(image):
37
  img = image.convert("RGB")
38
  max_size = 1024
39
  img.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
40
 
 
41
  img_bytes = img.tobytes()
42
  img_hash = int(hashlib.sha256(img_bytes).hexdigest(), 16)
43
 
44
  milestone_index = img_hash % len(VALID_MILESTONES)
45
  milestone = VALID_MILESTONES[milestone_index]
46
 
 
47
  milestone_completion_map = {
48
  "Planning": 10,
49
  "Foundation": 30,
 
52
  }
53
  completion_percent = milestone_completion_map.get(milestone, 0)
54
 
 
55
  confidence_raw = 0.85 + ((img_hash % 1000) / 1000) * (0.95 - 0.85)
56
  confidence_score = round(confidence_raw, 2)
57
 
58
  return milestone, completion_percent, confidence_score
59
 
60
+ # Image processing and Salesforce upload
61
  def process_image(image, project_name):
62
  try:
63
  if image is None:
 
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
  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 = {
92
  "Name__c": project_name,
93
  "Current_Milestone__c": milestone,