Janeka commited on
Commit
ee56820
·
verified ·
1 Parent(s): d0c6198

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -20
app.py CHANGED
@@ -7,10 +7,10 @@ import os
7
  import requests
8
  import hashlib
9
 
10
- # Configuration
11
  MODEL_URL = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.onnx"
12
  MODEL_PATH = "realesr.onnx"
13
- EXPECTED_MD5 = "8a628e89b1e4d9f5f174a3e8c0c7b3b1" # MD5 hash of correct model file
14
 
15
  def verify_file(file_path, expected_md5):
16
  """Verify file integrity using MD5 hash"""
@@ -21,25 +21,39 @@ def verify_file(file_path, expected_md5):
21
  return file_hash == expected_md5
22
 
23
  def download_model():
24
- """Download model with verification"""
25
  print("Downloading model...")
26
- try:
27
- response = requests.get(MODEL_URL, stream=True)
28
- response.raise_for_status()
29
-
30
- # Save in chunks to handle large files
31
- with open(MODEL_PATH, "wb") as f:
32
- for chunk in response.iter_content(chunk_size=8192):
33
- f.write(chunk)
34
-
35
- # Verify download
36
- if not verify_file(MODEL_PATH, EXPECTED_MD5):
37
- os.remove(MODEL_PATH)
38
- raise ValueError("Downloaded file is corrupted")
39
 
40
- print("Model downloaded and verified successfully!")
41
- except Exception as e:
42
- raise gr.Error(f"Model download failed: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  # Download model if missing or corrupted
45
  if not verify_file(MODEL_PATH, EXPECTED_MD5):
@@ -53,7 +67,7 @@ try:
53
  )
54
  print("Model loaded successfully!")
55
  except Exception as e:
56
- raise gr.Error(f"Model loading failed: {str(e)}\nTry deleting and reuploading the model file.")
57
 
58
  def enhance(image):
59
  """Image enhancement function"""
 
7
  import requests
8
  import hashlib
9
 
10
+ # Configuration - UPDATED MODEL URL
11
  MODEL_URL = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.onnx"
12
  MODEL_PATH = "realesr.onnx"
13
+ EXPECTED_MD5 = "8a628e89b1e4d9f5f174a3e8c0c7b3b1" # MD5 hash for verification
14
 
15
  def verify_file(file_path, expected_md5):
16
  """Verify file integrity using MD5 hash"""
 
21
  return file_hash == expected_md5
22
 
23
  def download_model():
24
+ """Download model with verification and retries"""
25
  print("Downloading model...")
26
+ for attempt in range(3): # Retry up to 3 times
27
+ try:
28
+ # Use a mirror URL if primary fails
29
+ urls = [
30
+ MODEL_URL,
31
+ "https://huggingface.co/spaces/akhaliq/Real-ESRGAN/resolve/main/realesr-general-x4v3.onnx"
32
+ ]
 
 
 
 
 
 
33
 
34
+ for url in urls:
35
+ try:
36
+ response = requests.get(url, stream=True, timeout=30)
37
+ response.raise_for_status()
38
+
39
+ # Save in chunks
40
+ with open(MODEL_PATH, "wb") as f:
41
+ for chunk in response.iter_content(chunk_size=8192):
42
+ f.write(chunk)
43
+
44
+ # Verify download
45
+ if verify_file(MODEL_PATH, EXPECTED_MD5):
46
+ print("Model downloaded and verified successfully!")
47
+ return
48
+ os.remove(MODEL_PATH)
49
+ except Exception:
50
+ continue
51
+
52
+ raise ValueError("All download attempts failed")
53
+ except Exception as e:
54
+ if attempt == 2: # Final attempt
55
+ raise gr.Error(f"Model download failed after 3 attempts: {str(e)}")
56
+ print(f"Attempt {attempt + 1} failed, retrying...")
57
 
58
  # Download model if missing or corrupted
59
  if not verify_file(MODEL_PATH, EXPECTED_MD5):
 
67
  )
68
  print("Model loaded successfully!")
69
  except Exception as e:
70
+ raise gr.Error(f"Model loading failed: {str(e)}\nTry deleting the space and recreating it.")
71
 
72
  def enhance(image):
73
  """Image enhancement function"""