Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import threading
|
| 3 |
import torch
|
| 4 |
import torch._dynamo
|
|
@@ -12,55 +15,17 @@ from transformers import (
|
|
| 12 |
import gradio as gr
|
| 13 |
import spaces
|
| 14 |
|
| 15 |
-
# νμν κ²½μ° Bitnet μ§μμ μν transformers μ€μΉ
|
| 16 |
-
# Hugging Face Spacesμμλ Dockerfile λ±μ ν΅ν΄ 미리 μ€μΉνλ κ²μ΄ λ μΌλ°μ μ
λλ€.
|
| 17 |
-
# λ‘컬μμ ν
μ€νΈ μμλ νμν μ μμ΅λλ€.
|
| 18 |
-
# print("Installing required transformers branch...")
|
| 19 |
-
# try:
|
| 20 |
-
# os.system("pip install git+https://github.com/shumingma/transformers.git -q")
|
| 21 |
-
# print("transformers branch installed.")
|
| 22 |
-
# except Exception as e:
|
| 23 |
-
# print(f"Error installing transformers branch: {e}")
|
| 24 |
-
# print("Proceeding with potentially default transformers version.")
|
| 25 |
-
|
| 26 |
-
# os.system("pip install accelerate bitsandbytes -q") # bitsandbytes, accelerateλ νμν μ μμ΅λλ€.
|
| 27 |
-
|
| 28 |
-
|
| 29 |
model_id = "microsoft/bitnet-b1.58-2B-4T"
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
model_id,
|
| 39 |
-
torch_dtype=torch.bfloat16,
|
| 40 |
-
device_map="auto",
|
| 41 |
-
# load_in_8bit=True # Bitnetμ 1.58bitμ΄λ―λ‘ 8bit λ‘λ©μ΄ μλ―Έ μμ μ μμ΅λλ€.
|
| 42 |
-
)
|
| 43 |
-
print(f"Model loaded successfully on device: {model.device}")
|
| 44 |
-
except Exception as e:
|
| 45 |
-
print(f"Error loading model: {e}")
|
| 46 |
-
# λͺ¨λΈ λ‘λ© μ€ν¨ μ λλ―Έ λͺ¨λΈ μ¬μ© λλ μ€λ₯ μ²λ¦¬
|
| 47 |
-
class DummyModel:
|
| 48 |
-
def generate(self, **kwargs):
|
| 49 |
-
# λλ―Έ μλ΅ μμ±
|
| 50 |
-
input_ids = kwargs.get('input_ids')
|
| 51 |
-
streamer = kwargs.get('streamer')
|
| 52 |
-
if streamer:
|
| 53 |
-
# κ°λ¨ν λλ―Έ μλ΅ μ€νΈλ¦¬λ°
|
| 54 |
-
dummy_response = "λͺ¨λΈ λ‘λ©μ μ€ν¨νμ¬ λλ―Έ μλ΅μ μ 곡ν©λλ€. μ€μ /κ²½λ‘λ₯Ό νμΈνμΈμ."
|
| 55 |
-
for char in dummy_response:
|
| 56 |
-
streamer.put(char)
|
| 57 |
-
streamer.end()
|
| 58 |
-
model = DummyModel()
|
| 59 |
-
tokenizer = AutoTokenizer.from_pretrained("gpt2") # λλ―Έ ν ν¬λμ΄μ
|
| 60 |
-
print("Using dummy model due to loading failure.")
|
| 61 |
-
|
| 62 |
|
| 63 |
-
@spaces.GPU
|
| 64 |
def respond(
|
| 65 |
message: str,
|
| 66 |
history: list[tuple[str, str]],
|
|
@@ -81,11 +46,6 @@ def respond(
|
|
| 81 |
Yields:
|
| 82 |
The growing response text as new tokens are generated.
|
| 83 |
"""
|
| 84 |
-
# λλ―Έ λͺ¨λΈ μ¬μ© μ μ€νΈλ¦¬λ° μ€λ₯ λ°©μ§
|
| 85 |
-
if isinstance(model, DummyModel):
|
| 86 |
-
yield "λͺ¨λΈ λ‘λ©μ μ€ν¨νμ¬ μλ΅μ μμ±ν μ μμ΅λλ€."
|
| 87 |
-
return
|
| 88 |
-
|
| 89 |
messages = [{"role": "system", "content": system_message}]
|
| 90 |
for user_msg, bot_msg in history:
|
| 91 |
if user_msg:
|
|
@@ -94,236 +54,45 @@ def respond(
|
|
| 94 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 95 |
messages.append({"role": "user", "content": message})
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 102 |
-
|
| 103 |
-
streamer = TextIteratorStreamer(
|
| 104 |
-
tokenizer, skip_prompt=True, skip_special_tokens=True
|
| 105 |
-
)
|
| 106 |
-
generate_kwargs = dict(
|
| 107 |
-
**inputs,
|
| 108 |
-
streamer=streamer,
|
| 109 |
-
max_new_tokens=max_tokens,
|
| 110 |
-
temperature=temperature,
|
| 111 |
-
top_p=top_p,
|
| 112 |
-
do_sample=True,
|
| 113 |
-
# Bitnet λͺ¨λΈμ νμν μΆκ° μΈμ μ€μ (λͺ¨λΈ λ¬Έμ νμΈ νμ)
|
| 114 |
-
# μλ₯Ό λ€μ΄, quantize_config λ±
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
# μ°λ λμμ λͺ¨λΈ μμ± μ€ν
|
| 118 |
-
thread = threading.Thread(target=model.generate, kwargs=generate_kwargs)
|
| 119 |
-
thread.start()
|
| 120 |
-
|
| 121 |
-
# μ€νΈλ¦¬λ¨Έλ‘λΆν° ν
μ€νΈλ₯Ό μ½μ΄μ yield
|
| 122 |
-
response = ""
|
| 123 |
-
for new_text in streamer:
|
| 124 |
-
# yield νκΈ° μ μ λΆνμν 곡백/ν ν° μ κ±° λλ μ²λ¦¬ κ°λ₯
|
| 125 |
-
response += new_text
|
| 126 |
-
yield response
|
| 127 |
-
|
| 128 |
-
except Exception as e:
|
| 129 |
-
print(f"Error during response generation: {e}")
|
| 130 |
-
yield f"μλ΅ μμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€: {e}"
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
# --- λμμΈ κ°μ μ μν CSS μ½λ ---
|
| 134 |
-
css_styles = """
|
| 135 |
-
/* μ 체 νμ΄μ§ λ°°κ²½ λ° κΈ°λ³Έ ν°νΈ μ€μ */
|
| 136 |
-
body {
|
| 137 |
-
font-family: 'Segoe UI', 'Roboto', 'Arial', sans-serif;
|
| 138 |
-
line-height: 1.6;
|
| 139 |
-
margin: 0;
|
| 140 |
-
padding: 20px; /* μ± μ£Όλ³ μ¬λ°± μΆκ° */
|
| 141 |
-
background-color: #f4f7f6; /* λΆλλ¬μ΄ λ°°κ²½μ */
|
| 142 |
-
}
|
| 143 |
-
|
| 144 |
-
/* λ©μΈ μ± μ»¨ν
μ΄λ μ€νμΌ */
|
| 145 |
-
.gradio-container {
|
| 146 |
-
max-width: 900px; /* μ€μ μ λ ¬ λ° μ΅λ λλΉ μ ν */
|
| 147 |
-
margin: 20px auto;
|
| 148 |
-
border-radius: 12px; /* λ₯κ·Ό λͺ¨μ리 */
|
| 149 |
-
overflow: hidden; /* μμ μμλ€μ΄ λͺ¨μ리λ₯Ό λμ§ μλλ‘ */
|
| 150 |
-
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1); /* κ·Έλ¦Όμ ν¨κ³Ό */
|
| 151 |
-
background-color: #ffffff; /* μ± λ΄μ© μμ λ°°κ²½μ */
|
| 152 |
-
}
|
| 153 |
-
|
| 154 |
-
/* νμ΄ν λ° μ€λͺ
μμ (ChatInterfaceμ κΈ°λ³Έ νμ΄ν/μ€λͺ
) */
|
| 155 |
-
/* μ΄ μμμ ChatInterface ꡬ쑰μ λ°λΌ μ νν ν΄λμ€ μ΄λ¦μ΄ λ€λ₯Ό μ μμΌλ,
|
| 156 |
-
.gradio-container λ΄λΆμ 첫 λΈλ‘μ΄λ H1/P νκ·Έλ₯Ό νκ²ν μ μμ΅λλ€.
|
| 157 |
-
ν
λ§μ ν¨κ» μ¬μ©νλ©΄ λλΆλΆ μ μ²λ¦¬λ©λλ€. μ¬κΈ°μλ μΆκ°μ μΈ ν¨λ© λ±λ§ κ³ λ € */
|
| 158 |
-
.gradio-container > .gradio-block:first-child {
|
| 159 |
-
padding: 20px 20px 10px 20px; /* μλ¨ ν¨λ© μ‘°μ */
|
| 160 |
-
}
|
| 161 |
-
|
| 162 |
-
/* μ±ν
λ°μ€ μμ μ€νμΌ */
|
| 163 |
-
.gradio-chatbox {
|
| 164 |
-
/* ν
λ§μ μν΄ μ€νμΌλ§λμ§λ§, μΆκ°μ μΈ λ΄λΆ ν¨λ© λ± μ‘°μ κ°λ₯ */
|
| 165 |
-
padding: 15px;
|
| 166 |
-
background-color: #fefefe; /* μ±ν
μμ λ°°κ²½μ */
|
| 167 |
-
border-radius: 8px; /* μ±ν
μμ λ΄λΆ λͺ¨μ리 */
|
| 168 |
-
border: 1px solid #e0e0e0; /* κ²½κ³μ */
|
| 169 |
-
}
|
| 170 |
-
|
| 171 |
-
/* μ±ν
λ©μμ§ μ€νμΌ */
|
| 172 |
-
.gradio-chatmessage {
|
| 173 |
-
margin-bottom: 12px;
|
| 174 |
-
padding: 10px 15px;
|
| 175 |
-
border-radius: 20px; /* λ₯κ·Ό λ©μμ§ λͺ¨μ리 */
|
| 176 |
-
max-width: 75%; /* λ©μμ§ λλΉ μ ν */
|
| 177 |
-
word-wrap: break-word; /* κΈ΄ λ¨μ΄ μ€λ°κΏ */
|
| 178 |
-
white-space: pre-wrap; /* 곡백 λ° μ€λ°κΏ μ μ§ */
|
| 179 |
-
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05); /* λ©μμ§μ μ½κ°μ κ·Έλ¦Όμ */
|
| 180 |
-
}
|
| 181 |
-
|
| 182 |
-
/* μ¬μ©μ λ©μμ§ μ€νμΌ */
|
| 183 |
-
.gradio-chatmessage.user {
|
| 184 |
-
background-color: #007bff; /* νλμ κ³μ΄ */
|
| 185 |
-
color: white;
|
| 186 |
-
margin-left: auto; /* μ€λ₯Έμͺ½ μ λ ¬ */
|
| 187 |
-
border-bottom-right-radius: 2px; /* μ€λ₯Έμͺ½ μλ λͺ¨μ리 κ°μ§κ² */
|
| 188 |
-
}
|
| 189 |
-
|
| 190 |
-
/* λ΄ λ©μμ§ μ€νμΌ */
|
| 191 |
-
.gradio-chatmessage.bot {
|
| 192 |
-
background-color: #e9ecef; /* λ°μ νμ */
|
| 193 |
-
color: #333; /* μ΄λμ΄ ν
μ€νΈ */
|
| 194 |
-
margin-right: auto; /* μΌμͺ½ μ λ ¬ */
|
| 195 |
-
border-bottom-left-radius: 2px; /* μΌμͺ½ μλ λͺ¨μ리 κ°μ§κ² */
|
| 196 |
-
}
|
| 197 |
-
|
| 198 |
-
/* μ
λ ₯μ°½ λ° λ²νΌ μμ μ€νμΌ */
|
| 199 |
-
.gradio-input-box {
|
| 200 |
-
padding: 15px;
|
| 201 |
-
border-top: 1px solid #eee; /* μμͺ½ κ²½κ³μ */
|
| 202 |
-
background-color: #f8f9fa; /* μ
λ ₯ μμ λ°°κ²½μ */
|
| 203 |
-
}
|
| 204 |
-
/* μ
λ ₯ ν
μ€νΈ μμ΄λ¦¬μ΄ μ€νμΌ */
|
| 205 |
-
.gradio-input-box textarea {
|
| 206 |
-
border-radius: 8px;
|
| 207 |
-
padding: 10px;
|
| 208 |
-
border: 1px solid #ccc;
|
| 209 |
-
resize: none !important; /* μ
λ ₯μ°½ ν¬κΈ° μ‘°μ λΉνμ±ν (μ ν μ¬ν) */
|
| 210 |
-
min-height: 50px; /* μ΅μ λμ΄ */
|
| 211 |
-
max-height: 150px; /* μ΅λ λμ΄ */
|
| 212 |
-
overflow-y: auto; /* λ΄μ© λμΉ κ²½μ° μ€ν¬λ‘€ */
|
| 213 |
-
}
|
| 214 |
-
/* μ€ν¬λ‘€λ° μ€νμΌ (μ ν μ¬ν) */
|
| 215 |
-
.gradio-input-box textarea::-webkit-scrollbar {
|
| 216 |
-
width: 8px;
|
| 217 |
-
}
|
| 218 |
-
.gradio-input-box textarea::-webkit-scrollbar-thumb {
|
| 219 |
-
background-color: #ccc;
|
| 220 |
-
border-radius: 4px;
|
| 221 |
-
}
|
| 222 |
-
.gradio-input-box textarea::-webkit-scrollbar-track {
|
| 223 |
-
background-color: #f1f1f1;
|
| 224 |
-
}
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
/* λ²νΌ μ€νμΌ */
|
| 228 |
-
.gradio-button {
|
| 229 |
-
border-radius: 8px;
|
| 230 |
-
padding: 10px 20px;
|
| 231 |
-
font-weight: bold;
|
| 232 |
-
transition: background-color 0.2s ease, opacity 0.2s ease; /* νΈλ² μ λλ©μ΄μ
*/
|
| 233 |
-
border: none; /* κΈ°λ³Έ ν
λ리 μ κ±° */
|
| 234 |
-
cursor: pointer;
|
| 235 |
-
}
|
| 236 |
-
|
| 237 |
-
.gradio-button:not(.clear-button) { /* Send λ²νΌ */
|
| 238 |
-
background-color: #28a745; /* μ΄λ‘μ */
|
| 239 |
-
color: white;
|
| 240 |
-
}
|
| 241 |
-
.gradio-button:not(.clear-button):hover {
|
| 242 |
-
background-color: #218838;
|
| 243 |
-
}
|
| 244 |
-
.gradio-button:disabled { /* λΉνμ±νλ λ²νΌ */
|
| 245 |
-
opacity: 0.6;
|
| 246 |
-
cursor: not-allowed;
|
| 247 |
-
}
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
.gradio-button.clear-button { /* Clear λ²νΌ */
|
| 251 |
-
background-color: #dc3545; /* λΉ¨κ°μ */
|
| 252 |
-
color: white;
|
| 253 |
-
}
|
| 254 |
-
.gradio-button.clear-button:hover {
|
| 255 |
-
background-color: #c82333;
|
| 256 |
-
}
|
| 257 |
-
|
| 258 |
-
/* Additional inputs (μΆκ° μ€μ ) μμ μ€νμΌ */
|
| 259 |
-
/* μ΄ μμμ λ³΄ν΅ μμ½λμΈ ννλ‘ λμ΄ μμΌλ©°, .gradio-accordion ν΄λμ€λ₯Ό κ°μ§λλ€. */
|
| 260 |
-
.gradio-accordion {
|
| 261 |
-
border-radius: 12px; /* μΈλΆ 컨ν
μ΄λμ λμΌν λͺ¨μ리 */
|
| 262 |
-
margin-top: 15px; /* μ±ν
μμκ³Όμ κ°κ²© */
|
| 263 |
-
border: 1px solid #ddd; /* κ²½κ³μ */
|
| 264 |
-
box-shadow: none; /* λ΄λΆ κ·Έλ¦Όμ μ κ±° */
|
| 265 |
-
}
|
| 266 |
-
/* μμ½λμΈ ν€λ (λΌλ²¨) μ€νμΌ */
|
| 267 |
-
.gradio-accordion .label {
|
| 268 |
-
font-weight: bold;
|
| 269 |
-
color: #007bff; /* νλμ κ³μ΄ */
|
| 270 |
-
padding: 15px; /* ν€λ ν¨λ© */
|
| 271 |
-
background-color: #e9ecef; /* ν€λ λ°°κ²½μ */
|
| 272 |
-
border-bottom: 1px solid #ddd; /* ν€λ μλ κ²½κ³μ */
|
| 273 |
-
border-top-left-radius: 11px; /* μλ¨ λͺ¨μ리 */
|
| 274 |
-
border-top-right-radius: 11px;
|
| 275 |
-
}
|
| 276 |
-
/* μμ½λμΈ λ΄μ© μμ μ€νμΌ */
|
| 277 |
-
.gradio-accordion .wrap {
|
| 278 |
-
padding: 15px; /* λ΄μ© ν¨λ© */
|
| 279 |
-
background-color: #fefefe; /* λ΄μ© λ°°κ²½μ */
|
| 280 |
-
border-bottom-left-radius: 11px; /* νλ¨ λͺ¨μ리 */
|
| 281 |
-
border-bottom-right-radius: 11px;
|
| 282 |
-
}
|
| 283 |
-
/* μΆκ° μ€μ λ΄ κ°λ³ μ
λ ₯ μ»΄ν¬λνΈ μ€οΏ½οΏ½οΏ½μΌ (μ¬λΌμ΄λ, ν
μ€νΈλ°μ€ λ±) */
|
| 284 |
-
.gradio-slider, .gradio-textbox, .gradio-number {
|
| 285 |
-
margin-bottom: 10px; /* κ° μ
λ ₯ μμ μλ κ°κ²© */
|
| 286 |
-
padding: 8px; /* λ΄λΆ ν¨λ© */
|
| 287 |
-
border: 1px solid #e0e0e0; /* κ²½κ³μ */
|
| 288 |
-
border-radius: 8px; /* λ₯κ·Ό λͺ¨μ리 */
|
| 289 |
-
background-color: #fff; /* λ°°κ²½μ */
|
| 290 |
-
}
|
| 291 |
-
/* μ
λ ₯ νλ λΌλ²¨ μ€νμΌ */
|
| 292 |
-
.gradio-label {
|
| 293 |
-
font-weight: normal; /* λΌλ²¨ ν°νΈ κ΅΅κΈ° */
|
| 294 |
-
margin-bottom: 5px; /* λΌλ²¨κ³Ό μ
λ ₯ νλ κ° κ°κ²© */
|
| 295 |
-
color: #555; /* λΌλ²¨ μμ */
|
| 296 |
-
display: block; /* λΌλ²¨μ λΈλ‘ μμλ‘ λ§λ€μ΄ μλ‘ μ¬λ¦Ό */
|
| 297 |
-
}
|
| 298 |
-
/* μ¬λΌμ΄λ νΈλ λ° νΈλ€ μ€νμΌ (λ μΈλ°ν μ‘°μ κ°λ₯) */
|
| 299 |
-
/* μ: .gradio-slider input[type="range"]::-webkit-slider-thumb {} */
|
| 300 |
-
|
| 301 |
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
|
| 310 |
-
# Gradio μΈν°νμ΄μ€ μ€μ
|
| 311 |
demo = gr.ChatInterface(
|
| 312 |
fn=respond,
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
description="<p style='text-align: center; color: #555;'>This chat application is powered by Microsoft's SOTA Bitnet-b1.58-2B-4T and designed for natural and fast conversations.</p>",
|
| 316 |
examples=[
|
| 317 |
[
|
| 318 |
-
"Hello!
|
| 319 |
-
"You are a helpful AI
|
| 320 |
512,
|
| 321 |
0.7,
|
| 322 |
0.95,
|
| 323 |
],
|
| 324 |
[
|
| 325 |
-
"Can you code a snake game
|
| 326 |
-
"You are a helpful AI
|
| 327 |
2048,
|
| 328 |
0.7,
|
| 329 |
0.95,
|
|
@@ -332,8 +101,7 @@ demo = gr.ChatInterface(
|
|
| 332 |
additional_inputs=[
|
| 333 |
gr.Textbox(
|
| 334 |
value="You are a helpful AI assistant.",
|
| 335 |
-
label="System message"
|
| 336 |
-
lines=3 # μμ€ν
λ©μμ§ μ
λ ₯μ°½ λμ΄ μ‘°μ
|
| 337 |
),
|
| 338 |
gr.Slider(
|
| 339 |
minimum=1,
|
|
@@ -357,14 +125,7 @@ demo = gr.ChatInterface(
|
|
| 357 |
label="Top-p (nucleus sampling)"
|
| 358 |
),
|
| 359 |
],
|
| 360 |
-
# ν
λ§ μ μ© (μ¬λ¬ ν
λ§ μ€ μ ν κ°λ₯: gr.themes.Soft(), gr.themes.Glass(), gr.themes.Default(), etc.)
|
| 361 |
-
theme=gr.themes.Soft(),
|
| 362 |
-
# 컀μ€ν
CSS μ μ©
|
| 363 |
-
css=css_styles,
|
| 364 |
)
|
| 365 |
|
| 366 |
-
# μ ν리μΌμ΄μ
μ€ν
|
| 367 |
if __name__ == "__main__":
|
| 368 |
-
|
| 369 |
-
demo.launch()
|
| 370 |
-
# demo.launch(debug=True) # λλ²κΉ
λͺ¨λ νμ±ν
|
|
|
|
| 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
|
|
|
|
| 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]],
|
|
|
|
| 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:
|
|
|
|
| 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",
|
| 84 |
+
description="Bitnet-b1.58-2B-4T",
|
|
|
|
| 85 |
examples=[
|
| 86 |
[
|
| 87 |
+
"Hello!",
|
| 88 |
+
"You are a helpful AI.",
|
| 89 |
512,
|
| 90 |
0.7,
|
| 91 |
0.95,
|
| 92 |
],
|
| 93 |
[
|
| 94 |
+
"Can you code a snake game?",
|
| 95 |
+
"You are a helpful AI.",
|
| 96 |
2048,
|
| 97 |
0.7,
|
| 98 |
0.95,
|
|
|
|
| 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,
|
|
|
|
| 125 |
label="Top-p (nucleus sampling)"
|
| 126 |
),
|
| 127 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
)
|
| 129 |
|
|
|
|
| 130 |
if __name__ == "__main__":
|
| 131 |
+
demo.launch()
|
|
|
|
|
|