File size: 4,424 Bytes
e02ab32
544d134
 
e4f0059
544d134
44eef32
 
226e1ca
f40d7c0
e02ab32
 
44eef32
226e1ca
 
 
 
 
 
 
 
 
 
a5f23c0
226e1ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5f23c0
e02ab32
226e1ca
 
 
 
 
 
e02ab32
226e1ca
 
 
 
 
 
 
 
404c948
7702982
226e1ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64e2829
 
 
226e1ca
 
 
04b6aa6
226e1ca
 
44eef32
 
226e1ca
 
 
71b9b3b
226e1ca
 
 
 
 
 
 
 
404c948
 
226e1ca
 
 
 
493aee0
226e1ca
b945165
f59146a
71b9b3b
226e1ca
 
 
 
 
 
 
404c948
226e1ca
 
 
 
0ab509d
226e1ca
 
14d2605
 
226e1ca
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from huggingface_hub import hf_hub_download, snapshot_download, login
import pandas as pd
import importlib
import importlib.util
import streamlit as st
import sys
import os
import shutil
from pathlib import Path
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel

# #___________ cache issue 03_08
# if os.path.exists("private_space_cache"):
#     shutil.rmtree("private_space_cache")  # Forcefully remove corrupted cache folder
# incomplete_files = Path(".").rglob("*.incomplete")
# for file in incomplete_files:
#     try:
#         file.unlink()
#     except Exception as e:
#         print(f"Could not delete {file}: {e}")
# #____________________________________

# HF_TOKEN_LLAMA = os.environ.get("HF_TOKEN_LLAMA")
# login(token=HF_TOKEN_LLAMA)

# HF_TOKEN = os.environ.get("HF_TOKEN") #get HF_TOKEN 
# login(token=HF_TOKEN)


# USER_NAME = os.getenv("USER_NAME", "").strip().strip('"')
# PRIVATE_SPACE_NAME = os.getenv("PRIVATE_SPACE_NAME", "").strip().strip('"')

# #Construct the repo ID
# REPO_ID = f"{USER_NAME}/{PRIVATE_SPACE_NAME}"

# REPO_TYPE = "space"

# # sys.path.append(repo_dir)

# # Download the entire space, including the fine-tuned model folder
# repo_dir = snapshot_download(
#     repo_id=REPO_ID,
#     repo_type=REPO_TYPE,
#     token=HF_TOKEN,
#     cache_dir="private_space_cache",
#     force_download=True  # Forces redownload
# )


# # Change the working directory to the downloaded snapshot directory
# # This step is very imporptant
# os.chdir(repo_dir)

# # # Add repo directory to sys.path so Python can find modules inside it
# sys.path.append(repo_dir)


# # Download specific files (if snapshot_download wasn't used)
# app_path = hf_hub_download(
#     repo_id=REPO_ID,
#     filename="app.py",
#     repo_type=REPO_TYPE
# )

# # Load and execute `app.py`
# spec_app = importlib.util.spec_from_file_location("*", app_path)
# app_module = importlib.util.module_from_spec(spec_app)
# spec_app.loader.exec_module(app_module)
# # Now you can use functions from utils_module
# result = app_module.main()


# Set page config as the FIRST command
#st.set_page_config(page_title="📐 Math Assignment Optimizer", layout="wide")

# Helper function to clean up cache
def clear_cache(cache_dir="private_space_cache"):
    """Remove cache directory and all .incomplete files."""
    if os.path.exists(cache_dir):
        shutil.rmtree(cache_dir, ignore_errors=True)
    for file in Path(".").rglob("*.incomplete"):
        try:
            file.unlink()
        except Exception as e:
            st.warning(f"Could not delete {file}: {e}")

# Get environment variables
HF_TOKEN_LLAMA = os.environ.get("HF_TOKEN_LLAMA")
HF_TOKEN = os.environ.get("HF_TOKEN")
USER_NAME = os.getenv("USER_NAME", "").strip().strip('"')
PRIVATE_SPACE_NAME = os.getenv("PRIVATE_SPACE_NAME", "").strip().strip('"')

# Log in to Hugging Face
login(token=HF_TOKEN_LLAMA)
login(token=HF_TOKEN)

# Construct the repo ID
REPO_ID = f"{USER_NAME}/{PRIVATE_SPACE_NAME}"
REPO_TYPE = "space"

# Always clear cache before downloading
with st.spinner("Clearing old cache..."):
    clear_cache()

# Download the private space
with st.spinner("Downloading private space..."):
    try:
        repo_dir = snapshot_download(
            repo_id=REPO_ID,
            repo_type=REPO_TYPE,
            token=HF_TOKEN,
            cache_dir="private_space_cache",
            force_download=True,
            local_dir_use_symlinks=False
        )
    except Exception as e:
        st.error(f"Failed to download private space: {e}")
        st.stop()

# Change working directory and update sys.path
os.chdir(repo_dir)
sys.path.append(repo_dir)

# Download and load app.py from the private space
try:
    app_path = hf_hub_download(
        repo_id=REPO_ID,
        filename="app.py",
        repo_type=REPO_TYPE,
        token=HF_TOKEN,
        force_download=True
    )
except Exception as e:
    st.error(f"Failed to download app.py: {e}")
    st.stop()

# Load and execute app.py
spec_app = importlib.util.spec_from_file_location("app_module", app_path)
app_module = importlib.util.module_from_spec(spec_app)
spec_app.loader.exec_module(app_module)

# Run the private app's main function
try:
    result = app_module.main()
except Exception as e:
    st.error(f"Error running private app: {e}")
    st.stop()

# Optional: Clean up after execution
with st.spinner("Cleaning up..."):
    clear_cache()