ddfws commited on
Commit
3d61864
·
verified ·
1 Parent(s): 6dfe986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +208 -33
app.py CHANGED
@@ -2,9 +2,18 @@ import gradio as gr
2
  import spaces
3
  import torch
4
 
5
- from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
 
 
 
6
 
7
 
 
 
 
 
8
 
9
  MODEL_ID = "ddfws/Rezaeian-StatsAI"
10
 
@@ -14,8 +23,11 @@ print("Loading tokenizer...")
14
 
15
 
16
  tokenizer = AutoTokenizer.from_pretrained(
 
17
  MODEL_ID,
 
18
  trust_remote_code=True
 
19
  )
20
 
21
 
@@ -23,13 +35,28 @@ tokenizer = AutoTokenizer.from_pretrained(
23
  print("Loading model...")
24
 
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  model = AutoModelForCausalLM.from_pretrained(
27
 
28
  MODEL_ID,
29
 
30
  device_map="auto",
31
 
32
- dtype=torch.float16,
33
 
34
  trust_remote_code=True
35
 
@@ -40,64 +67,116 @@ model = AutoModelForCausalLM.from_pretrained(
40
  model.eval()
41
 
42
 
 
43
  print("MODEL READY")
44
 
45
 
46
 
47
 
 
 
 
 
 
48
  SYSTEM_PROMPT = """
49
 
50
- تو یک استاد دانشگاه تهران در درس آمار و احتمال مهندسی هستی.
 
 
 
 
 
 
 
51
 
52
- برای دانشجوی کارشناسی مهندسی پاسخ بده.
53
 
54
- قوانین:
55
 
56
- - پاسخ کوتاه نده.
57
- - مانند جزوه دانشگاهی توضیح بده.
58
- - مراحل حل را کامل بنویس.
59
- - فرمول‌ها را با LaTeX بنویس.
60
- - مثال مشابه ارائه کن.
61
- - نکات امتحانی را بگو.
62
 
63
 
64
- ساختار:
65
 
66
- 1- تشخیص موضوع
67
 
68
- 2- توضیح مفهوم
69
 
70
- 3- فرمول‌ها
71
 
72
- 4- حل مرحله به مرحله
73
 
74
- 5- مثال مشابه
 
 
 
 
 
 
 
 
75
 
76
- 6- نتیجه نهایی
77
 
78
 
79
  مباحث:
80
 
81
  Probability
82
- Bayes
83
- Random Variable
 
 
 
84
  Binomial
 
85
  Poisson
 
86
  Normal
 
87
  Exponential
 
 
 
88
  Expectation
 
89
  Variance
 
 
 
 
 
 
 
90
  Confidence Interval
 
91
  Hypothesis Testing
 
92
  Regression
 
93
  ANOVA
 
94
  Reliability
95
 
96
 
 
 
 
 
 
 
 
 
 
 
97
  """
98
 
99
 
100
 
 
 
 
 
101
 
102
  @spaces.GPU
103
  def generate(message, history):
@@ -106,8 +185,11 @@ def generate(message, history):
106
  messages = [
107
 
108
  {
 
109
  "role":"system",
 
110
  "content":SYSTEM_PROMPT
 
111
  }
112
 
113
  ]
@@ -116,28 +198,44 @@ def generate(message, history):
116
 
117
  for user, assistant in history[-4:]:
118
 
 
119
  messages.append(
 
120
  {
 
121
  "role":"user",
 
122
  "content":user
 
123
  }
 
124
  )
125
 
126
 
127
  messages.append(
 
128
  {
 
129
  "role":"assistant",
 
130
  "content":assistant
 
131
  }
 
132
  )
133
 
134
 
135
 
136
  messages.append(
 
137
  {
 
138
  "role":"user",
 
139
  "content":message
 
140
  }
 
141
  )
142
 
143
 
@@ -162,7 +260,7 @@ def generate(message, history):
162
 
163
  truncation=True,
164
 
165
- max_length=4096
166
 
167
  )
168
 
@@ -180,19 +278,31 @@ def generate(message, history):
180
 
181
  with torch.no_grad():
182
 
 
183
  output = model.generate(
184
 
185
  **inputs,
186
 
187
- max_new_tokens=2048,
188
 
189
- temperature=0.3,
 
 
 
 
 
 
 
190
 
191
  top_p=0.9,
192
 
 
193
  do_sample=True,
194
 
195
- repetition_penalty=1.1
 
 
 
 
196
 
197
  )
198
 
@@ -200,7 +310,11 @@ def generate(message, history):
200
 
201
  answer = tokenizer.decode(
202
 
203
- output[0][inputs["input_ids"].shape[-1]:],
 
 
 
 
204
 
205
  skip_special_tokens=True
206
 
@@ -212,6 +326,10 @@ def generate(message, history):
212
 
213
 
214
 
 
 
 
 
215
 
216
  def chat(message, history):
217
 
@@ -228,8 +346,11 @@ def chat(message, history):
228
  history.append(
229
 
230
  [
 
231
  message,
 
232
  answer
 
233
  ]
234
 
235
  )
@@ -240,10 +361,14 @@ def chat(message, history):
240
 
241
 
242
 
 
 
 
 
243
 
244
  css = """
245
 
246
- .gradio-container{
247
 
248
  direction:rtl;
249
 
@@ -253,34 +378,49 @@ max-width:1400px!important;
253
 
254
 
255
 
256
- .message{
257
 
258
  direction:rtl!important;
259
 
260
  text-align:right!important;
261
 
 
 
262
  font-size:18px!important;
263
 
264
- line-height:2!important;
265
 
266
  white-space:pre-wrap!important;
267
 
268
  }
269
 
270
 
271
- textarea{
 
272
 
273
  direction:rtl!important;
274
 
275
  text-align:right!important;
276
 
 
 
277
  }
278
 
279
 
 
 
 
 
 
 
 
280
  """
281
 
282
 
283
 
 
 
 
284
 
285
 
286
  with gr.Blocks(css=css) as demo:
@@ -290,9 +430,20 @@ with gr.Blocks(css=css) as demo:
290
  """
291
  # 🤖 Rezaeian StatsAI
292
 
293
- دستیار آمار و احتمال مهندسی
 
 
 
 
 
 
 
 
 
 
 
 
294
 
295
- حل تشریحی به سبک استاد دانشگاه
296
  """
297
  )
298
 
@@ -308,14 +459,20 @@ with gr.Blocks(css=css) as demo:
308
 
309
  textbox = gr.Textbox(
310
 
311
- lines=3,
312
 
313
- placeholder="سوال خود را وارد کنید..."
314
 
315
  )
316
 
317
 
318
 
 
 
 
 
 
 
319
  textbox.submit(
320
 
321
  chat,
@@ -340,6 +497,24 @@ with gr.Blocks(css=css) as demo:
340
 
341
 
342
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
343
 
344
  demo.launch(
345
 
 
2
  import spaces
3
  import torch
4
 
5
+ from transformers import (
6
+ AutoTokenizer,
7
+ AutoModelForCausalLM,
8
+ BitsAndBytesConfig
9
+ )
10
+
11
 
12
 
13
+ # ==========================
14
+ # MODEL
15
+ # ==========================
16
+
17
 
18
  MODEL_ID = "ddfws/Rezaeian-StatsAI"
19
 
 
23
 
24
 
25
  tokenizer = AutoTokenizer.from_pretrained(
26
+
27
  MODEL_ID,
28
+
29
  trust_remote_code=True
30
+
31
  )
32
 
33
 
 
35
  print("Loading model...")
36
 
37
 
38
+
39
+ bnb_config = BitsAndBytesConfig(
40
+
41
+ load_in_4bit=True,
42
+
43
+ bnb_4bit_quant_type="nf4",
44
+
45
+ bnb_4bit_compute_dtype=torch.float16,
46
+
47
+ bnb_4bit_use_double_quant=True
48
+
49
+ )
50
+
51
+
52
+
53
  model = AutoModelForCausalLM.from_pretrained(
54
 
55
  MODEL_ID,
56
 
57
  device_map="auto",
58
 
59
+ quantization_config=bnb_config,
60
 
61
  trust_remote_code=True
62
 
 
67
  model.eval()
68
 
69
 
70
+
71
  print("MODEL READY")
72
 
73
 
74
 
75
 
76
+ # ==========================
77
+ # SYSTEM PROMPT
78
+ # ==========================
79
+
80
+
81
  SYSTEM_PROMPT = """
82
 
83
+ تو Rezaeian StatsAI هستی.
84
+
85
+ یک استاد تمام دانشگاه تهران در درس آمار و احتمال مهندسی.
86
+
87
+
88
+ هدف:
89
+
90
+ آموزش عمیق دانشجوی کارشناسی مهندسی.
91
 
 
92
 
93
+ قوانین پاسخ:
94
 
95
+ - پاسخ کوتاه ممنوع.
96
+ - مانند استاد دانشگاه توضیح بده.
97
+ - تمام مراحل حل را بنویس.
98
+ - فقط جواب نهایی نده.
99
+ - مثال مهندسی ارائه کن.
 
100
 
101
 
102
+ ساختار پاسخ:
103
 
 
104
 
105
+ 1) تشخیص موضوع
106
 
107
+ 2) توضیح مفهوم
108
 
109
+ 3) روش حل
110
 
111
+ 4) فرمول‌های لازم
112
+
113
+ 5) حل مرحله به مرحله
114
+
115
+ 6) مثال مشابه
116
+
117
+ 7) نکات امتحانی
118
+
119
+ 8) نتیجه نهایی
120
 
 
121
 
122
 
123
  مباحث:
124
 
125
  Probability
126
+
127
+ Bayes theorem
128
+
129
+ Random Variables
130
+
131
  Binomial
132
+
133
  Poisson
134
+
135
  Normal
136
+
137
  Exponential
138
+
139
+ Gamma
140
+
141
  Expectation
142
+
143
  Variance
144
+
145
+ Covariance
146
+
147
+ Sampling
148
+
149
+ CLT
150
+
151
  Confidence Interval
152
+
153
  Hypothesis Testing
154
+
155
  Regression
156
+
157
  ANOVA
158
+
159
  Reliability
160
 
161
 
162
+
163
+ فرمول‌ها را با LaTeX بنویس.
164
+
165
+ متغیرهای ریاضی:
166
+
167
+ X,Y,μ,σ,P,E,Var
168
+
169
+ انگلیسی بمانند.
170
+
171
+
172
  """
173
 
174
 
175
 
176
+ # ==========================
177
+ # GENERATE
178
+ # ==========================
179
+
180
 
181
  @spaces.GPU
182
  def generate(message, history):
 
185
  messages = [
186
 
187
  {
188
+
189
  "role":"system",
190
+
191
  "content":SYSTEM_PROMPT
192
+
193
  }
194
 
195
  ]
 
198
 
199
  for user, assistant in history[-4:]:
200
 
201
+
202
  messages.append(
203
+
204
  {
205
+
206
  "role":"user",
207
+
208
  "content":user
209
+
210
  }
211
+
212
  )
213
 
214
 
215
  messages.append(
216
+
217
  {
218
+
219
  "role":"assistant",
220
+
221
  "content":assistant
222
+
223
  }
224
+
225
  )
226
 
227
 
228
 
229
  messages.append(
230
+
231
  {
232
+
233
  "role":"user",
234
+
235
  "content":message
236
+
237
  }
238
+
239
  )
240
 
241
 
 
260
 
261
  truncation=True,
262
 
263
+ max_length=8192
264
 
265
  )
266
 
 
278
 
279
  with torch.no_grad():
280
 
281
+
282
  output = model.generate(
283
 
284
  **inputs,
285
 
 
286
 
287
+ max_new_tokens=4096,
288
+
289
+
290
+ min_new_tokens=700,
291
+
292
+
293
+ temperature=0.25,
294
+
295
 
296
  top_p=0.9,
297
 
298
+
299
  do_sample=True,
300
 
301
+
302
+ repetition_penalty=1.1,
303
+
304
+
305
+ pad_token_id=tokenizer.eos_token_id
306
 
307
  )
308
 
 
310
 
311
  answer = tokenizer.decode(
312
 
313
+ output[0][
314
+
315
+ inputs["input_ids"].shape[-1]:
316
+
317
+ ],
318
 
319
  skip_special_tokens=True
320
 
 
326
 
327
 
328
 
329
+ # ==========================
330
+ # CHAT
331
+ # ==========================
332
+
333
 
334
  def chat(message, history):
335
 
 
346
  history.append(
347
 
348
  [
349
+
350
  message,
351
+
352
  answer
353
+
354
  ]
355
 
356
  )
 
361
 
362
 
363
 
364
+ # ==========================
365
+ # CSS
366
+ # ==========================
367
+
368
 
369
  css = """
370
 
371
+ .gradio-container {
372
 
373
  direction:rtl;
374
 
 
378
 
379
 
380
 
381
+ .message {
382
 
383
  direction:rtl!important;
384
 
385
  text-align:right!important;
386
 
387
+ font-family:Tahoma,Arial;
388
+
389
  font-size:18px!important;
390
 
391
+ line-height:2.2!important;
392
 
393
  white-space:pre-wrap!important;
394
 
395
  }
396
 
397
 
398
+
399
+ textarea {
400
 
401
  direction:rtl!important;
402
 
403
  text-align:right!important;
404
 
405
+ font-size:18px!important;
406
+
407
  }
408
 
409
 
410
+
411
+ footer {
412
+
413
+ display:none!important;
414
+
415
+ }
416
+
417
  """
418
 
419
 
420
 
421
+ # ==========================
422
+ # UI
423
+ # ==========================
424
 
425
 
426
  with gr.Blocks(css=css) as demo:
 
430
  """
431
  # 🤖 Rezaeian StatsAI
432
 
433
+ دستیار هوشمند آمار و احتمال مهندسی
434
+
435
+ حل تشریحی به سبک استاد دانشگاه تهران
436
+
437
+ ✓ توضیح مفهوم
438
+
439
+ ✓ فرمول‌ها
440
+
441
+ ✓ حل مرحله‌ای
442
+
443
+ ✓ مثال مشابه
444
+
445
+ ✓ نکات امتحانی
446
 
 
447
  """
448
  )
449
 
 
459
 
460
  textbox = gr.Textbox(
461
 
462
+ placeholder="سؤال آمار و احتمال خود را وارد کنید",
463
 
464
+ lines=4
465
 
466
  )
467
 
468
 
469
 
470
+ clear = gr.Button(
471
+ "پاک کردن گفتگو"
472
+ )
473
+
474
+
475
+
476
  textbox.submit(
477
 
478
  chat,
 
497
 
498
 
499
 
500
+ clear.click(
501
+
502
+ lambda: [],
503
+
504
+ None,
505
+
506
+ chatbot
507
+
508
+ )
509
+
510
+
511
+
512
+
513
+
514
+ # ==========================
515
+ # RUN
516
+ # ==========================
517
+
518
 
519
  demo.launch(
520