VortexHunter23 commited on
Commit
2fd4429
·
verified ·
1 Parent(s): 31058a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -67
app.py CHANGED
@@ -1,79 +1,28 @@
1
- import gradio as gr
2
  import spaces
3
  import torch
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
 
6
  MODEL_ID = "Scalai/scal-lite-60b-code"
7
 
8
- print("Loading tokenizer...")
9
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
10
 
11
- print("Loading model (this happens once at Space startup)...")
12
- model = AutoModelForCausalLM.from_pretrained(
13
- MODEL_ID,
14
- torch_dtype="auto",
15
- device_map="auto",
16
- trust_remote_code=True,
17
- )
18
- model.eval()
19
 
20
-
21
- @spaces.GPU(duration=90) # max seconds this call is allowed to hold a GPU
22
- def generate(prompt, system_prompt, max_new_tokens, temperature, top_p):
23
- messages = []
24
- if system_prompt.strip():
25
- messages.append({"role": "system", "content": system_prompt.strip()})
26
- messages.append({"role": "user", "content": prompt})
27
-
28
- inputs = tokenizer.apply_chat_template(
29
- messages,
30
- add_generation_prompt=True,
31
- return_tensors="pt",
32
- ).to(model.device)
33
-
34
- attention_mask = torch.ones_like(inputs)
35
-
36
- with torch.no_grad():
37
- outputs = model.generate(
38
- inputs,
39
- attention_mask=attention_mask,
40
- max_new_tokens=int(max_new_tokens),
41
- do_sample=temperature > 0,
42
- temperature=max(temperature, 0.01),
43
- top_p=top_p,
44
  )
 
45
 
46
- reply = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
47
- return reply
48
 
49
-
50
- with gr.Blocks(title="ScaLite-60B-Coder") as demo:
51
- gr.Markdown(
52
- "# ScaLite-60B-Coder (ZeroGPU)\n"
53
- "Bilingual (EN/ES) code + math model on a shared ZeroGPU slice. "
54
- "First response after idle time will be slower (cold start)."
55
- )
56
- with gr.Row():
57
- with gr.Column():
58
- system_prompt = gr.Textbox(
59
- label="System prompt",
60
- value="You are a logical, bilingual AI assistant. Think step-by-step.",
61
- lines=2,
62
- )
63
- prompt = gr.Textbox(label="Prompt", placeholder="Write a Python function that...", lines=6)
64
- with gr.Row():
65
- max_new_tokens = gr.Slider(64, 3500, value=1024, step=64, label="Max new tokens")
66
- temperature = gr.Slider(0.0, 1.5, value=0.6, step=0.05, label="Temperature")
67
- top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
68
- run_btn = gr.Button("Generate", variant="primary")
69
- with gr.Column():
70
- output = gr.Textbox(label="Response", lines=20)
71
-
72
- run_btn.click(
73
- fn=generate,
74
- inputs=[prompt, system_prompt, max_new_tokens, temperature, top_p],
75
- outputs=output,
76
- )
77
-
78
- if __name__ == "__main__":
79
- demo.queue().launch()
 
 
1
  import spaces
2
  import torch
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
5
  MODEL_ID = "Scalai/scal-lite-60b-code"
6
 
 
7
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
8
 
9
+ _model_holder = {"model": None}
 
 
 
 
 
 
 
10
 
11
+ @spaces.GPU(duration=180) # generous — first load under MXFP4 can be slow
12
+ def _load_model():
13
+ if _model_holder["model"] is None:
14
+ _model_holder["model"] = AutoModelForCausalLM.from_pretrained(
15
+ MODEL_ID,
16
+ torch_dtype="auto",
17
+ device_map="cuda",
18
+ trust_remote_code=True,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  )
20
+ return _model_holder["model"]
21
 
22
+ # Force the load to happen under a REAL GPU context, not emulation
23
+ _load_model()
24
 
25
+ @spaces.GPU(duration=90)
26
+ def generate(prompt, system_prompt, max_new_tokens, temperature, top_p):
27
+ model = _model_holder["model"]
28
+ # ... rest of your generate function, using `model` instead of the old global