Shrijanagain commited on
Commit
fa67e34
·
verified ·
1 Parent(s): 96efac1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -55
app.py CHANGED
@@ -3,7 +3,10 @@ import torch
3
  import spaces
4
  import gradio as gr
5
 
6
- from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
 
7
 
8
 
9
  # =====================================================
@@ -15,32 +18,33 @@ MODEL_ID = os.getenv(
15
  "WeiboAI/VibeThinker-3B"
16
  )
17
 
18
-
19
- DEVICE = (
20
- "cuda"
21
- if torch.cuda.is_available()
22
- else "cpu"
23
- )
24
 
25
 
 
 
26
  print("MODEL:", MODEL_ID)
27
  print("DEVICE:", DEVICE)
 
28
 
29
 
30
  # =====================================================
31
- # LOAD
32
  # =====================================================
33
 
34
  print("Loading tokenizer...")
35
 
36
  tokenizer = AutoTokenizer.from_pretrained(
37
  MODEL_ID,
38
- trust_remote_code=True,
39
  )
40
 
41
 
42
- print("Loading model...")
 
 
43
 
 
44
 
45
  model = AutoModelForCausalLM.from_pretrained(
46
  MODEL_ID,
@@ -56,12 +60,11 @@ model = AutoModelForCausalLM.from_pretrained(
56
 
57
  model.eval()
58
 
59
-
60
  print("MODEL READY")
61
 
62
 
63
  # =====================================================
64
- # GENERATE
65
  # =====================================================
66
 
67
 
@@ -69,26 +72,24 @@ print("MODEL READY")
69
  def generate(
70
  prompt,
71
  max_tokens,
72
- temperature,
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
 
89
  inputs = {
90
- k: v.to(model.device)
91
- for k, v in inputs.items()
92
  }
93
 
94
 
@@ -100,7 +101,7 @@ def generate(
100
 
101
  with torch.no_grad():
102
 
103
- output = model.generate(
104
 
105
  **inputs,
106
 
@@ -118,7 +119,7 @@ def generate(
118
 
119
  top_k=50,
120
 
121
- repetition_penalty=1.1,
122
 
123
  no_repeat_ngram_size=3,
124
 
@@ -132,18 +133,18 @@ def generate(
132
  )
133
 
134
 
135
- answer = tokenizer.decode(
136
- output[0][input_length:],
137
- skip_special_tokens=True,
138
  )
139
 
140
 
141
- return answer.strip()
142
 
143
 
144
 
145
  # =====================================================
146
- # GRADIO CHAT
147
  # =====================================================
148
 
149
 
@@ -151,24 +152,35 @@ 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
 
@@ -179,93 +191,102 @@ def chat(
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(
219
- 64,
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__":
@@ -273,4 +294,5 @@ if __name__ == "__main__":
273
  demo.launch(
274
  server_name="0.0.0.0",
275
  server_port=7860,
 
276
  )
 
3
  import spaces
4
  import gradio as gr
5
 
6
+ from transformers import (
7
+ AutoTokenizer,
8
+ AutoModelForCausalLM,
9
+ )
10
 
11
 
12
  # =====================================================
 
18
  "WeiboAI/VibeThinker-3B"
19
  )
20
 
21
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
22
 
23
 
24
+ print("=" * 60)
25
+ print("X-RUDRA MODEL SPACE")
26
  print("MODEL:", MODEL_ID)
27
  print("DEVICE:", DEVICE)
28
+ print("=" * 60)
29
 
30
 
31
  # =====================================================
32
+ # LOAD TOKENIZER
33
  # =====================================================
34
 
35
  print("Loading tokenizer...")
36
 
37
  tokenizer = AutoTokenizer.from_pretrained(
38
  MODEL_ID,
39
+ trust_remote_code=True
40
  )
41
 
42
 
43
+ # =====================================================
44
+ # LOAD MODEL
45
+ # =====================================================
46
 
47
+ print("Loading model...")
48
 
49
  model = AutoModelForCausalLM.from_pretrained(
50
  MODEL_ID,
 
60
 
61
  model.eval()
62
 
 
63
  print("MODEL READY")
64
 
65
 
66
  # =====================================================
67
+ # GENERATION
68
  # =====================================================
69
 
70
 
 
72
  def generate(
73
  prompt,
74
  max_tokens,
75
+ temperature
76
  ):
77
 
78
  if not prompt.strip():
79
+ return "Please enter a prompt."
80
 
81
 
 
 
82
  inputs = tokenizer(
83
  prompt,
84
  return_tensors="pt",
85
  truncation=True,
86
+ max_length=4096
87
  )
88
 
89
 
90
  inputs = {
91
+ key: value.to(model.device)
92
+ for key, value in inputs.items()
93
  }
94
 
95
 
 
101
 
102
  with torch.no_grad():
103
 
104
+ outputs = model.generate(
105
 
106
  **inputs,
107
 
 
119
 
120
  top_k=50,
121
 
122
+ repetition_penalty=1.15,
123
 
124
  no_repeat_ngram_size=3,
125
 
 
133
  )
134
 
135
 
136
+ result = tokenizer.decode(
137
+ outputs[0][input_length:],
138
+ skip_special_tokens=True
139
  )
140
 
141
 
142
+ return result.strip()
143
 
144
 
145
 
146
  # =====================================================
147
+ # CHAT FUNCTION
148
  # =====================================================
149
 
150
 
 
152
  message,
153
  history,
154
  max_tokens,
155
+ temperature
156
  ):
157
 
158
+ answer = generate(
159
  message,
160
  max_tokens,
161
+ temperature
162
  )
163
 
164
+
165
  history = history or []
166
 
167
+
168
  history.append(
169
+ {
170
+ "role": "user",
171
+ "content": message
172
+ }
173
  )
174
 
175
+
176
+ history.append(
177
+ {
178
+ "role": "assistant",
179
+ "content": answer
180
+ }
181
+ )
182
+
183
+
184
  return "", history
185
 
186
 
 
191
 
192
 
193
  with gr.Blocks(
194
+ title="X-RUDRA"
195
  ) as demo:
196
 
197
 
198
  gr.Markdown(
199
  f"""
200
+ # ⚡ X-RUDRA
201
+
202
+ Model:
203
+
204
+ `{MODEL_ID}`
205
 
206
+ Device:
207
 
208
+ `{DEVICE}`
209
  """
210
  )
211
 
212
 
213
  chatbot = gr.Chatbot(
214
+ type="messages",
215
  height=600,
216
+ label="Chat"
217
  )
218
 
219
 
220
  with gr.Row():
221
 
222
  message = gr.Textbox(
223
+ placeholder="Ask anything...",
224
+ scale=8
225
  )
226
 
227
+
228
  send = gr.Button(
229
  "Send",
230
  variant="primary",
231
+ scale=1
232
  )
233
 
234
 
235
+
236
  with gr.Row():
237
 
238
  max_tokens = gr.Slider(
239
+ minimum=64,
240
+ maximum=2048,
241
  value=512,
242
  step=64,
243
+ label="Max Tokens"
244
  )
245
 
246
 
247
  temperature = gr.Slider(
248
+ minimum=0.1,
249
+ maximum=1.2,
250
  value=0.7,
251
  step=0.1,
252
+ label="Temperature"
253
  )
254
 
255
 
256
+
257
  send.click(
258
+ fn=chat,
259
  inputs=[
260
  message,
261
  chatbot,
262
  max_tokens,
263
+ temperature
264
  ],
265
  outputs=[
266
  message,
267
+ chatbot
268
+ ]
269
  )
270
 
271
 
272
  message.submit(
273
+ fn=chat,
274
  inputs=[
275
  message,
276
  chatbot,
277
  max_tokens,
278
+ temperature
279
  ],
280
  outputs=[
281
  message,
282
+ chatbot
283
+ ]
284
  )
285
 
286
 
287
 
288
  # =====================================================
289
+ # START SERVER
290
  # =====================================================
291
 
292
  if __name__ == "__main__":
 
294
  demo.launch(
295
  server_name="0.0.0.0",
296
  server_port=7860,
297
+ show_error=True
298
  )