pavansuresh commited on
Commit
91dabb2
·
verified ·
1 Parent(s): 5e82f81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -5
app.py CHANGED
@@ -10,13 +10,18 @@ from simple_salesforce import Salesforce
10
  from datetime import datetime
11
  import base64
12
  import io
 
 
 
 
 
13
 
14
  # Initialize PaddleOCR once with updated parameters
15
  ocr_model = PaddleOCR(use_textline_orientation=True, lang='en')
16
 
17
  def analyze_uv_coverage(img, brightness_threshold=150, kernel_size=5, apply_blur=True, adaptive_thresh=False):
18
  """
19
- Analyze UV sterilization coverage by multiplexing the grayscale image.
20
  Optional adaptive thresholding and Gaussian blur for noise reduction.
21
  Morphological operations clean the mask for better accuracy.
22
  """
@@ -91,20 +96,39 @@ def upload_image_and_get_url(image_path):
91
  from datetime import datetime
92
  import uuid
93
 
94
- # Define the uploads directory in the Hugging Face Space
95
- public_dir = os.path.join(os.getcwd(), "uploads") # Use current working directory + uploads
96
- os.makedirs(public_dir, exist_ok=True) # Create directory if it doesn't exist
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  # Generate a unique filename to avoid conflicts
99
  unique_filename = f"{uuid.uuid4().hex}_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}.jpg"
100
  destination_path = os.path.join(public_dir, unique_filename)
101
 
102
  # Copy the image to the uploads directory
103
- shutil.copy(image_path, destination_path)
 
 
 
 
 
 
 
104
 
105
  # Construct the public URL using the HF_SPACE_URL from .env
106
  space_url = os.environ.get('HF_SPACE_URL', 'https://huggingface.co/spaces/pavansuresh/Smartlab_tech')
107
  public_url = f"{space_url.rstrip('/')}/uploads/{unique_filename}"
 
108
 
109
  return public_url
110
 
 
10
  from datetime import datetime
11
  import base64
12
  import io
13
+ import logging
14
+
15
+ # Set up logging to debug file writing issues
16
+ logging.basicConfig(level=logging.INFO)
17
+ logger = logging.getLogger(__name__)
18
 
19
  # Initialize PaddleOCR once with updated parameters
20
  ocr_model = PaddleOCR(use_textline_orientation=True, lang='en')
21
 
22
  def analyze_uv_coverage(img, brightness_threshold=150, kernel_size=5, apply_blur=True, adaptive_thresh=False):
23
  """
24
+ Analyze UV sterilization coverage by thresholding the grayscale image.
25
  Optional adaptive thresholding and Gaussian blur for noise reduction.
26
  Morphological operations clean the mask for better accuracy.
27
  """
 
96
  from datetime import datetime
97
  import uuid
98
 
99
+ # Define the uploads directory in the Hugging Face Space's persistent storage
100
+ # Use the repository root (where the Space files are stored)
101
+ space_root = os.getenv("HF_SPACE_STORAGE", "/home/user/app") # Fallback to default path
102
+ public_dir = os.path.join(space_root, "uploads")
103
+
104
+ # Create the uploads directory if it doesn't exist
105
+ try:
106
+ os.makedirs(public_dir, exist_ok=True)
107
+ # Ensure the directory is writable
108
+ os.chmod(public_dir, 0o775)
109
+ logger.info(f"Created or verified uploads directory at: {public_dir}")
110
+ except Exception as e:
111
+ logger.error(f"Failed to create uploads directory: {e}")
112
+ raise
113
 
114
  # Generate a unique filename to avoid conflicts
115
  unique_filename = f"{uuid.uuid4().hex}_{datetime.utcnow().strftime('%Y%m%d_%H%M%S')}.jpg"
116
  destination_path = os.path.join(public_dir, unique_filename)
117
 
118
  # Copy the image to the uploads directory
119
+ try:
120
+ shutil.copy(image_path, destination_path)
121
+ # Ensure the file is readable
122
+ os.chmod(destination_path, 0o664)
123
+ logger.info(f"Successfully copied image to: {destination_path}")
124
+ except Exception as e:
125
+ logger.error(f"Failed to copy image to {destination_path}: {e}")
126
+ raise
127
 
128
  # Construct the public URL using the HF_SPACE_URL from .env
129
  space_url = os.environ.get('HF_SPACE_URL', 'https://huggingface.co/spaces/pavansuresh/Smartlab_tech')
130
  public_url = f"{space_url.rstrip('/')}/uploads/{unique_filename}"
131
+ logger.info(f"Generated public URL: {public_url}")
132
 
133
  return public_url
134