Hanxiaofeng123 commited on
Commit
317de70
·
verified ·
1 Parent(s): 6bd9af4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -17
app.py CHANGED
@@ -1,30 +1,34 @@
1
  import os
2
- from huggingface_hub import hf_hub_download
3
 
4
  def download_model():
5
- # 使用绝对路径
6
- model_dir = "/app/checkpoint"
7
- model_path = os.path.join(model_dir, "final_model_K_30.pth")
8
 
9
- # 确保目录存在并设置权限
10
- os.makedirs(model_dir, exist_ok=True)
11
- os.chmod(model_dir, 0o777)
 
 
 
12
 
13
  if not os.path.exists(model_path):
14
- print("使用 huggingface_hub 下载模型...")
15
  try:
16
- downloaded_path = hf_hub_download(
17
- repo_id="Hanxiaofeng123/Deepcube",
18
- filename="checkpoint/final_model_K_30.pth",
19
- repo_type="space",
20
- local_dir=model_dir, # 下载到指定目录
21
- local_dir_use_symlinks=False
22
- )
23
- print(f"模型下载完成: {downloaded_path}")
 
 
 
24
  return True
25
 
26
  except Exception as e:
27
- print(f"huggingface_hub 下载失败: {e}")
28
  return False
29
  else:
30
  print("模型文件已存在")
 
1
  import os
2
+ import requests
3
 
4
  def download_model():
5
+ model_path = "checkpoint/final_model_K_30.pth"
 
 
6
 
7
+ # 检查并修复目录权限问题
8
+ try:
9
+ os.makedirs("checkpoint", exist_ok=True)
10
+ except PermissionError:
11
+ print("无法创建目录,尝试使用当前目录")
12
+ model_path = "final_model_K_30.pth" # 直接下载到当前目录
13
 
14
  if not os.path.exists(model_path):
15
+ print("下载模型文件...")
16
  try:
17
+ url = "https://huggingface.co/spaces/Hanxiaofeng123/Deepcube/resolve/main/checkpoint/final_model_K_30.pth"
18
+
19
+ response = requests.get(url, stream=True, timeout=60)
20
+ response.raise_for_status()
21
+
22
+ # 直接下载到文件
23
+ with open(model_path, 'wb') as f:
24
+ for data in response.iter_content(8192):
25
+ f.write(data)
26
+
27
+ print(f"下载完成: {model_path}")
28
  return True
29
 
30
  except Exception as e:
31
+ print(f"下载失败: {e}")
32
  return False
33
  else:
34
  print("模型文件已存在")