Jin Zhu commited on
Commit
083833e
·
1 Parent(s): 0ca4954
Files changed (3) hide show
  1. Dockerfile +2 -0
  2. download_private_code.py +37 -0
  3. src/app.py +0 -35
Dockerfile CHANGED
@@ -1,5 +1,7 @@
1
  FROM python:3.10.8
2
 
 
 
3
  WORKDIR /app
4
 
5
  RUN apt-get update && apt-get install -y \
 
1
  FROM python:3.10.8
2
 
3
+ python download_private_model.py
4
+
5
  WORKDIR /app
6
 
7
  RUN apt-get update && apt-get install -y \
download_private_code.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # src/download_private_model.py
2
+ import os
3
+ import requests
4
+ from pathlib import Path
5
+
6
+ MODEL_DIR = Path("/src/FineTune")
7
+ MODEL_PATH = MODEL_DIR / "model.py"
8
+
9
+ def download_model():
10
+ print("📥 Checking private model file...")
11
+ if MODEL_PATH.exists():
12
+ print(f"✅ model.py already exists at {MODEL_PATH}")
13
+ return
14
+
15
+ hf_token = os.getenv("HF_TOKEN")
16
+ if not hf_token:
17
+ raise RuntimeError(
18
+ "❌ HF_TOKEN not found. Please set it in Hugging Face Space → Settings → Secrets."
19
+ )
20
+
21
+ url = "https://huggingface.co/mamba413/ada-core/resolve/main/model.py"
22
+ headers = {"Authorization": f"Bearer {hf_token}"}
23
+ MODEL_DIR.mkdir(parents=True, exist_ok=True)
24
+
25
+ print(f"📡 Downloading from {url}")
26
+ response = requests.get(url, headers=headers)
27
+ if response.status_code != 200:
28
+ raise RuntimeError(
29
+ f"❌ Failed to download model.py (HTTP {response.status_code}): {response.text[:200]}"
30
+ )
31
+
32
+ with open(MODEL_PATH, "wb") as f:
33
+ f.write(response.content)
34
+ print(f"✅ Successfully downloaded model.py to {MODEL_PATH}")
35
+
36
+ if __name__ == "__main__":
37
+ download_model()
src/app.py CHANGED
@@ -1,7 +1,5 @@
1
  import os
2
  from pathlib import Path
3
- import requests
4
- import sys
5
 
6
  # -----------------
7
  # Get the directory where app.py is located
@@ -30,39 +28,6 @@ if os.environ.get('SPACE_ID'):
30
  streamlit_dir = Path('/tmp/.streamlit')
31
  streamlit_dir.mkdir(exist_ok=True, parents=True)
32
 
33
- # ======================================================
34
- # ✅ 新增:在运行时从 Hugging Face 下载私有 model.py(写入 /tmp)
35
- # ======================================================
36
- target_dir = Path("/tmp/FineTune")
37
- target_path = target_dir / "model.py"
38
-
39
- if not target_path.exists():
40
- print(f"📥 Attempting to download private model.py to: {target_path}")
41
- hf_token = os.getenv("HF_TOKEN")
42
- if not hf_token:
43
- raise RuntimeError(
44
- "❌ HF_TOKEN not found. Please configure it in Hugging Face Space → Settings → Secrets."
45
- )
46
-
47
- url = "https://huggingface.co/mamba413/ada-core/resolve/main/model.py"
48
- headers = {"Authorization": f"Bearer {hf_token}"}
49
- response = requests.get(url, headers=headers)
50
-
51
- if response.status_code != 200:
52
- raise RuntimeError(
53
- f"❌ Failed to download model.py (HTTP {response.status_code}): {response.text[:200]}"
54
- )
55
-
56
- os.makedirs(target_dir, exist_ok=True)
57
- with open(target_path, "wb") as f:
58
- f.write(response.content)
59
- print("✅ Successfully downloaded private model.py")
60
- else:
61
- print("✅ model.py already exists in /tmp, skipping download.")
62
-
63
- # ✅ 添加 /tmp 到 Python 导入路径
64
- if str(target_dir.parent) not in sys.path:
65
- sys.path.insert(0, str(target_dir.parent))
66
 
67
  import streamlit as st
68
  from FineTune.model import ComputeScore
 
1
  import os
2
  from pathlib import Path
 
 
3
 
4
  # -----------------
5
  # Get the directory where app.py is located
 
28
  streamlit_dir = Path('/tmp/.streamlit')
29
  streamlit_dir.mkdir(exist_ok=True, parents=True)
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  import streamlit as st
33
  from FineTune.model import ComputeScore