Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,10 @@
|
|
1 |
-
# ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํ๋ ๋ช
๋ น์ด์
๋๋ค.
|
2 |
-
# ์ด ๋ถ๋ถ์ ์คํฌ๋ฆฝํธ ์คํ ์ด๋ฐ์ ํ ๋ฒ ์คํ๋ฉ๋๋ค.
|
3 |
import os
|
4 |
-
|
5 |
os.system("pip install git+https://github.com/shumingma/transformers.git")
|
6 |
-
print("Installation complete.")
|
7 |
|
8 |
-
# ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ import ํฉ๋๋ค.
|
9 |
import threading
|
10 |
import torch
|
11 |
import torch._dynamo
|
12 |
-
import gradio as gr
|
13 |
-
import spaces # Hugging Face Spaces ๊ด๋ จ ์ ํธ๋ฆฌํฐ
|
14 |
-
|
15 |
-
# torch._dynamo ์ค์ (์ ํ ์ฌํญ, ์ฑ๋ฅ ํฅ์ ์๋)
|
16 |
torch._dynamo.config.suppress_errors = True
|
17 |
|
18 |
from transformers import (
|
@@ -20,37 +12,20 @@ from transformers import (
|
|
20 |
AutoTokenizer,
|
21 |
TextIteratorStreamer,
|
22 |
)
|
|
|
|
|
23 |
|
24 |
-
# --- ๋ชจ๋ธ ๋ก๋ ---
|
25 |
-
# ๋ชจ๋ธ ๊ฒฝ๋ก ์ค์ (Hugging Face ๋ชจ๋ธ ID)
|
26 |
model_id = "microsoft/bitnet-b1.58-2B-4T"
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
36 |
-
model = AutoModelForCausalLM.from_pretrained(
|
37 |
-
model_id,
|
38 |
-
torch_dtype=torch.bfloat16, # bf16 ์ฌ์ฉ (GPU ๊ถ์ฅ)
|
39 |
-
device_map="auto", # ์ฌ์ฉ ๊ฐ๋ฅํ ๋๋ฐ์ด์ค์ ์๋์ผ๋ก ๋ชจ๋ธ ๋ฐฐ์น
|
40 |
-
trust_remote_code=True
|
41 |
-
)
|
42 |
-
print(f"๋ชจ๋ธ ๋๋ฐ์ด์ค: {model.device}")
|
43 |
-
print("๋ชจ๋ธ ๋ก๋ ์๋ฃ.")
|
44 |
-
|
45 |
-
except Exception as e:
|
46 |
-
print(f"๋ชจ๋ธ ๋ก๋ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
47 |
-
tokenizer = None
|
48 |
-
model = None
|
49 |
-
print("๋ชจ๋ธ ๋ก๋์ ์คํจํ์ต๋๋ค. ์ ํ๋ฆฌ์ผ์ด์
์ด ์ ๋๋ก ๋์ํ์ง ์์ ์ ์์ต๋๋ค.")
|
50 |
-
|
51 |
|
52 |
-
|
53 |
-
@spaces.GPU # ์ด ํจ์๊ฐ GPU ์์์ ์ฌ์ฉํ๋๋ก ๋ช
์ (Hugging Face Spaces)
|
54 |
def respond(
|
55 |
message: str,
|
56 |
history: list[tuple[str, str]],
|
@@ -59,109 +34,98 @@ def respond(
|
|
59 |
temperature: float,
|
60 |
top_p: float,
|
61 |
):
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
messages
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
**inputs,
|
87 |
-
streamer=streamer,
|
88 |
-
max_new_tokens=max_tokens,
|
89 |
-
temperature=temperature,
|
90 |
-
top_p=top_p,
|
91 |
-
do_sample=True,
|
92 |
-
pad_token_id=tokenizer.eos_token_id # ํจ๋ฉ ํ ํฐ ID ์ค์
|
93 |
-
)
|
94 |
-
|
95 |
-
# ๋ชจ๋ธ ์์ฑ์ ๋ณ๋์ ์ค๋ ๋์์ ์คํ
|
96 |
-
thread = threading.Thread(target=model.generate, kwargs=generate_kwargs)
|
97 |
-
thread.start()
|
98 |
-
|
99 |
-
# ์คํธ๋ฆฌ๋จธ์์ ์์ฑ๋ ํ
์คํธ๋ฅผ ์ฝ์ด์ yield
|
100 |
-
response = ""
|
101 |
-
for new_text in streamer:
|
102 |
-
response += new_text
|
103 |
-
yield response # ์ค์๊ฐ์ผ๋ก ์๋ต์ Gradio ์ธํฐํ์ด์ค๋ก ์ ๋ฌ
|
104 |
-
|
105 |
-
except Exception as e:
|
106 |
-
yield f"ํ
์คํธ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {e}"
|
107 |
-
# ์ค๋ฅ ๋ฐ์ ์ ์ค๋ ๋ ์ฒ๋ฆฌ ๋ก์ง ์ถ๊ฐ ๊ณ ๋ ค ํ์ (์ ํ ์ฌํญ)
|
108 |
-
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
],
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
gr.Slider(
|
139 |
-
minimum=1,
|
140 |
-
maximum=4096, # ๋ชจ๋ธ ์ต๋ ์ปจํ
์คํธ ๊ธธ์ด ๊ณ ๋ ค (๋๋ ๋ ๊ธธ๊ฒ ์ค์ )
|
141 |
-
value=512,
|
142 |
-
step=1,
|
143 |
-
label="Max new tokens"
|
144 |
-
),
|
145 |
-
gr.Slider(
|
146 |
-
minimum=0.1,
|
147 |
-
maximum=2.0, # Temperature ๋ฒ์ ์กฐ์ (ํ์์)
|
148 |
-
value=0.7,
|
149 |
-
step=0.1,
|
150 |
-
label="Temperature"
|
151 |
-
),
|
152 |
-
gr.Slider(
|
153 |
-
minimum=0.0, # Top-p ๋ฒ์ ์กฐ์ (ํ์์)
|
154 |
-
maximum=1.0,
|
155 |
-
value=0.95,
|
156 |
-
step=0.05,
|
157 |
-
label="Top-p (nucleus sampling)"
|
158 |
-
),
|
159 |
],
|
160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
-
|
163 |
-
|
164 |
-
# debug=True๋ก ์ค์ ํ๋ฉด ์์ธ ๋ก๊ทธ๋ฅผ ๋ณผ ์ ์์ต๋๋ค.
|
165 |
-
demo.launch(debug=True)
|
166 |
-
else:
|
167 |
-
print("๋ชจ๋ธ ๋ก๋ ์คํจ๋ก ์ธํด Gradio ์ธํฐํ์ด์ค๋ฅผ ์คํํ ์ ์์ต๋๋ค.")
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
|
3 |
os.system("pip install git+https://github.com/shumingma/transformers.git")
|
|
|
4 |
|
|
|
5 |
import threading
|
6 |
import torch
|
7 |
import torch._dynamo
|
|
|
|
|
|
|
|
|
8 |
torch._dynamo.config.suppress_errors = True
|
9 |
|
10 |
from transformers import (
|
|
|
12 |
AutoTokenizer,
|
13 |
TextIteratorStreamer,
|
14 |
)
|
15 |
+
import gradio as gr
|
16 |
+
import spaces
|
17 |
|
|
|
|
|
18 |
model_id = "microsoft/bitnet-b1.58-2B-4T"
|
19 |
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
21 |
+
model = AutoModelForCausalLM.from_pretrained(
|
22 |
+
model_id,
|
23 |
+
torch_dtype=torch.bfloat16,
|
24 |
+
device_map="auto"
|
25 |
+
)
|
26 |
+
print(model.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
@spaces.GPU
|
|
|
29 |
def respond(
|
30 |
message: str,
|
31 |
history: list[tuple[str, str]],
|
|
|
34 |
temperature: float,
|
35 |
top_p: float,
|
36 |
):
|
37 |
+
"""
|
38 |
+
Generate a chat response using streaming with TextIteratorStreamer.
|
39 |
+
Args:
|
40 |
+
message: User's current message.
|
41 |
+
history: List of (user, assistant) tuples from previous turns.
|
42 |
+
system_message: Initial system prompt guiding the assistant.
|
43 |
+
max_tokens: Maximum number of tokens to generate.
|
44 |
+
temperature: Sampling temperature.
|
45 |
+
top_p: Nucleus sampling probability.
|
46 |
+
Yields:
|
47 |
+
The growing response text as new tokens are generated.
|
48 |
+
"""
|
49 |
+
messages = [{"role": "system", "content": system_message}]
|
50 |
+
for user_msg, bot_msg in history:
|
51 |
+
if user_msg:
|
52 |
+
messages.append({"role": "user", "content": user_msg})
|
53 |
+
if bot_msg:
|
54 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
55 |
+
messages.append({"role": "user", "content": message})
|
56 |
+
|
57 |
+
prompt = tokenizer.apply_chat_template(
|
58 |
+
messages, tokenize=False, add_generation_prompt=True
|
59 |
+
)
|
60 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
streamer = TextIteratorStreamer(
|
63 |
+
tokenizer, skip_prompt=True, skip_special_tokens=True
|
64 |
+
)
|
65 |
+
generate_kwargs = dict(
|
66 |
+
**inputs,
|
67 |
+
streamer=streamer,
|
68 |
+
max_new_tokens=max_tokens,
|
69 |
+
temperature=temperature,
|
70 |
+
top_p=top_p,
|
71 |
+
do_sample=True,
|
72 |
+
)
|
73 |
+
thread = threading.Thread(target=model.generate, kwargs=generate_kwargs)
|
74 |
+
thread.start()
|
75 |
+
|
76 |
+
response = ""
|
77 |
+
for new_text in streamer:
|
78 |
+
response += new_text
|
79 |
+
yield response
|
80 |
+
|
81 |
+
demo = gr.ChatInterface(
|
82 |
+
fn=respond,
|
83 |
+
title="Bitnet-b1.58-2B-4T Chatbot",
|
84 |
+
description="This chat application is powered by Microsoft's SOTA Bitnet-b1.58-2B-4T and designed for natural and fast conversations.",
|
85 |
+
examples=[
|
86 |
+
[
|
87 |
+
"Hello! How are you?",
|
88 |
+
"You are a helpful AI assistant for everyday tasks.",
|
89 |
+
512,
|
90 |
+
0.7,
|
91 |
+
0.95,
|
92 |
],
|
93 |
+
[
|
94 |
+
"Can you code a snake game in Python?",
|
95 |
+
"You are a helpful AI assistant for coding.",
|
96 |
+
2048,
|
97 |
+
0.7,
|
98 |
+
0.95,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
],
|
100 |
+
],
|
101 |
+
additional_inputs=[
|
102 |
+
gr.Textbox(
|
103 |
+
value="You are a helpful AI assistant.",
|
104 |
+
label="System message"
|
105 |
+
),
|
106 |
+
gr.Slider(
|
107 |
+
minimum=1,
|
108 |
+
maximum=8192,
|
109 |
+
value=2048,
|
110 |
+
step=1,
|
111 |
+
label="Max new tokens"
|
112 |
+
),
|
113 |
+
gr.Slider(
|
114 |
+
minimum=0.1,
|
115 |
+
maximum=4.0,
|
116 |
+
value=0.7,
|
117 |
+
step=0.1,
|
118 |
+
label="Temperature"
|
119 |
+
),
|
120 |
+
gr.Slider(
|
121 |
+
minimum=0.1,
|
122 |
+
maximum=1.0,
|
123 |
+
value=0.95,
|
124 |
+
step=0.05,
|
125 |
+
label="Top-p (nucleus sampling)"
|
126 |
+
),
|
127 |
+
],
|
128 |
+
)
|
129 |
|
130 |
+
if __name__ == "__main__":
|
131 |
+
demo.launch()
|
|
|
|
|
|
|
|