Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,48 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
if not gguf_path.exists(): # grab ready-made gguf
|
14 |
-
subprocess.run(["huggingface-cli","download",
|
15 |
-
"microsoft/bitnet-b1.58-2B-4T-gguf",
|
16 |
-
"--include","ggml-model-i2_s.gguf",
|
17 |
-
"--local-dir",".","--repo-type","model"], check=True)
|
18 |
-
|
19 |
-
from llama_cpp import Llama # after wheel install
|
20 |
-
llm = Llama(model_path=str(gguf_path),
|
21 |
-
n_threads=threads,
|
22 |
-
n_ctx=8192, # bitnet uses 4-T rope, large ctx is fine
|
23 |
-
logits_all=False)
|
24 |
-
|
25 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
26 |
-
|
27 |
-
|
28 |
-
# llama-cpp streams dicts; we yield just the text chunk
|
29 |
-
for chunk in llm(prompt,
|
30 |
-
max_tokens=n_predict,
|
31 |
-
temperature=temperature,
|
32 |
-
stream=True):
|
33 |
-
yield chunk["choices"][0]["text"]
|
34 |
|
35 |
@spaces.GPU(duration=15)
|
36 |
-
def gpu():
|
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 |
-
gr.Slider(0.0,1.0,0.0,0.05,label="top-p (placeholder)"),
|
67 |
-
],
|
68 |
-
)
|
69 |
-
|
70 |
-
if __name__=="__main__":
|
71 |
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
import threading
|
3 |
+
import torch
|
4 |
+
import torch._dynamo
|
5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
6 |
+
import gradio as gr
|
7 |
+
import spaces
|
8 |
+
|
9 |
+
os.system("pip install git+https://github.com/shumingma/transformers.git")
|
10 |
+
torch._dynamo.config.suppress_errors = True
|
11 |
+
|
12 |
+
model_id = "microsoft/bitnet-b1.58-2B-4T"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float32, device_map={"": "cpu"}).to("cpu")
|
15 |
+
model.to("cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
@spaces.GPU(duration=15)
|
18 |
+
def gpu():
|
19 |
+
print("[GPU] | GPU maintained.")
|
20 |
+
|
21 |
+
def respond_simple(message: str, max_tokens: int, temperature: float, top_p: float):
|
22 |
+
inputs = tokenizer(message, return_tensors="pt").to("cpu")
|
23 |
+
streamer = TextIteratorStreamer(tokenizer, skip_special_tokens=True)
|
24 |
+
thread = threading.Thread(target=model.generate, kwargs={
|
25 |
+
**inputs,
|
26 |
+
"streamer": streamer,
|
27 |
+
"max_new_tokens": max_tokens,
|
28 |
+
"temperature": temperature,
|
29 |
+
"top_p": top_p,
|
30 |
+
"do_sample": True
|
31 |
+
})
|
32 |
+
thread.start()
|
33 |
+
output = ""
|
34 |
+
for chunk in streamer:
|
35 |
+
output += chunk
|
36 |
+
return output
|
37 |
+
|
38 |
+
with gr.Blocks() as demo:
|
39 |
+
gr.Markdown("## bitnet-b1.58-2b-4t completion")
|
40 |
+
tok = gr.Slider(1, 8192, value=2048, step=1, label="max new tokens")
|
41 |
+
temp = gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="temperature")
|
42 |
+
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="top-p")
|
43 |
+
inp = gr.Textbox(label="prompt", lines=2)
|
44 |
+
out = gr.Textbox(label="completion", lines=10)
|
45 |
+
inp.submit(respond_simple, [inp, tok, temp, top_p], out)
|
46 |
+
|
47 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
48 |
demo.launch()
|