Jin Zhu commited on
Commit
2022987
·
1 Parent(s): 90fe997

Update app.py

Browse files
Files changed (1) hide show
  1. src/app.py +20 -1
src/app.py CHANGED
@@ -4,6 +4,23 @@ import time
4
  import os
5
  from pathlib import Path
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  # -----------------
9
  # Page Configuration
@@ -25,7 +42,9 @@ def load_model(from_pretrained, base_model, cache_dir, device):
25
  """
26
  is_hf_space = os.environ.get('SPACE_ID') is not None
27
  if is_hf_space:
28
- cache_dir = str(Path.home() / '.cache' / 'huggingface')
 
 
29
  device = 'cpu'
30
  print("Using **CPU** now!")
31
 
 
4
  import os
5
  from pathlib import Path
6
 
7
+ # -----------------
8
+ # Fix Streamlit Permission Issues
9
+ # -----------------
10
+ # 在 HF Space 中,将 Streamlit 配置目录设置到可写位置
11
+ if os.environ.get('SPACE_ID'):
12
+ os.environ['STREAMLIT_SERVER_FILE_WATCHER_TYPE'] = 'none'
13
+ os.environ['STREAMLIT_BROWSER_GATHER_USAGE_STATS'] = 'false'
14
+ os.environ['STREAMLIT_SERVER_ENABLE_CORS'] = 'false'
15
+
16
+ # 设置 HuggingFace 缓存到可写目录
17
+ os.environ['HF_HOME'] = '/tmp/huggingface_cache'
18
+ os.environ['TRANSFORMERS_CACHE'] = '/tmp/huggingface_cache'
19
+ os.environ['HF_DATASETS_CACHE'] = '/tmp/huggingface_cache'
20
+
21
+ # 设置可写的配置目录
22
+ streamlit_dir = Path('/tmp/.streamlit')
23
+ streamlit_dir.mkdir(exist_ok=True, parents=True)
24
 
25
  # -----------------
26
  # Page Configuration
 
42
  """
43
  is_hf_space = os.environ.get('SPACE_ID') is not None
44
  if is_hf_space:
45
+ cache_dir = '/tmp/huggingface_cache'
46
+ os.makedirs(cache_dir, exist_ok=True)
47
+
48
  device = 'cpu'
49
  print("Using **CPU** now!")
50