deepyuna commited on
Commit
226e1ca
·
verified ·
1 Parent(s): a938bdd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -42
app.py CHANGED
@@ -5,67 +5,149 @@ import importlib.util
5
  import streamlit as st
6
  import sys
7
  import os
 
8
  from pathlib import Path
9
  from transformers import AutoModelForCausalLM, AutoTokenizer
10
  from peft import PeftModel
11
- import shutil
12
 
13
- #___________ cache issue 03_08
14
- if os.path.exists("private_space_cache"):
15
- shutil.rmtree("private_space_cache") # Forcefully remove corrupted cache folder
16
- incomplete_files = Path(".").rglob("*.incomplete")
17
- for file in incomplete_files:
18
- try:
19
- file.unlink()
20
- except Exception as e:
21
- print(f"Could not delete {file}: {e}")
22
- #____________________________________
23
 
24
- HF_TOKEN_LLAMA = os.environ.get("HF_TOKEN_LLAMA")
25
- login(token=HF_TOKEN_LLAMA)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
- HF_TOKEN = os.environ.get("HF_TOKEN") #get HF_TOKEN
28
- login(token=HF_TOKEN)
29
 
 
 
 
 
 
 
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  USER_NAME = os.getenv("USER_NAME", "").strip().strip('"')
32
  PRIVATE_SPACE_NAME = os.getenv("PRIVATE_SPACE_NAME", "").strip().strip('"')
33
 
34
- #Construct the repo ID
35
- REPO_ID = f"{USER_NAME}/{PRIVATE_SPACE_NAME}"
 
36
 
 
 
37
  REPO_TYPE = "space"
38
 
39
- # sys.path.append(repo_dir)
40
-
41
- # Download the entire space, including the fine-tuned model folder
42
- repo_dir = snapshot_download(
43
- repo_id=REPO_ID,
44
- repo_type=REPO_TYPE,
45
- token=HF_TOKEN,
46
- cache_dir="private_space_cache",
47
- force_download=True # Forces redownload
48
- )
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- # Change the working directory to the downloaded snapshot directory
52
- # This step is very imporptant
53
  os.chdir(repo_dir)
54
-
55
- # # Add repo directory to sys.path so Python can find modules inside it
56
  sys.path.append(repo_dir)
57
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- # Download specific files (if snapshot_download wasn't used)
60
- app_path = hf_hub_download(
61
- repo_id=REPO_ID,
62
- filename="app.py",
63
- repo_type=REPO_TYPE
64
- )
65
-
66
- # Load and execute `app.py`
67
- spec_app = importlib.util.spec_from_file_location("*", app_path)
68
  app_module = importlib.util.module_from_spec(spec_app)
69
  spec_app.loader.exec_module(app_module)
70
- # Now you can use functions from utils_module
71
- result = app_module.main()
 
 
 
 
 
 
 
 
 
 
5
  import streamlit as st
6
  import sys
7
  import os
8
+ import shutil
9
  from pathlib import Path
10
  from transformers import AutoModelForCausalLM, AutoTokenizer
11
  from peft import PeftModel
 
12
 
13
+ # #___________ cache issue 03_08
14
+ # if os.path.exists("private_space_cache"):
15
+ # shutil.rmtree("private_space_cache") # Forcefully remove corrupted cache folder
16
+ # incomplete_files = Path(".").rglob("*.incomplete")
17
+ # for file in incomplete_files:
18
+ # try:
19
+ # file.unlink()
20
+ # except Exception as e:
21
+ # print(f"Could not delete {file}: {e}")
22
+ # #____________________________________
23
 
24
+ # HF_TOKEN_LLAMA = os.environ.get("HF_TOKEN_LLAMA")
25
+ # login(token=HF_TOKEN_LLAMA)
26
+
27
+ # HF_TOKEN = os.environ.get("HF_TOKEN") #get HF_TOKEN
28
+ # login(token=HF_TOKEN)
29
+
30
+
31
+ # USER_NAME = os.getenv("USER_NAME", "").strip().strip('"')
32
+ # PRIVATE_SPACE_NAME = os.getenv("PRIVATE_SPACE_NAME", "").strip().strip('"')
33
+
34
+ # #Construct the repo ID
35
+ # REPO_ID = f"{USER_NAME}/{PRIVATE_SPACE_NAME}"
36
+
37
+ # REPO_TYPE = "space"
38
+
39
+ # # sys.path.append(repo_dir)
40
+
41
+ # # Download the entire space, including the fine-tuned model folder
42
+ # repo_dir = snapshot_download(
43
+ # repo_id=REPO_ID,
44
+ # repo_type=REPO_TYPE,
45
+ # token=HF_TOKEN,
46
+ # cache_dir="private_space_cache",
47
+ # force_download=True # Forces redownload
48
+ # )
49
+
50
+
51
+ # # Change the working directory to the downloaded snapshot directory
52
+ # # This step is very imporptant
53
+ # os.chdir(repo_dir)
54
+
55
+ # # # Add repo directory to sys.path so Python can find modules inside it
56
+ # sys.path.append(repo_dir)
57
 
 
 
58
 
59
+ # # Download specific files (if snapshot_download wasn't used)
60
+ # app_path = hf_hub_download(
61
+ # repo_id=REPO_ID,
62
+ # filename="app.py",
63
+ # repo_type=REPO_TYPE
64
+ # )
65
 
66
+ # # Load and execute `app.py`
67
+ # spec_app = importlib.util.spec_from_file_location("*", app_path)
68
+ # app_module = importlib.util.module_from_spec(spec_app)
69
+ # spec_app.loader.exec_module(app_module)
70
+ # # Now you can use functions from utils_module
71
+ # result = app_module.main()
72
+
73
+
74
+
75
+ # Helper function to clean up cache
76
+ def clear_cache(cache_dir="private_space_cache"):
77
+ """Remove cache directory and all .incomplete files."""
78
+ if os.path.exists(cache_dir):
79
+ shutil.rmtree(cache_dir, ignore_errors=True)
80
+ for file in Path(".").rglob("*.incomplete"):
81
+ try:
82
+ file.unlink()
83
+ except Exception as e:
84
+ st.warning(f"Could not delete {file}: {e}")
85
+
86
+ # Streamlit app setup
87
+ st.title("Math Question Labeling Tool (Public Interface)")
88
+
89
+ # Get environment variables
90
+ HF_TOKEN_LLAMA = os.environ.get("HF_TOKEN_LLAMA")
91
+ HF_TOKEN = os.environ.get("HF_TOKEN")
92
  USER_NAME = os.getenv("USER_NAME", "").strip().strip('"')
93
  PRIVATE_SPACE_NAME = os.getenv("PRIVATE_SPACE_NAME", "").strip().strip('"')
94
 
95
+ # Log in to Hugging Face
96
+ login(token=HF_TOKEN_LLAMA)
97
+ login(token=HF_TOKEN)
98
 
99
+ # Construct the repo ID
100
+ REPO_ID = f"{USER_NAME}/{PRIVATE_SPACE_NAME}"
101
  REPO_TYPE = "space"
102
 
103
+ # Always clear cache before downloading
104
+ with st.spinner("Clearing old cache..."):
105
+ clear_cache()
 
 
 
 
 
 
 
106
 
107
+ # Download the private space
108
+ with st.spinner("Downloading private space..."):
109
+ try:
110
+ repo_dir = snapshot_download(
111
+ repo_id=REPO_ID,
112
+ repo_type=REPO_TYPE,
113
+ token=HF_TOKEN,
114
+ cache_dir="private_space_cache",
115
+ force_download=True, # Always redownload to avoid stale cache
116
+ local_dir_use_symlinks=False # Avoid symlinks that might break
117
+ )
118
+ except Exception as e:
119
+ st.error(f"Failed to download private space: {e}")
120
+ st.stop()
121
 
122
+ # Change working directory and update sys.path
 
123
  os.chdir(repo_dir)
 
 
124
  sys.path.append(repo_dir)
125
 
126
+ # Download and load app.py from the private space
127
+ try:
128
+ app_path = hf_hub_download(
129
+ repo_id=REPO_ID,
130
+ filename="app.py",
131
+ repo_type=REPO_TYPE,
132
+ token=HF_TOKEN,
133
+ force_download=True # Ensure fresh download
134
+ )
135
+ except Exception as e:
136
+ st.error(f"Failed to download app.py: {e}")
137
+ st.stop()
138
 
139
+ # Load and execute app.py
140
+ spec_app = importlib.util.spec_from_file_location("app_module", app_path)
 
 
 
 
 
 
 
141
  app_module = importlib.util.module_from_spec(spec_app)
142
  spec_app.loader.exec_module(app_module)
143
+
144
+ # Run the private app's main function
145
+ try:
146
+ result = app_module.main()
147
+ except Exception as e:
148
+ st.error(f"Error running private app: {e}")
149
+ st.stop()
150
+
151
+ # Optional: Clean up after execution
152
+ with st.spinner("Cleaning up..."):
153
+ clear_cache()