Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,19 @@ import torch
|
|
| 3 |
import spaces
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
from transformers import
|
| 7 |
-
AutoTokenizer,
|
| 8 |
-
AutoModelForCausalLM,
|
| 9 |
-
)
|
| 10 |
|
| 11 |
|
| 12 |
-
# =====================================================
|
| 13 |
# CONFIG
|
| 14 |
-
# =====================================================
|
| 15 |
|
| 16 |
MODEL_ID = os.getenv(
|
| 17 |
"MODEL_ID",
|
| 18 |
"WeiboAI/VibeThinker-3B"
|
| 19 |
)
|
| 20 |
|
|
|
|
| 21 |
DEVICE = (
|
| 22 |
"cuda"
|
| 23 |
if torch.cuda.is_available()
|
|
@@ -25,16 +23,13 @@ DEVICE = (
|
|
| 25 |
)
|
| 26 |
|
| 27 |
|
| 28 |
-
print("=" * 60)
|
| 29 |
-
print("X-RUDRA MODEL SPACE")
|
| 30 |
print("MODEL:", MODEL_ID)
|
| 31 |
print("DEVICE:", DEVICE)
|
| 32 |
-
print("=" * 60)
|
| 33 |
|
| 34 |
|
| 35 |
-
# =====================================================
|
| 36 |
-
# LOAD
|
| 37 |
-
# =====================================================
|
| 38 |
|
| 39 |
print("Loading tokenizer...")
|
| 40 |
|
|
@@ -49,12 +44,12 @@ print("Loading model...")
|
|
| 49 |
|
| 50 |
model = AutoModelForCausalLM.from_pretrained(
|
| 51 |
MODEL_ID,
|
|
|
|
| 52 |
torch_dtype=(
|
| 53 |
torch.float16
|
| 54 |
if DEVICE == "cuda"
|
| 55 |
else torch.float32
|
| 56 |
),
|
| 57 |
-
device_map="auto",
|
| 58 |
trust_remote_code=True,
|
| 59 |
)
|
| 60 |
|
|
@@ -65,9 +60,9 @@ model.eval()
|
|
| 65 |
print("MODEL READY")
|
| 66 |
|
| 67 |
|
| 68 |
-
# =====================================================
|
| 69 |
-
#
|
| 70 |
-
# =====================================================
|
| 71 |
|
| 72 |
|
| 73 |
@spaces.GPU
|
|
@@ -78,15 +73,16 @@ def generate(
|
|
| 78 |
):
|
| 79 |
|
| 80 |
if not prompt.strip():
|
|
|
|
| 81 |
|
| 82 |
-
return "Enter prompt."
|
| 83 |
|
|
|
|
| 84 |
|
| 85 |
inputs = tokenizer(
|
| 86 |
prompt,
|
| 87 |
return_tensors="pt",
|
| 88 |
-
padding=True,
|
| 89 |
truncation=True,
|
|
|
|
| 90 |
)
|
| 91 |
|
| 92 |
|
|
@@ -96,7 +92,6 @@ def generate(
|
|
| 96 |
}
|
| 97 |
|
| 98 |
|
| 99 |
-
# FIXED
|
| 100 |
input_length = (
|
| 101 |
inputs["input_ids"]
|
| 102 |
.shape[-1]
|
|
@@ -105,7 +100,7 @@ def generate(
|
|
| 105 |
|
| 106 |
with torch.no_grad():
|
| 107 |
|
| 108 |
-
|
| 109 |
|
| 110 |
**inputs,
|
| 111 |
|
|
@@ -119,6 +114,18 @@ def generate(
|
|
| 119 |
|
| 120 |
do_sample=True,
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
pad_token_id=(
|
| 123 |
tokenizer.eos_token_id
|
| 124 |
),
|
|
@@ -126,7 +133,7 @@ def generate(
|
|
| 126 |
|
| 127 |
|
| 128 |
answer = tokenizer.decode(
|
| 129 |
-
|
| 130 |
skip_special_tokens=True,
|
| 131 |
)
|
| 132 |
|
|
@@ -135,38 +142,77 @@ def generate(
|
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
-
# =====================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
# UI
|
| 140 |
-
# =====================================================
|
| 141 |
|
| 142 |
|
| 143 |
with gr.Blocks(
|
| 144 |
-
title="X-RUDRA
|
| 145 |
) as demo:
|
| 146 |
|
| 147 |
|
| 148 |
gr.Markdown(
|
| 149 |
f"""
|
| 150 |
-
# ⚡ X-RUDRA
|
| 151 |
|
| 152 |
-
|
| 153 |
|
| 154 |
-
`{
|
| 155 |
-
|
| 156 |
-
Device:
|
| 157 |
-
|
| 158 |
-
`{DEVICE}`
|
| 159 |
"""
|
| 160 |
)
|
| 161 |
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
lines=6,
|
| 166 |
-
placeholder="Ask something..."
|
| 167 |
)
|
| 168 |
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
with gr.Row():
|
| 171 |
|
| 172 |
max_tokens = gr.Slider(
|
|
@@ -174,48 +220,56 @@ Device:
|
|
| 174 |
2048,
|
| 175 |
value=512,
|
| 176 |
step=64,
|
| 177 |
-
label="Max Tokens"
|
| 178 |
)
|
| 179 |
|
| 180 |
|
| 181 |
temperature = gr.Slider(
|
| 182 |
0.1,
|
| 183 |
-
1.
|
| 184 |
value=0.7,
|
| 185 |
step=0.1,
|
| 186 |
-
label="Temperature"
|
| 187 |
)
|
| 188 |
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
)
|
| 199 |
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
inputs=[
|
| 204 |
-
|
|
|
|
| 205 |
max_tokens,
|
| 206 |
temperature,
|
| 207 |
],
|
| 208 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 209 |
)
|
| 210 |
|
| 211 |
|
| 212 |
-
|
|
|
|
| 213 |
# START
|
| 214 |
-
# =====================================================
|
| 215 |
|
| 216 |
if __name__ == "__main__":
|
| 217 |
|
| 218 |
-
|
| 219 |
demo.launch(
|
| 220 |
server_name="0.0.0.0",
|
| 221 |
server_port=7860,
|
|
|
|
| 3 |
import spaces
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
+
# =====================================================
|
| 10 |
# CONFIG
|
| 11 |
+
# =====================================================
|
| 12 |
|
| 13 |
MODEL_ID = os.getenv(
|
| 14 |
"MODEL_ID",
|
| 15 |
"WeiboAI/VibeThinker-3B"
|
| 16 |
)
|
| 17 |
|
| 18 |
+
|
| 19 |
DEVICE = (
|
| 20 |
"cuda"
|
| 21 |
if torch.cuda.is_available()
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
| 26 |
print("MODEL:", MODEL_ID)
|
| 27 |
print("DEVICE:", DEVICE)
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
+
# =====================================================
|
| 31 |
+
# LOAD
|
| 32 |
+
# =====================================================
|
| 33 |
|
| 34 |
print("Loading tokenizer...")
|
| 35 |
|
|
|
|
| 44 |
|
| 45 |
model = AutoModelForCausalLM.from_pretrained(
|
| 46 |
MODEL_ID,
|
| 47 |
+
device_map="auto",
|
| 48 |
torch_dtype=(
|
| 49 |
torch.float16
|
| 50 |
if DEVICE == "cuda"
|
| 51 |
else torch.float32
|
| 52 |
),
|
|
|
|
| 53 |
trust_remote_code=True,
|
| 54 |
)
|
| 55 |
|
|
|
|
| 60 |
print("MODEL READY")
|
| 61 |
|
| 62 |
|
| 63 |
+
# =====================================================
|
| 64 |
+
# GENERATE
|
| 65 |
+
# =====================================================
|
| 66 |
|
| 67 |
|
| 68 |
@spaces.GPU
|
|
|
|
| 73 |
):
|
| 74 |
|
| 75 |
if not prompt.strip():
|
| 76 |
+
return "Enter prompt"
|
| 77 |
|
|
|
|
| 78 |
|
| 79 |
+
# Normal tokenizer input
|
| 80 |
|
| 81 |
inputs = tokenizer(
|
| 82 |
prompt,
|
| 83 |
return_tensors="pt",
|
|
|
|
| 84 |
truncation=True,
|
| 85 |
+
max_length=4096,
|
| 86 |
)
|
| 87 |
|
| 88 |
|
|
|
|
| 92 |
}
|
| 93 |
|
| 94 |
|
|
|
|
| 95 |
input_length = (
|
| 96 |
inputs["input_ids"]
|
| 97 |
.shape[-1]
|
|
|
|
| 100 |
|
| 101 |
with torch.no_grad():
|
| 102 |
|
| 103 |
+
output = model.generate(
|
| 104 |
|
| 105 |
**inputs,
|
| 106 |
|
|
|
|
| 114 |
|
| 115 |
do_sample=True,
|
| 116 |
|
| 117 |
+
top_p=0.95,
|
| 118 |
+
|
| 119 |
+
top_k=50,
|
| 120 |
+
|
| 121 |
+
repetition_penalty=1.1,
|
| 122 |
+
|
| 123 |
+
no_repeat_ngram_size=3,
|
| 124 |
+
|
| 125 |
+
eos_token_id=(
|
| 126 |
+
tokenizer.eos_token_id
|
| 127 |
+
),
|
| 128 |
+
|
| 129 |
pad_token_id=(
|
| 130 |
tokenizer.eos_token_id
|
| 131 |
),
|
|
|
|
| 133 |
|
| 134 |
|
| 135 |
answer = tokenizer.decode(
|
| 136 |
+
output[0][input_length:],
|
| 137 |
skip_special_tokens=True,
|
| 138 |
)
|
| 139 |
|
|
|
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
+
# =====================================================
|
| 146 |
+
# GRADIO CHAT
|
| 147 |
+
# =====================================================
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def chat(
|
| 151 |
+
message,
|
| 152 |
+
history,
|
| 153 |
+
max_tokens,
|
| 154 |
+
temperature,
|
| 155 |
+
):
|
| 156 |
+
|
| 157 |
+
response = generate(
|
| 158 |
+
message,
|
| 159 |
+
max_tokens,
|
| 160 |
+
temperature,
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
history = history or []
|
| 164 |
+
|
| 165 |
+
history.append(
|
| 166 |
+
(
|
| 167 |
+
message,
|
| 168 |
+
response,
|
| 169 |
+
)
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
return "", history
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
# =====================================================
|
| 177 |
# UI
|
| 178 |
+
# =====================================================
|
| 179 |
|
| 180 |
|
| 181 |
with gr.Blocks(
|
| 182 |
+
title="X-RUDRA MODEL"
|
| 183 |
) as demo:
|
| 184 |
|
| 185 |
|
| 186 |
gr.Markdown(
|
| 187 |
f"""
|
| 188 |
+
# ⚡ X-RUDRA MODEL
|
| 189 |
|
| 190 |
+
**{MODEL_ID}**
|
| 191 |
|
| 192 |
+
Device: `{DEVICE}`
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
"""
|
| 194 |
)
|
| 195 |
|
| 196 |
|
| 197 |
+
chatbot = gr.Chatbot(
|
| 198 |
+
height=600,
|
|
|
|
|
|
|
| 199 |
)
|
| 200 |
|
| 201 |
|
| 202 |
+
with gr.Row():
|
| 203 |
+
|
| 204 |
+
message = gr.Textbox(
|
| 205 |
+
placeholder="Ask something...",
|
| 206 |
+
scale=8,
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
send = gr.Button(
|
| 210 |
+
"Send",
|
| 211 |
+
variant="primary",
|
| 212 |
+
scale=1,
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
|
| 216 |
with gr.Row():
|
| 217 |
|
| 218 |
max_tokens = gr.Slider(
|
|
|
|
| 220 |
2048,
|
| 221 |
value=512,
|
| 222 |
step=64,
|
| 223 |
+
label="Max Tokens",
|
| 224 |
)
|
| 225 |
|
| 226 |
|
| 227 |
temperature = gr.Slider(
|
| 228 |
0.1,
|
| 229 |
+
1.2,
|
| 230 |
value=0.7,
|
| 231 |
step=0.1,
|
| 232 |
+
label="Temperature",
|
| 233 |
)
|
| 234 |
|
| 235 |
|
| 236 |
+
send.click(
|
| 237 |
+
chat,
|
| 238 |
+
inputs=[
|
| 239 |
+
message,
|
| 240 |
+
chatbot,
|
| 241 |
+
max_tokens,
|
| 242 |
+
temperature,
|
| 243 |
+
],
|
| 244 |
+
outputs=[
|
| 245 |
+
message,
|
| 246 |
+
chatbot,
|
| 247 |
+
],
|
| 248 |
)
|
| 249 |
|
| 250 |
|
| 251 |
+
message.submit(
|
| 252 |
+
chat,
|
| 253 |
inputs=[
|
| 254 |
+
message,
|
| 255 |
+
chatbot,
|
| 256 |
max_tokens,
|
| 257 |
temperature,
|
| 258 |
],
|
| 259 |
+
outputs=[
|
| 260 |
+
message,
|
| 261 |
+
chatbot,
|
| 262 |
+
],
|
| 263 |
)
|
| 264 |
|
| 265 |
|
| 266 |
+
|
| 267 |
+
# =====================================================
|
| 268 |
# START
|
| 269 |
+
# =====================================================
|
| 270 |
|
| 271 |
if __name__ == "__main__":
|
| 272 |
|
|
|
|
| 273 |
demo.launch(
|
| 274 |
server_name="0.0.0.0",
|
| 275 |
server_port=7860,
|