wintergw commited on
Commit
cdd4b3d
·
verified ·
1 Parent(s): 2a19278

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +8 -16
  2. app.py +154 -0
  3. requirements.txt +9 -2
README.md CHANGED
@@ -1,20 +1,12 @@
1
  ---
2
- title: Textbook Complex Public
3
- emoji: 🚀
4
- colorFrom: red
5
- colorTo: red
6
- sdk: docker
7
- app_port: 8501
8
- tags:
9
- - streamlit
10
  pinned: false
11
- short_description: Streamlit template space
12
- license: mit
13
  ---
14
 
15
- # Welcome to Streamlit!
16
-
17
- Edit `/src/streamlit_app.py` to customize this app to your heart's desire. :heart:
18
-
19
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
20
- forums](https://discuss.streamlit.io).
 
1
  ---
2
+ title: Website Public
3
+ emoji:
4
+ colorFrom: pink
5
+ colorTo: purple
6
+ sdk: streamlit
7
+ sdk_version: 1.43.0
8
+ app_file: app.py
 
9
  pinned: false
 
 
10
  ---
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import hf_hub_download, snapshot_download, login
2
+ import pandas as pd
3
+ import importlib
4
+ import importlib.util
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
+ # Set page config as the FIRST command
75
+ st.set_page_config(page_title="📐 Math Assignment Optimizer", layout="wide")
76
+
77
+ # Helper function to clean up cache
78
+ def clear_cache(cache_dir="private_space_cache"):
79
+ """Remove cache directory and all .incomplete files."""
80
+ if os.path.exists(cache_dir):
81
+ shutil.rmtree(cache_dir, ignore_errors=True)
82
+ for file in Path(".").rglob("*.incomplete"):
83
+ try:
84
+ file.unlink()
85
+ except Exception as e:
86
+ st.warning(f"Could not delete {file}: {e}")
87
+
88
+ # Get environment variables
89
+ HF_TOKEN_LLAMA = os.environ.get("HF_TOKEN_LLAMA")
90
+ HF_TOKEN = os.environ.get("HF_TOKEN")
91
+ USER_NAME = os.getenv("USER_NAME", "").strip().strip('"')
92
+ PRIVATE_SPACE_NAME = os.getenv("PRIVATE_SPACE_NAME", "").strip().strip('"')
93
+
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,
116
+ local_dir_use_symlinks=False
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
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
+ print("Public version is running!!!!")
147
+ result = app_module.main()
148
+ except Exception as e:
149
+ st.error(f"Error running private app: {e}")
150
+ st.stop()
151
+
152
+ # Optional: Clean up after execution
153
+ with st.spinner("Cleaning up..."):
154
+ clear_cache()
requirements.txt CHANGED
@@ -1,3 +1,10 @@
1
- altair
2
  pandas
3
- streamlit
 
 
 
 
 
 
 
 
1
+ streamlit
2
  pandas
3
+ openpyxl
4
+ pyscipopt
5
+ transformers>=4.30.0
6
+ torch
7
+ peft
8
+ gurobipy
9
+ matplotlib
10
+ seaborn