Shrijanagain commited on
Commit
1298db5
·
verified ·
1 Parent(s): 7e303b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +231 -57
app.py CHANGED
@@ -2,9 +2,17 @@ import os
2
  import time
3
  import torch
4
  import gradio as gr
 
 
 
 
 
 
5
 
6
- from transformers import AutoTokenizer, AutoModelForCausalLM
7
 
 
 
 
8
 
9
  MODEL_ID = os.getenv(
10
  "MODEL_ID",
@@ -13,45 +21,89 @@ MODEL_ID = os.getenv(
13
 
14
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
15
 
16
- print(f"Loading model: {MODEL_ID}")
17
- print(f"Device: {DEVICE}")
 
18
 
19
 
20
- tokenizer = AutoTokenizer.from_pretrained(
21
- MODEL_ID,
22
- trust_remote_code=True,
23
- )
24
 
25
- model_kwargs = {
26
- "trust_remote_code": True,
27
- }
28
 
29
- if torch.cuda.is_available():
30
- model_kwargs["device_map"] = "auto"
31
- model_kwargs["torch_dtype"] = torch.bfloat16
32
- else:
33
- model_kwargs["torch_dtype"] = torch.float32
34
 
 
35
 
36
- model = AutoModelForCausalLM.from_pretrained(
37
- MODEL_ID,
38
- **model_kwargs,
39
- )
 
 
 
 
 
 
 
 
40
 
41
- model.eval()
 
 
 
 
 
 
 
 
 
 
42
 
43
- if tokenizer.pad_token_id is None:
44
- tokenizer.pad_token = tokenizer.eos_token
45
 
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  def generate(
48
- prompt: str,
49
- max_new_tokens: int = 1024,
50
- temperature: float = 0.7,
51
- top_p: float = 0.9,
52
  ):
 
53
  if not prompt or not prompt.strip():
54
- return "ERROR: prompt is empty."
 
 
 
 
 
55
 
56
  messages = [
57
  {
@@ -60,6 +112,7 @@ def generate(
60
  }
61
  ]
62
 
 
63
  inputs = tokenizer.apply_chat_template(
64
  messages,
65
  tokenize=True,
@@ -67,35 +120,77 @@ def generate(
67
  return_tensors="pt",
68
  )
69
 
 
70
  if torch.cuda.is_available():
71
- inputs = inputs.to(model.device)
 
 
 
 
72
  else:
73
- inputs = inputs.to(DEVICE)
 
 
 
 
74
 
75
  input_length = inputs.shape[-1]
76
 
 
77
  with torch.inference_mode():
 
78
  output = model.generate(
 
79
  inputs,
80
- max_new_tokens=int(max_new_tokens),
81
- temperature=float(temperature),
82
- top_p=float(top_p),
83
- do_sample=float(temperature) > 0,
84
- pad_token_id=tokenizer.pad_token_id,
85
- eos_token_id=tokenizer.eos_token_id,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  )
87
 
88
- generated = output[0][input_length:]
 
 
 
 
89
 
90
  text = tokenizer.decode(
91
  generated,
92
  skip_special_tokens=True,
93
  )
94
 
 
95
  return text.strip()
96
 
97
 
 
 
 
 
 
 
98
  def health():
 
99
  return {
100
  "status": "ok",
101
  "model": MODEL_ID,
@@ -105,76 +200,155 @@ def health():
105
  }
106
 
107
 
108
- with gr.Blocks(title="X RUDRA - LFM2 2.6B") as demo:
 
 
 
 
 
 
 
 
 
109
 
110
  gr.Markdown(
111
  """
112
- # ⚡ X RUDRA Model B
113
 
114
- **LiquidAI/LFM2-2.6B**
115
 
116
- Independent second-model reasoning endpoint.
117
- """
 
 
 
 
 
 
 
118
  )
119
 
 
120
  prompt = gr.Textbox(
 
121
  label="Prompt",
122
- placeholder="Enter a question...",
 
 
 
 
123
  lines=8,
124
  )
125
 
 
126
  with gr.Row():
 
 
127
  max_tokens = gr.Slider(
128
- 128,
129
- 4096,
 
 
 
130
  value=1024,
 
131
  step=128,
132
- label="Max new tokens",
 
133
  )
134
 
 
135
  temperature = gr.Slider(
136
- 0.0,
137
- 1.5,
 
 
 
138
  value=0.7,
 
139
  step=0.05,
 
140
  label="Temperature",
141
  )
142
 
 
143
  top_p = gr.Slider(
144
- 0.1,
145
- 1.0,
 
 
 
146
  value=0.9,
 
147
  step=0.05,
 
148
  label="Top P",
149
  )
150
 
151
- generate_button = gr.Button(
152
- "Generate",
 
 
 
 
153
  variant="primary",
154
  )
155
 
 
156
  output = gr.Textbox(
 
157
  label="Response",
 
158
  lines=20,
159
  )
160
 
161
- generate_button.click(
162
- generate,
 
 
 
163
  inputs=[
 
164
  prompt,
 
165
  max_tokens,
 
166
  temperature,
 
167
  top_p,
 
168
  ],
 
169
  outputs=output,
 
170
  api_name="generate",
171
  )
172
 
173
- gr.api(
174
- health,
175
- api_name="health",
 
176
  )
177
 
178
 
 
 
 
 
 
 
179
  if __name__ == "__main__":
180
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import time
3
  import torch
4
  import gradio as gr
5
+ import spaces
6
+
7
+ from transformers import (
8
+ AutoTokenizer,
9
+ AutoModelForCausalLM,
10
+ )
11
 
 
12
 
13
+ # ============================================================
14
+ # CONFIG
15
+ # ============================================================
16
 
17
  MODEL_ID = os.getenv(
18
  "MODEL_ID",
 
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
+ # LAZY MODEL LOADING
31
+ # ============================================================
 
32
 
33
+ tokenizer = None
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
+ model = AutoModelForCausalLM.from_pretrained(
70
+ MODEL_ID,
71
+ **kwargs,
72
+ )
73
+
74
+ model.eval()
75
+
76
+
77
+ if tokenizer.pad_token_id is None:
78
+
79
+ tokenizer.pad_token = tokenizer.eos_token
80
+
81
+
82
+ print("MODEL READY")
83
+
84
+
85
+ # ============================================================
86
+ # GENERATION
87
+ # ============================================================
88
+
89
+
90
+ @spaces.GPU(
91
+ duration=120
92
+ )
93
  def generate(
94
+ prompt,
95
+ max_new_tokens,
96
+ temperature,
97
+ top_p,
98
  ):
99
+
100
  if not prompt or not prompt.strip():
101
+
102
+ return "ERROR: Empty prompt"
103
+
104
+
105
+ load_model()
106
+
107
 
108
  messages = [
109
  {
 
112
  }
113
  ]
114
 
115
+
116
  inputs = tokenizer.apply_chat_template(
117
  messages,
118
  tokenize=True,
 
120
  return_tensors="pt",
121
  )
122
 
123
+
124
  if torch.cuda.is_available():
125
+
126
+ inputs = inputs.to(
127
+ model.device
128
+ )
129
+
130
  else:
131
+
132
+ inputs = inputs.to(
133
+ DEVICE
134
+ )
135
+
136
 
137
  input_length = inputs.shape[-1]
138
 
139
+
140
  with torch.inference_mode():
141
+
142
  output = model.generate(
143
+
144
  inputs,
145
+
146
+ max_new_tokens=int(
147
+ max_new_tokens
148
+ ),
149
+
150
+ temperature=float(
151
+ temperature
152
+ ),
153
+
154
+ top_p=float(
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
+ generated = output[0][
173
+ input_length:
174
+ ]
175
+
176
 
177
  text = tokenizer.decode(
178
  generated,
179
  skip_special_tokens=True,
180
  )
181
 
182
+
183
  return text.strip()
184
 
185
 
186
+
187
+ # ============================================================
188
+ # HEALTH
189
+ # ============================================================
190
+
191
+
192
  def health():
193
+
194
  return {
195
  "status": "ok",
196
  "model": MODEL_ID,
 
200
  }
201
 
202
 
203
+
204
+ # ============================================================
205
+ # GRADIO UI
206
+ # ============================================================
207
+
208
+
209
+ with gr.Blocks(
210
+ title="X-RUDRA M1/M2"
211
+ ) as demo:
212
+
213
 
214
  gr.Markdown(
215
  """
216
+ # ⚡ X-RUDRA Model Space
217
 
218
+ Independent reasoning model endpoint.
219
 
220
+ Model:
221
+ `{}`
222
+
223
+ Device:
224
+ `{}`
225
+ """.format(
226
+ MODEL_ID,
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
+ minimum=128,
250
+
251
+ maximum=4096,
252
+
253
  value=1024,
254
+
255
  step=128,
256
+
257
+ label="Max Tokens",
258
  )
259
 
260
+
261
  temperature = gr.Slider(
262
+
263
+ minimum=0,
264
+
265
+ maximum=1.5,
266
+
267
  value=0.7,
268
+
269
  step=0.05,
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
+ button = gr.Button(
291
+
292
+ "🚀 Generate",
293
+
294
  variant="primary",
295
  )
296
 
297
+
298
  output = gr.Textbox(
299
+
300
  label="Response",
301
+
302
  lines=20,
303
  )
304
 
305
+
306
+ button.click(
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
+ )