Spaces:
Sleeping
Sleeping
Taylor commited on
Commit ·
26dd3c3
1
Parent(s): 103b9da
fix: switch from llama-cpp-python to transformers+peft
Browse filesllama-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.
- app.py +54 -33
- 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
|
| 8 |
-
from
|
| 9 |
-
import
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
-
print(
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
)
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
print("
|
| 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 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
| 48 |
-
bule_out = generate(prompt,
|
| 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)
|
| 60 |
-
Buleyean: [buleyean-smollm2-360m](https://huggingface.co/forkjoin-ai/buleyean-smollm2-360m) -- same model, trained from rejection alone
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|