Kavya-Jain commited on
Commit
ad2e526
·
verified ·
1 Parent(s): 203ea81

Upload Preprocessing.py

Browse files
Files changed (1) hide show
  1. Preprocessing.py +14 -1
Preprocessing.py CHANGED
@@ -10,12 +10,15 @@ GITHUB_BASE_URL = "https://raw.githubusercontent.com/jainkavya738/Electricity-ML
10
 
11
  NUM_IMPUTER_URL = "https://github.com/jainkavya738/Electricity-ML-Artifacts/raw/refs/heads/main/numerical_imputer.pkl"
12
  CAT_IMPUTER_URL = "https://github.com/jainkavya738/Electricity-ML-Artifacts/raw/refs/heads/main/categorical_imputer.pkl"
13
- LE_STRUCTURE_TYPE_URL = "https://github.com/jainkavya738/Electricity-ML-Artifacts/raw/refs/heads/main/label_encoder_structure_type.pkl "
14
  SCALER_URL = "https://github.com/jainkavya738/Electricity-ML-Artifacts/raw/refs/heads/main/scaler.pkl"
15
  MODEL_URL = "https://github.com/jainkavya738/Electricity-ML-Artifacts/raw/refs/heads/main/model.pkl"
16
 
17
  def download_and_load_pkl(url, filename):
18
  print(f"Attempting to download {filename} from {url}")
 
 
 
19
  try:
20
  response = requests.get(url, stream=True)
21
  response.raise_for_status()
@@ -30,12 +33,22 @@ def download_and_load_pkl(url, filename):
30
  print(f"Successfully downloaded and loaded {filename}")
31
  os.remove(temp_filepath)
32
  return obj
 
33
  except requests.exceptions.RequestException as e:
34
  print(f"Error downloading {temp_filepath} from {url}: {e}")
35
  return None
 
36
  except Exception as e:
37
  print(f"Error loading {temp_filepath} after download: {e}")
38
  return None
 
 
 
 
 
 
 
 
39
 
40
  numerical_imputer = download_and_load_pkl(NUM_IMPUTER_URL, "numerical_imputer.pkl")
41
  categorical_imputer = download_and_load_pkl(CAT_IMPUTER_URL, "categorical_imputer.pkl")
 
10
 
11
  NUM_IMPUTER_URL = "https://github.com/jainkavya738/Electricity-ML-Artifacts/raw/refs/heads/main/numerical_imputer.pkl"
12
  CAT_IMPUTER_URL = "https://github.com/jainkavya738/Electricity-ML-Artifacts/raw/refs/heads/main/categorical_imputer.pkl"
13
+ LE_STRUCTURE_TYPE_URL = "https://github.com/jainkavya738/Electricity-ML-Artifacts/raw/refs/heads/main/label_encoder_structure_type.pkl"
14
  SCALER_URL = "https://github.com/jainkavya738/Electricity-ML-Artifacts/raw/refs/heads/main/scaler.pkl"
15
  MODEL_URL = "https://github.com/jainkavya738/Electricity-ML-Artifacts/raw/refs/heads/main/model.pkl"
16
 
17
  def download_and_load_pkl(url, filename):
18
  print(f"Attempting to download {filename} from {url}")
19
+
20
+ temp_filepath = None
21
+
22
  try:
23
  response = requests.get(url, stream=True)
24
  response.raise_for_status()
 
33
  print(f"Successfully downloaded and loaded {filename}")
34
  os.remove(temp_filepath)
35
  return obj
36
+
37
  except requests.exceptions.RequestException as e:
38
  print(f"Error downloading {temp_filepath} from {url}: {e}")
39
  return None
40
+
41
  except Exception as e:
42
  print(f"Error loading {temp_filepath} after download: {e}")
43
  return None
44
+
45
+ finally:
46
+ if temp_filepath and os.path.exists(temp_filepath):
47
+ try:
48
+ os.remove(temp_filepath)
49
+ print(f"Cleaned up temporary file: {temp_filepath}")
50
+ except OSError as e:
51
+ print(f"Warning: Could not remove temporary file {temp_filepath}: {e}")
52
 
53
  numerical_imputer = download_and_load_pkl(NUM_IMPUTER_URL, "numerical_imputer.pkl")
54
  categorical_imputer = download_and_load_pkl(CAT_IMPUTER_URL, "categorical_imputer.pkl")