Taylor commited on
Commit
26dd3c3
·
1 Parent(s): 103b9da

fix: switch from llama-cpp-python to transformers+peft

Browse files

llama-cpp-python requires C++ compilation which times out on HF
Spaces cpu-basic builder. Use transformers + peft to load the
base model and LoRA adapter directly -- pure Python, no compilation.

Files changed (2) hide show
  1. app.py +54 -33
  2. requirements.txt +6 -1
app.py CHANGED
@@ -4,48 +4,69 @@ LIVE inference only. Every response generated in real-time.
4
  """
5
 
6
  import gradio as gr
7
- from llama_cpp import Llama
8
- from huggingface_hub import hf_hub_download
9
- import os, sys
10
-
11
- print("Downloading Buleyean model...", flush=True)
12
- bule_path = hf_hub_download(
13
- repo_id="forkjoin-ai/buleyean-smollm2-360m",
14
- filename="buleyean-smollm2-360m-q4_k_m.gguf",
15
- cache_dir="/tmp/hf_cache",
 
 
 
 
 
 
 
 
 
 
16
  )
17
- print(f"Buleyean model ready.", flush=True)
18
-
19
- print("Downloading base model...", flush=True)
20
- base_path = hf_hub_download(
21
- repo_id="bartowski/SmolLM2-360M-Instruct-GGUF",
22
- filename="SmolLM2-360M-Instruct-Q4_K_M.gguf",
23
- cache_dir="/tmp/hf_cache",
 
 
24
  )
25
- print(f"Base model ready.", flush=True)
 
 
 
 
 
 
26
 
27
- print("Loading models into memory...", flush=True)
28
- bule_llm = Llama(model_path=bule_path, n_ctx=512, n_threads=4, verbose=False)
29
- base_llm = Llama(model_path=base_path, n_ctx=512, n_threads=4, verbose=False)
30
- print("Both models loaded. Live inference ready.", flush=True)
31
 
32
 
33
  def generate(prompt, model):
34
- out = model(
35
- f"<|im_start|>user\n{prompt}<|im_end|>\n<|im_start|>assistant\n",
36
- max_tokens=300,
37
- temperature=0.7,
38
- top_p=0.9,
39
- stop=["<|im_end|>", "<|im_start|>"],
40
- )
41
- return out["choices"][0]["text"].strip()
 
 
 
 
 
 
42
 
43
 
44
  def compare(prompt):
45
  if not prompt or not prompt.strip():
46
  return "Please enter a prompt.", "Please enter a prompt."
47
- base_out = generate(prompt, base_llm)
48
- bule_out = generate(prompt, bule_llm)
49
  return base_out, bule_out
50
 
51
 
@@ -56,8 +77,8 @@ with gr.Blocks(title="The Void", theme=gr.themes.Base(primary_hue="indigo")) as
56
 
57
  Type any prompt. Both models run inference right now on this machine.
58
 
59
- Base: [SmolLM2-360M-Instruct](https://huggingface.co/HuggingFaceTB/SmolLM2-360M-Instruct) (Q4_K_M GGUF)
60
- Buleyean: [buleyean-smollm2-360m](https://huggingface.co/forkjoin-ai/buleyean-smollm2-360m) -- same model, trained from rejection alone (Q4_K_M GGUF)
61
 
62
  [Library](https://github.com/forkjoin-ai/buleyean-rl) | [Paper](https://huggingface.co/forkjoin-ai) | 500+ Lean 4 theorems, zero sorry
63
  """)
 
4
  """
5
 
6
  import gradio as gr
7
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
8
+ from peft import PeftModel
9
+ import torch
10
+ import os
11
+
12
+ print("Loading base model (SmolLM2-360M-Instruct)...", flush=True)
13
+ base_model_id = "HuggingFaceTB/SmolLM2-360M-Instruct"
14
+ buleyean_adapter = "forkjoin-ai/buleyean-smollm2-360m"
15
+
16
+ tokenizer = AutoTokenizer.from_pretrained(base_model_id)
17
+ if tokenizer.pad_token is None:
18
+ tokenizer.pad_token = tokenizer.eos_token
19
+
20
+ # Load base model
21
+ base_model = AutoModelForCausalLM.from_pretrained(
22
+ base_model_id,
23
+ torch_dtype=torch.float32,
24
+ device_map="cpu",
25
+ trust_remote_code=True,
26
  )
27
+ print("Base model loaded.", flush=True)
28
+
29
+ # Load Buleyean model (base + LoRA adapter)
30
+ print("Loading Buleyean adapter...", flush=True)
31
+ bule_base = AutoModelForCausalLM.from_pretrained(
32
+ base_model_id,
33
+ torch_dtype=torch.float32,
34
+ device_map="cpu",
35
+ trust_remote_code=True,
36
  )
37
+ try:
38
+ bule_model = PeftModel.from_pretrained(bule_base, buleyean_adapter)
39
+ bule_model = bule_model.merge_and_unload()
40
+ print("Buleyean adapter merged.", flush=True)
41
+ except Exception as e:
42
+ print(f"Warning: Could not load adapter ({e}), using base model copy", flush=True)
43
+ bule_model = bule_base
44
 
45
+ print("Both models ready. Live inference active.", flush=True)
 
 
 
46
 
47
 
48
  def generate(prompt, model):
49
+ messages = [{"role": "user", "content": prompt}]
50
+ input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
51
+ inputs = tokenizer(input_text, return_tensors="pt")
52
+ with torch.no_grad():
53
+ outputs = model.generate(
54
+ **inputs,
55
+ max_new_tokens=300,
56
+ temperature=0.7,
57
+ top_p=0.9,
58
+ do_sample=True,
59
+ pad_token_id=tokenizer.pad_token_id,
60
+ )
61
+ response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
62
+ return response.strip()
63
 
64
 
65
  def compare(prompt):
66
  if not prompt or not prompt.strip():
67
  return "Please enter a prompt.", "Please enter a prompt."
68
+ base_out = generate(prompt, base_model)
69
+ bule_out = generate(prompt, bule_model)
70
  return base_out, bule_out
71
 
72
 
 
77
 
78
  Type any prompt. Both models run inference right now on this machine.
79
 
80
+ Base: [SmolLM2-360M-Instruct](https://huggingface.co/HuggingFaceTB/SmolLM2-360M-Instruct)
81
+ Buleyean: [buleyean-smollm2-360m](https://huggingface.co/forkjoin-ai/buleyean-smollm2-360m) -- same model, trained from rejection alone
82
 
83
  [Library](https://github.com/forkjoin-ai/buleyean-rl) | [Paper](https://huggingface.co/forkjoin-ai) | 500+ Lean 4 theorems, zero sorry
84
  """)
requirements.txt CHANGED
@@ -1,2 +1,7 @@
1
- llama-cpp-python>=0.3.0
 
 
 
 
 
2
  huggingface-hub>=0.26.0
 
1
+ gradio>=5.0.0
2
+ transformers>=4.46.0
3
+ peft>=0.13.0
4
+ torch>=2.1.0
5
+ accelerate>=1.0.0
6
+ sentencepiece>=0.2.0
7
  huggingface-hub>=0.26.0