Spaces:
Runtime error
Runtime error
Markus Clauss DIRU Vetsuisse
Claude
commited on
Commit
Β·
d387b61
1
Parent(s):
424d0e4
Use HF_TOKEN from environment variable for automatic model loading
Browse files- Remove manual token input field from interface
- Read HF_TOKEN from environment variables
- Auto-load model on app startup
- Simplify user experience - no manual token entry needed
- Users now set HF_TOKEN in Space settings > Variables and secrets
π€ Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
app.py
CHANGED
|
@@ -48,15 +48,23 @@ except ImportError:
|
|
| 48 |
# Global variables for model and tokenizer
|
| 49 |
model = None
|
| 50 |
tokenizer = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
@spaces.GPU
|
| 53 |
-
def load_model(
|
| 54 |
-
"""Load Apertus model with HuggingFace token"""
|
| 55 |
-
global model, tokenizer
|
| 56 |
-
|
| 57 |
-
if
|
| 58 |
-
return "
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
model_name = "swiss-ai/Apertus-8B-Instruct-2509"
|
| 61 |
|
| 62 |
try:
|
|
@@ -96,6 +104,7 @@ def load_model(hf_token):
|
|
| 96 |
# Check for xIELU optimization status
|
| 97 |
xielu_status = "β
CUDA xIELU Active" if XIELU_AVAILABLE and torch.cuda.is_available() else "π€ HuggingFace Optimized"
|
| 98 |
|
|
|
|
| 99 |
if memory_usage > 0:
|
| 100 |
return f"β
Model loaded successfully!\nπ Parameters: {total_params:,}\nπΎ Memory: {memory_usage:.1f} GB\nπ Optimization: {xielu_status}"
|
| 101 |
else:
|
|
@@ -108,9 +117,12 @@ def load_model(hf_token):
|
|
| 108 |
def chat_with_apertus(message, max_tokens=300):
|
| 109 |
"""Simple chat function"""
|
| 110 |
global model, tokenizer
|
| 111 |
-
|
|
|
|
| 112 |
if model is None or tokenizer is None:
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
|
| 115 |
try:
|
| 116 |
formatted_prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
|
@@ -2159,31 +2171,14 @@ def create_interface():
|
|
| 2159 |
</div>
|
| 2160 |
""")
|
| 2161 |
|
| 2162 |
-
|
| 2163 |
-
|
| 2164 |
-
|
| 2165 |
-
|
| 2166 |
-
|
| 2167 |
-
|
| 2168 |
-
|
| 2169 |
-
|
| 2170 |
-
)
|
| 2171 |
-
with gr.Column(scale=1):
|
| 2172 |
-
load_btn = gr.Button(
|
| 2173 |
-
"π¨π Load Apertus Model",
|
| 2174 |
-
variant="primary",
|
| 2175 |
-
size="lg",
|
| 2176 |
-
elem_classes="auth-button"
|
| 2177 |
-
)
|
| 2178 |
-
|
| 2179 |
-
with gr.Row():
|
| 2180 |
-
model_status = gr.Textbox(
|
| 2181 |
-
label="π Model Status",
|
| 2182 |
-
interactive=False,
|
| 2183 |
-
container=True
|
| 2184 |
-
)
|
| 2185 |
-
|
| 2186 |
-
load_btn.click(load_model, inputs=[hf_token], outputs=[model_status])
|
| 2187 |
|
| 2188 |
# Main Interface Tabs
|
| 2189 |
with gr.Tabs():
|
|
@@ -2202,6 +2197,7 @@ def create_interface():
|
|
| 2202 |
chat_output = gr.Markdown(label="Apertus Response")
|
| 2203 |
|
| 2204 |
chat_btn.click(chat_with_apertus, inputs=[chat_input, max_tokens], outputs=[chat_output])
|
|
|
|
| 2205 |
|
| 2206 |
# Attention Analysis Tab
|
| 2207 |
with gr.TabItem("ποΈ Attention Patterns"):
|
|
@@ -2462,6 +2458,9 @@ def create_interface():
|
|
| 2462 |
</div>
|
| 2463 |
""")
|
| 2464 |
|
|
|
|
|
|
|
|
|
|
| 2465 |
return demo
|
| 2466 |
|
| 2467 |
# Launch the app
|
|
|
|
| 48 |
# Global variables for model and tokenizer
|
| 49 |
model = None
|
| 50 |
tokenizer = None
|
| 51 |
+
model_loaded = False
|
| 52 |
+
|
| 53 |
+
# Get HF token from environment
|
| 54 |
+
HF_TOKEN = os.environ.get('HF_TOKEN', None)
|
| 55 |
|
| 56 |
@spaces.GPU
|
| 57 |
+
def load_model():
|
| 58 |
+
"""Load Apertus model with HuggingFace token from environment"""
|
| 59 |
+
global model, tokenizer, model_loaded
|
| 60 |
+
|
| 61 |
+
if model_loaded:
|
| 62 |
+
return "β
Model already loaded!"
|
| 63 |
+
|
| 64 |
+
hf_token = HF_TOKEN
|
| 65 |
+
if not hf_token:
|
| 66 |
+
return "β No HuggingFace token found. Please set HF_TOKEN environment variable."
|
| 67 |
+
|
| 68 |
model_name = "swiss-ai/Apertus-8B-Instruct-2509"
|
| 69 |
|
| 70 |
try:
|
|
|
|
| 104 |
# Check for xIELU optimization status
|
| 105 |
xielu_status = "β
CUDA xIELU Active" if XIELU_AVAILABLE and torch.cuda.is_available() else "π€ HuggingFace Optimized"
|
| 106 |
|
| 107 |
+
model_loaded = True
|
| 108 |
if memory_usage > 0:
|
| 109 |
return f"β
Model loaded successfully!\nπ Parameters: {total_params:,}\nπΎ Memory: {memory_usage:.1f} GB\nπ Optimization: {xielu_status}"
|
| 110 |
else:
|
|
|
|
| 117 |
def chat_with_apertus(message, max_tokens=300):
|
| 118 |
"""Simple chat function"""
|
| 119 |
global model, tokenizer
|
| 120 |
+
|
| 121 |
+
# Try to load model if not loaded
|
| 122 |
if model is None or tokenizer is None:
|
| 123 |
+
load_result = load_model()
|
| 124 |
+
if "β" in load_result:
|
| 125 |
+
return load_result
|
| 126 |
|
| 127 |
try:
|
| 128 |
formatted_prompt = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
|
|
|
| 2171 |
</div>
|
| 2172 |
""")
|
| 2173 |
|
| 2174 |
+
# Model Status Display
|
| 2175 |
+
model_status = gr.Textbox(
|
| 2176 |
+
label="π Model Status",
|
| 2177 |
+
value="β³ Loading Apertus model...",
|
| 2178 |
+
interactive=False,
|
| 2179 |
+
container=True
|
| 2180 |
+
)
|
| 2181 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2182 |
|
| 2183 |
# Main Interface Tabs
|
| 2184 |
with gr.Tabs():
|
|
|
|
| 2197 |
chat_output = gr.Markdown(label="Apertus Response")
|
| 2198 |
|
| 2199 |
chat_btn.click(chat_with_apertus, inputs=[chat_input, max_tokens], outputs=[chat_output])
|
| 2200 |
+
chat_input.submit(chat_with_apertus, inputs=[chat_input, max_tokens], outputs=[chat_output])
|
| 2201 |
|
| 2202 |
# Attention Analysis Tab
|
| 2203 |
with gr.TabItem("ποΈ Attention Patterns"):
|
|
|
|
| 2458 |
</div>
|
| 2459 |
""")
|
| 2460 |
|
| 2461 |
+
# Auto-load model on startup
|
| 2462 |
+
demo.load(load_model, outputs=[model_status])
|
| 2463 |
+
|
| 2464 |
return demo
|
| 2465 |
|
| 2466 |
# Launch the app
|