Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
-
import time
|
| 3 |
import torch
|
| 4 |
-
import gradio as gr
|
| 5 |
import spaces
|
|
|
|
| 6 |
|
| 7 |
from transformers import (
|
| 8 |
AutoTokenizer,
|
|
@@ -16,70 +15,54 @@ from transformers import (
|
|
| 16 |
|
| 17 |
MODEL_ID = os.getenv(
|
| 18 |
"MODEL_ID",
|
| 19 |
-
"LiquidAI/LFM2-2.6B"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
|
| 22 |
-
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
|
|
|
|
| 24 |
print("X-RUDRA MODEL SPACE")
|
| 25 |
print("MODEL:", MODEL_ID)
|
| 26 |
print("DEVICE:", DEVICE)
|
|
|
|
| 27 |
|
| 28 |
|
| 29 |
# ============================================================
|
| 30 |
-
#
|
| 31 |
# ============================================================
|
| 32 |
|
| 33 |
-
|
| 34 |
-
model = None
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
def load_model():
|
| 38 |
-
|
| 39 |
-
global tokenizer
|
| 40 |
-
global model
|
| 41 |
-
|
| 42 |
-
if model is not None:
|
| 43 |
-
return
|
| 44 |
-
|
| 45 |
-
print("Loading tokenizer...")
|
| 46 |
-
|
| 47 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 48 |
-
MODEL_ID,
|
| 49 |
-
trust_remote_code=True,
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
kwargs = {
|
| 53 |
-
"trust_remote_code": True,
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
if torch.cuda.is_available():
|
| 58 |
-
|
| 59 |
-
kwargs["device_map"] = "auto"
|
| 60 |
-
kwargs["torch_dtype"] = torch.bfloat16
|
| 61 |
-
|
| 62 |
-
else:
|
| 63 |
-
|
| 64 |
-
kwargs["torch_dtype"] = torch.float32
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
print("Loading model...")
|
| 68 |
|
| 69 |
-
|
| 70 |
-
MODEL_ID,
|
| 71 |
-
**kwargs,
|
| 72 |
-
)
|
| 73 |
|
| 74 |
-
model.eval()
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
if tokenizer.pad_token_id is None:
|
| 78 |
|
| 79 |
-
|
| 80 |
|
| 81 |
|
| 82 |
-
|
| 83 |
|
| 84 |
|
| 85 |
# ============================================================
|
|
@@ -87,268 +70,153 @@ def load_model():
|
|
| 87 |
# ============================================================
|
| 88 |
|
| 89 |
|
| 90 |
-
@spaces.GPU
|
| 91 |
-
duration=120
|
| 92 |
-
)
|
| 93 |
def generate(
|
| 94 |
prompt,
|
| 95 |
-
|
| 96 |
temperature,
|
| 97 |
-
top_p,
|
| 98 |
):
|
| 99 |
|
| 100 |
-
if not prompt
|
| 101 |
-
|
| 102 |
-
return "ERROR: Empty prompt"
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
load_model()
|
| 106 |
|
|
|
|
| 107 |
|
| 108 |
-
messages = [
|
| 109 |
-
{
|
| 110 |
-
"role": "user",
|
| 111 |
-
"content": prompt.strip(),
|
| 112 |
-
}
|
| 113 |
-
]
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
messages,
|
| 118 |
-
tokenize=True,
|
| 119 |
-
add_generation_prompt=True,
|
| 120 |
return_tensors="pt",
|
|
|
|
|
|
|
| 121 |
)
|
| 122 |
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
)
|
| 129 |
-
|
| 130 |
-
else:
|
| 131 |
-
|
| 132 |
-
inputs = inputs.to(
|
| 133 |
-
DEVICE
|
| 134 |
-
)
|
| 135 |
|
| 136 |
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
|
| 140 |
-
with torch.
|
| 141 |
|
| 142 |
-
|
| 143 |
|
| 144 |
-
inputs,
|
| 145 |
|
| 146 |
max_new_tokens=int(
|
| 147 |
-
|
| 148 |
),
|
| 149 |
|
| 150 |
temperature=float(
|
| 151 |
temperature
|
| 152 |
),
|
| 153 |
|
| 154 |
-
|
| 155 |
-
top_p
|
| 156 |
-
),
|
| 157 |
-
|
| 158 |
-
do_sample=(
|
| 159 |
-
float(temperature) > 0
|
| 160 |
-
),
|
| 161 |
|
| 162 |
pad_token_id=(
|
| 163 |
-
tokenizer.pad_token_id
|
| 164 |
-
),
|
| 165 |
-
|
| 166 |
-
eos_token_id=(
|
| 167 |
tokenizer.eos_token_id
|
| 168 |
),
|
| 169 |
)
|
| 170 |
|
| 171 |
|
| 172 |
-
|
| 173 |
-
input_length:
|
| 174 |
-
]
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
text = tokenizer.decode(
|
| 178 |
-
generated,
|
| 179 |
skip_special_tokens=True,
|
| 180 |
)
|
| 181 |
|
| 182 |
|
| 183 |
-
return
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
# ============================================================
|
| 188 |
-
# HEALTH
|
| 189 |
-
# ============================================================
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
def health():
|
| 193 |
-
|
| 194 |
-
return {
|
| 195 |
-
"status": "ok",
|
| 196 |
-
"model": MODEL_ID,
|
| 197 |
-
"device": DEVICE,
|
| 198 |
-
"cuda": torch.cuda.is_available(),
|
| 199 |
-
"timestamp": time.time(),
|
| 200 |
-
}
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
# ============================================================
|
| 205 |
-
#
|
| 206 |
# ============================================================
|
| 207 |
|
| 208 |
|
| 209 |
with gr.Blocks(
|
| 210 |
-
title="X-RUDRA
|
| 211 |
) as demo:
|
| 212 |
|
| 213 |
|
| 214 |
gr.Markdown(
|
| 215 |
-
"""
|
| 216 |
-
# ⚡ X-RUDRA
|
| 217 |
-
|
| 218 |
-
Independent reasoning model endpoint.
|
| 219 |
|
| 220 |
Model:
|
| 221 |
-
|
|
|
|
| 222 |
|
| 223 |
Device:
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
DEVICE,
|
| 228 |
-
)
|
| 229 |
)
|
| 230 |
|
| 231 |
|
| 232 |
prompt = gr.Textbox(
|
| 233 |
-
|
| 234 |
label="Prompt",
|
| 235 |
-
|
| 236 |
-
placeholder=
|
| 237 |
-
"Enter your question..."
|
| 238 |
-
),
|
| 239 |
-
|
| 240 |
-
lines=8,
|
| 241 |
)
|
| 242 |
|
| 243 |
|
| 244 |
with gr.Row():
|
| 245 |
|
| 246 |
-
|
| 247 |
max_tokens = gr.Slider(
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
value=1024,
|
| 254 |
-
|
| 255 |
-
step=128,
|
| 256 |
-
|
| 257 |
-
label="Max Tokens",
|
| 258 |
)
|
| 259 |
|
| 260 |
|
| 261 |
temperature = gr.Slider(
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
maximum=1.5,
|
| 266 |
-
|
| 267 |
value=0.7,
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
label="Temperature",
|
| 272 |
-
)
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
top_p = gr.Slider(
|
| 276 |
-
|
| 277 |
-
minimum=0.1,
|
| 278 |
-
|
| 279 |
-
maximum=1,
|
| 280 |
-
|
| 281 |
-
value=0.9,
|
| 282 |
-
|
| 283 |
-
step=0.05,
|
| 284 |
-
|
| 285 |
-
label="Top P",
|
| 286 |
)
|
| 287 |
|
| 288 |
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
"🚀 Generate",
|
| 293 |
-
|
| 294 |
-
variant="primary",
|
| 295 |
)
|
| 296 |
|
| 297 |
|
| 298 |
-
output = gr.
|
| 299 |
-
|
| 300 |
-
label="Response",
|
| 301 |
-
|
| 302 |
-
lines=20,
|
| 303 |
)
|
| 304 |
|
| 305 |
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
fn=generate,
|
| 309 |
-
|
| 310 |
inputs=[
|
| 311 |
-
|
| 312 |
prompt,
|
| 313 |
-
|
| 314 |
max_tokens,
|
| 315 |
-
|
| 316 |
temperature,
|
| 317 |
-
|
| 318 |
-
top_p,
|
| 319 |
-
|
| 320 |
],
|
| 321 |
-
|
| 322 |
outputs=output,
|
| 323 |
-
|
| 324 |
-
api_name="generate",
|
| 325 |
-
)
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
gr.JSON(
|
| 329 |
-
health(),
|
| 330 |
-
label="Health"
|
| 331 |
)
|
| 332 |
|
| 333 |
|
| 334 |
-
|
| 335 |
# ============================================================
|
| 336 |
# START
|
| 337 |
# ============================================================
|
| 338 |
|
| 339 |
-
|
| 340 |
if __name__ == "__main__":
|
| 341 |
|
| 342 |
-
demo.launch(
|
| 343 |
|
|
|
|
| 344 |
server_name="0.0.0.0",
|
| 345 |
-
|
| 346 |
-
server_port=int(
|
| 347 |
-
|
| 348 |
-
os.getenv(
|
| 349 |
-
"PORT",
|
| 350 |
-
7860
|
| 351 |
-
)
|
| 352 |
-
|
| 353 |
-
),
|
| 354 |
)
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import torch
|
|
|
|
| 3 |
import spaces
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
from transformers import (
|
| 7 |
AutoTokenizer,
|
|
|
|
| 15 |
|
| 16 |
MODEL_ID = os.getenv(
|
| 17 |
"MODEL_ID",
|
| 18 |
+
"LiquidAI/LFM2-2.6B"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
DEVICE = (
|
| 22 |
+
"cuda"
|
| 23 |
+
if torch.cuda.is_available()
|
| 24 |
+
else "cpu"
|
| 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 MODEL
|
| 37 |
# ============================================================
|
| 38 |
|
| 39 |
+
print("Loading tokenizer...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 42 |
+
MODEL_ID,
|
| 43 |
+
trust_remote_code=True,
|
| 44 |
+
)
|
| 45 |
|
|
|
|
| 46 |
|
| 47 |
+
print("Loading model...")
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
| 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 |
|
|
|
|
| 61 |
|
| 62 |
+
model.eval()
|
| 63 |
|
| 64 |
|
| 65 |
+
print("MODEL READY")
|
| 66 |
|
| 67 |
|
| 68 |
# ============================================================
|
|
|
|
| 70 |
# ============================================================
|
| 71 |
|
| 72 |
|
| 73 |
+
@spaces.GPU
|
|
|
|
|
|
|
| 74 |
def generate(
|
| 75 |
prompt,
|
| 76 |
+
max_tokens,
|
| 77 |
temperature,
|
|
|
|
| 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 |
|
| 93 |
+
inputs = {
|
| 94 |
+
k: v.to(model.device)
|
| 95 |
+
for k, v in inputs.items()
|
| 96 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
|
| 99 |
+
# FIXED
|
| 100 |
+
input_length = (
|
| 101 |
+
inputs["input_ids"]
|
| 102 |
+
.shape[-1]
|
| 103 |
+
)
|
| 104 |
|
| 105 |
|
| 106 |
+
with torch.no_grad():
|
| 107 |
|
| 108 |
+
outputs = model.generate(
|
| 109 |
|
| 110 |
+
**inputs,
|
| 111 |
|
| 112 |
max_new_tokens=int(
|
| 113 |
+
max_tokens
|
| 114 |
),
|
| 115 |
|
| 116 |
temperature=float(
|
| 117 |
temperature
|
| 118 |
),
|
| 119 |
|
| 120 |
+
do_sample=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
pad_token_id=(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
tokenizer.eos_token_id
|
| 124 |
),
|
| 125 |
)
|
| 126 |
|
| 127 |
|
| 128 |
+
answer = tokenizer.decode(
|
| 129 |
+
outputs[0][input_length:],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
skip_special_tokens=True,
|
| 131 |
)
|
| 132 |
|
| 133 |
|
| 134 |
+
return answer.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
# ============================================================
|
| 139 |
+
# UI
|
| 140 |
# ============================================================
|
| 141 |
|
| 142 |
|
| 143 |
with gr.Blocks(
|
| 144 |
+
title="X-RUDRA M2"
|
| 145 |
) as demo:
|
| 146 |
|
| 147 |
|
| 148 |
gr.Markdown(
|
| 149 |
+
f"""
|
| 150 |
+
# ⚡ X-RUDRA M2
|
|
|
|
|
|
|
| 151 |
|
| 152 |
Model:
|
| 153 |
+
|
| 154 |
+
`{MODEL_ID}`
|
| 155 |
|
| 156 |
Device:
|
| 157 |
+
|
| 158 |
+
`{DEVICE}`
|
| 159 |
+
"""
|
|
|
|
|
|
|
| 160 |
)
|
| 161 |
|
| 162 |
|
| 163 |
prompt = gr.Textbox(
|
|
|
|
| 164 |
label="Prompt",
|
| 165 |
+
lines=6,
|
| 166 |
+
placeholder="Ask something..."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
)
|
| 168 |
|
| 169 |
|
| 170 |
with gr.Row():
|
| 171 |
|
|
|
|
| 172 |
max_tokens = gr.Slider(
|
| 173 |
+
64,
|
| 174 |
+
2048,
|
| 175 |
+
value=512,
|
| 176 |
+
step=64,
|
| 177 |
+
label="Max Tokens"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
)
|
| 179 |
|
| 180 |
|
| 181 |
temperature = gr.Slider(
|
| 182 |
+
0.1,
|
| 183 |
+
1.5,
|
|
|
|
|
|
|
|
|
|
| 184 |
value=0.7,
|
| 185 |
+
step=0.1,
|
| 186 |
+
label="Temperature"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
)
|
| 188 |
|
| 189 |
|
| 190 |
+
btn = gr.Button(
|
| 191 |
+
"Generate",
|
| 192 |
+
variant="primary"
|
|
|
|
|
|
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
|
| 196 |
+
output = gr.Markdown(
|
| 197 |
+
label="Response"
|
|
|
|
|
|
|
|
|
|
| 198 |
)
|
| 199 |
|
| 200 |
|
| 201 |
+
btn.click(
|
| 202 |
+
generate,
|
|
|
|
|
|
|
| 203 |
inputs=[
|
|
|
|
| 204 |
prompt,
|
|
|
|
| 205 |
max_tokens,
|
|
|
|
| 206 |
temperature,
|
|
|
|
|
|
|
|
|
|
| 207 |
],
|
|
|
|
| 208 |
outputs=output,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
)
|