Shrijanagain commited on
Commit
c590226
·
verified ·
1 Parent(s): b7a24a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -291
app.py CHANGED
@@ -1,9 +1,7 @@
1
  import os
2
- import time
3
-
4
  import torch
5
- import gradio as gr
6
  import spaces
 
7
 
8
  from transformers import (
9
  AutoTokenizer,
@@ -17,10 +15,9 @@ from transformers import (
17
 
18
  MODEL_ID = os.getenv(
19
  "MODEL_ID",
20
- "WeiboAI/VibeThinker-3B",
21
  )
22
 
23
-
24
  DEVICE = (
25
  "cuda"
26
  if torch.cuda.is_available()
@@ -29,83 +26,43 @@ DEVICE = (
29
 
30
 
31
  print("=" * 60)
32
- print("X-RUDRA MODEL A")
33
  print("MODEL:", MODEL_ID)
34
  print("DEVICE:", DEVICE)
35
  print("=" * 60)
36
 
37
 
38
-
39
  # ============================================================
40
- # LAZY MODEL STORAGE
41
  # ============================================================
42
 
43
- tokenizer = None
44
- model = None
45
-
46
-
47
-
48
- def load_model():
49
-
50
- global tokenizer
51
- global model
52
-
53
-
54
- if model is not None:
55
- return
56
-
57
-
58
- print("Loading tokenizer...")
59
-
60
-
61
- tokenizer = AutoTokenizer.from_pretrained(
62
- MODEL_ID,
63
- trust_remote_code=True,
64
- )
65
-
66
-
67
- model_kwargs = {
68
- "trust_remote_code": True,
69
- }
70
-
71
-
72
- if torch.cuda.is_available():
73
 
74
- model_kwargs["device_map"] = "auto"
75
-
76
- model_kwargs["torch_dtype"] = (
77
- torch.bfloat16
78
- )
79
-
80
- else:
81
-
82
- model_kwargs["torch_dtype"] = (
83
- torch.float32
84
- )
85
-
86
-
87
-
88
- print("Loading model...")
89
-
90
-
91
- model = AutoModelForCausalLM.from_pretrained(
92
- MODEL_ID,
93
- **model_kwargs,
94
- )
95
 
96
 
97
- model.eval()
98
 
99
 
100
- if tokenizer.pad_token_id is None:
 
 
 
 
 
 
 
 
 
101
 
102
- tokenizer.pad_token = (
103
- tokenizer.eos_token
104
- )
105
 
 
106
 
107
- print("MODEL READY")
108
 
 
109
 
110
 
111
  # ============================================================
@@ -113,329 +70,153 @@ def load_model():
113
  # ============================================================
114
 
115
 
116
- @spaces.GPU(
117
- duration=120
118
- )
119
  def generate(
120
  prompt,
121
- max_new_tokens,
122
  temperature,
123
- top_p,
124
  ):
125
 
 
126
 
127
- if not prompt or not prompt.strip():
128
-
129
- return (
130
- "ERROR: prompt is empty."
131
- )
132
-
133
-
134
- load_model()
135
-
136
 
137
 
138
- messages = [
139
- {
140
- "role": "user",
141
- "content": prompt.strip(),
142
- }
143
- ]
144
-
145
-
146
-
147
- if hasattr(
148
- tokenizer,
149
- "apply_chat_template"
150
- ):
151
-
152
-
153
- inputs = tokenizer.apply_chat_template(
154
-
155
- messages,
156
-
157
- tokenize=True,
158
-
159
- add_generation_prompt=True,
160
-
161
- return_tensors="pt",
162
- )
163
-
164
-
165
- else:
166
-
167
-
168
- inputs = tokenizer(
169
- prompt,
170
- return_tensors="pt",
171
- ).input_ids
172
-
173
-
174
-
175
-
176
- if torch.cuda.is_available():
177
-
178
- inputs = inputs.to(
179
- model.device
180
- )
181
-
182
- else:
183
-
184
- inputs = inputs.to(
185
- DEVICE
186
- )
187
 
188
 
 
 
 
 
189
 
190
- input_length = inputs.shape[-1]
191
 
 
 
 
 
 
192
 
193
 
194
- with torch.inference_mode():
195
 
196
- output = model.generate(
197
 
198
- inputs,
199
 
200
  max_new_tokens=int(
201
- max_new_tokens
202
  ),
203
 
204
  temperature=float(
205
  temperature
206
  ),
207
 
208
- top_p=float(
209
- top_p
210
- ),
211
-
212
-
213
- do_sample=(
214
- float(temperature) > 0
215
- ),
216
-
217
 
218
  pad_token_id=(
219
- tokenizer.pad_token_id
220
- ),
221
-
222
-
223
- eos_token_id=(
224
  tokenizer.eos_token_id
225
  ),
226
  )
227
 
228
 
229
-
230
- generated = output[0][
231
- input_length:
232
- ]
233
-
234
-
235
-
236
- text = tokenizer.decode(
237
-
238
- generated,
239
-
240
  skip_special_tokens=True,
241
-
242
  )
243
 
244
 
245
-
246
- return text.strip()
247
 
248
 
249
 
250
  # ============================================================
251
- # HEALTH
252
- # ============================================================
253
-
254
-
255
- def health():
256
-
257
- return {
258
-
259
- "status": "ok",
260
-
261
- "model": MODEL_ID,
262
-
263
- "device": DEVICE,
264
-
265
- "cuda": torch.cuda.is_available(),
266
-
267
- "timestamp": time.time(),
268
-
269
- }
270
-
271
-
272
-
273
- # ============================================================
274
- # GRADIO UI
275
  # ============================================================
276
 
277
 
278
  with gr.Blocks(
279
- title="X-RUDRA Model A"
280
  ) as demo:
281
 
282
 
283
  gr.Markdown(
284
  f"""
285
- # ⚡ X-RUDRA — Model A
286
-
287
- ### WeiboAI/VibeThinker-3B
288
 
289
- Independent reasoning model.
290
-
291
- **Model ID**
292
 
293
  `{MODEL_ID}`
294
 
295
- **Device**
296
 
297
  `{DEVICE}`
298
  """
299
  )
300
 
301
 
302
-
303
  prompt = gr.Textbox(
304
-
305
  label="Prompt",
306
-
307
- placeholder=(
308
- "Ask anything..."
309
- ),
310
-
311
- lines=8,
312
-
313
  )
314
 
315
 
316
-
317
  with gr.Row():
318
 
319
-
320
  max_tokens = gr.Slider(
321
-
322
- minimum=128,
323
-
324
- maximum=4096,
325
-
326
- value=1024,
327
-
328
- step=128,
329
-
330
- label="Max tokens",
331
-
332
  )
333
 
334
 
335
  temperature = gr.Slider(
336
-
337
- minimum=0,
338
-
339
- maximum=1.5,
340
-
341
  value=0.7,
342
-
343
- step=0.05,
344
-
345
- label="Temperature",
346
-
347
  )
348
 
349
 
350
- top_p = gr.Slider(
351
-
352
- minimum=0.1,
353
-
354
- maximum=1.0,
355
-
356
- value=0.9,
357
-
358
- step=0.05,
359
-
360
- label="Top P",
361
-
362
- )
363
-
364
-
365
-
366
- button = gr.Button(
367
-
368
- "🚀 Generate",
369
-
370
- variant="primary",
371
-
372
  )
373
 
374
 
375
-
376
- output = gr.Textbox(
377
-
378
- label="Response",
379
-
380
- lines=20,
381
-
382
  )
383
 
384
 
385
-
386
- button.click(
387
-
388
  generate,
389
-
390
  inputs=[
391
-
392
  prompt,
393
-
394
  max_tokens,
395
-
396
  temperature,
397
-
398
- top_p,
399
-
400
  ],
401
-
402
  outputs=output,
403
-
404
- api_name="generate",
405
-
406
- )
407
-
408
-
409
-
410
- gr.JSON(
411
-
412
- value=health(),
413
-
414
- label="Health",
415
-
416
  )
417
 
418
 
419
-
420
  # ============================================================
421
  # START
422
  # ============================================================
423
 
424
-
425
  if __name__ == "__main__":
426
 
427
 
428
  demo.launch(
429
-
430
  server_name="0.0.0.0",
431
-
432
- server_port=int(
433
-
434
- os.getenv(
435
- "PORT",
436
- "7860"
437
- )
438
-
439
- ),
440
-
441
  )
 
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
+ "WeiboAI/VibeThinker-3B"
19
  )
20
 
 
21
  DEVICE = (
22
  "cuda"
23
  if torch.cuda.is_available()
 
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
  )