Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,20 +2,14 @@ import gradio as gr
|
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
from transformers import
|
| 6 |
-
AutoTokenizer,
|
| 7 |
-
AutoModelForCausalLM
|
| 8 |
-
)
|
| 9 |
-
|
| 10 |
|
| 11 |
|
| 12 |
-
# =========================
|
| 13 |
-
# MODEL
|
| 14 |
-
# =========================
|
| 15 |
|
| 16 |
MODEL_ID = "ddfws/Rezaeian-StatsAI"
|
| 17 |
|
| 18 |
|
|
|
|
| 19 |
print("Loading tokenizer...")
|
| 20 |
|
| 21 |
|
|
@@ -30,13 +24,19 @@ print("Loading model...")
|
|
| 30 |
|
| 31 |
|
| 32 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 33 |
MODEL_ID,
|
|
|
|
| 34 |
device_map="auto",
|
|
|
|
| 35 |
dtype=torch.float16,
|
|
|
|
| 36 |
trust_remote_code=True
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
|
|
|
|
| 40 |
model.eval()
|
| 41 |
|
| 42 |
|
|
@@ -44,105 +44,60 @@ print("MODEL READY")
|
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
-
# =========================
|
| 48 |
-
# PROMPT
|
| 49 |
-
# =========================
|
| 50 |
-
|
| 51 |
|
| 52 |
SYSTEM_PROMPT = """
|
| 53 |
|
| 54 |
-
تو
|
| 55 |
-
|
| 56 |
-
یک استاد تمام دانشگاه تهران در درس آمار و احتمال مهندسی.
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
قوانین پاسخ:
|
| 62 |
|
| 63 |
- پاسخ کوتاه نده.
|
| 64 |
-
- مانند
|
| 65 |
-
-
|
| 66 |
-
-
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
-
ساختار پاسخ:
|
| 70 |
|
|
|
|
| 71 |
|
| 72 |
1- تشخیص موضوع
|
| 73 |
|
| 74 |
2- توضیح مفهوم
|
| 75 |
|
| 76 |
-
3- رو
|
| 77 |
-
|
| 78 |
-
4- فرمولهای لازم
|
| 79 |
-
|
| 80 |
-
5- حل مرحله به مرحله
|
| 81 |
-
|
| 82 |
-
6- مثال مشابه مهندسی
|
| 83 |
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
|
|
|
|
| 88 |
|
| 89 |
-
موضوعات:
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
قوانین احتمال
|
| 94 |
|
|
|
|
| 95 |
Bayes
|
| 96 |
-
|
| 97 |
Random Variable
|
| 98 |
-
|
| 99 |
Binomial
|
| 100 |
-
|
| 101 |
Poisson
|
| 102 |
-
|
| 103 |
Normal
|
| 104 |
-
|
| 105 |
Exponential
|
| 106 |
-
|
| 107 |
-
Gamma
|
| 108 |
-
|
| 109 |
Expectation
|
| 110 |
-
|
| 111 |
Variance
|
| 112 |
-
|
| 113 |
-
Covariance
|
| 114 |
-
|
| 115 |
-
Sampling
|
| 116 |
-
|
| 117 |
-
CLT
|
| 118 |
-
|
| 119 |
Confidence Interval
|
| 120 |
-
|
| 121 |
Hypothesis Testing
|
| 122 |
-
|
| 123 |
Regression
|
| 124 |
-
|
| 125 |
ANOVA
|
| 126 |
-
|
| 127 |
Reliability
|
| 128 |
|
| 129 |
|
| 130 |
-
فرمولها را با LaTeX بنویس.
|
| 131 |
-
|
| 132 |
-
متغیرهای ریاضی:
|
| 133 |
-
|
| 134 |
-
X Y μ σ P E Var
|
| 135 |
-
|
| 136 |
-
انگلیسی بمانند.
|
| 137 |
-
|
| 138 |
"""
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
-
# =========================
|
| 143 |
-
# GENERATE
|
| 144 |
-
# =========================
|
| 145 |
-
|
| 146 |
|
| 147 |
@spaces.GPU
|
| 148 |
def generate(message, history):
|
|
@@ -159,37 +114,30 @@ def generate(message, history):
|
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
-
for user, assistant in history[-
|
| 163 |
-
|
| 164 |
|
| 165 |
messages.append(
|
| 166 |
-
|
| 167 |
{
|
| 168 |
"role":"user",
|
| 169 |
"content":user
|
| 170 |
}
|
| 171 |
-
|
| 172 |
)
|
| 173 |
|
| 174 |
|
| 175 |
messages.append(
|
| 176 |
-
|
| 177 |
{
|
| 178 |
"role":"assistant",
|
| 179 |
"content":assistant
|
| 180 |
}
|
| 181 |
-
|
| 182 |
)
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
messages.append(
|
| 187 |
-
|
| 188 |
{
|
| 189 |
"role":"user",
|
| 190 |
"content":message
|
| 191 |
}
|
| 192 |
-
|
| 193 |
)
|
| 194 |
|
| 195 |
|
|
@@ -214,7 +162,7 @@ def generate(message, history):
|
|
| 214 |
|
| 215 |
truncation=True,
|
| 216 |
|
| 217 |
-
max_length=
|
| 218 |
|
| 219 |
)
|
| 220 |
|
|
@@ -232,24 +180,19 @@ def generate(message, history):
|
|
| 232 |
|
| 233 |
with torch.no_grad():
|
| 234 |
|
| 235 |
-
|
| 236 |
output = model.generate(
|
| 237 |
|
| 238 |
**inputs,
|
| 239 |
|
| 240 |
-
max_new_tokens=
|
| 241 |
-
|
| 242 |
-
min_new_tokens=700,
|
| 243 |
|
| 244 |
-
temperature=0.
|
| 245 |
|
| 246 |
top_p=0.9,
|
| 247 |
|
| 248 |
do_sample=True,
|
| 249 |
|
| 250 |
-
repetition_penalty=1.1
|
| 251 |
-
|
| 252 |
-
pad_token_id=tokenizer.eos_token_id
|
| 253 |
|
| 254 |
)
|
| 255 |
|
|
@@ -257,11 +200,7 @@ def generate(message, history):
|
|
| 257 |
|
| 258 |
answer = tokenizer.decode(
|
| 259 |
|
| 260 |
-
output[0][
|
| 261 |
-
|
| 262 |
-
inputs["input_ids"].shape[-1]:
|
| 263 |
-
|
| 264 |
-
],
|
| 265 |
|
| 266 |
skip_special_tokens=True
|
| 267 |
|
|
@@ -273,10 +212,6 @@ def generate(message, history):
|
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
-
# =========================
|
| 277 |
-
# CHAT
|
| 278 |
-
# =========================
|
| 279 |
-
|
| 280 |
|
| 281 |
def chat(message, history):
|
| 282 |
|
|
@@ -305,10 +240,6 @@ def chat(message, history):
|
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
-
# =========================
|
| 309 |
-
# STYLE
|
| 310 |
-
# =========================
|
| 311 |
-
|
| 312 |
|
| 313 |
css = """
|
| 314 |
|
|
@@ -321,15 +252,6 @@ max-width:1400px!important;
|
|
| 321 |
}
|
| 322 |
|
| 323 |
|
| 324 |
-
#chatbox{
|
| 325 |
-
|
| 326 |
-
height:900px!important;
|
| 327 |
-
|
| 328 |
-
overflow-y:auto!important;
|
| 329 |
-
|
| 330 |
-
}
|
| 331 |
-
|
| 332 |
-
|
| 333 |
|
| 334 |
.message{
|
| 335 |
|
|
@@ -339,39 +261,26 @@ text-align:right!important;
|
|
| 339 |
|
| 340 |
font-size:18px!important;
|
| 341 |
|
| 342 |
-
line-height:2
|
| 343 |
|
| 344 |
white-space:pre-wrap!important;
|
| 345 |
|
| 346 |
}
|
| 347 |
|
| 348 |
|
| 349 |
-
|
| 350 |
textarea{
|
| 351 |
|
| 352 |
direction:rtl!important;
|
| 353 |
|
| 354 |
text-align:right!important;
|
| 355 |
|
| 356 |
-
font-size:18px!important;
|
| 357 |
-
|
| 358 |
}
|
| 359 |
|
| 360 |
|
| 361 |
-
|
| 362 |
-
footer{
|
| 363 |
-
|
| 364 |
-
display:none!important;
|
| 365 |
-
|
| 366 |
-
}
|
| 367 |
-
|
| 368 |
"""
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
-
# =========================
|
| 373 |
-
# UI
|
| 374 |
-
# =========================
|
| 375 |
|
| 376 |
|
| 377 |
with gr.Blocks(css=css) as demo:
|
|
@@ -381,16 +290,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 381 |
"""
|
| 382 |
# 🤖 Rezaeian StatsAI
|
| 383 |
|
| 384 |
-
دستیار
|
| 385 |
-
|
| 386 |
-
حل مسائل به ��بک استاد دانشگاه تهران
|
| 387 |
-
|
| 388 |
-
✔ توضیح کامل
|
| 389 |
-
✔ فرمول
|
| 390 |
-
✔ حل مرحلهای
|
| 391 |
-
✔ مثال مشابه
|
| 392 |
-
✔ نکات امتحانی
|
| 393 |
|
|
|
|
| 394 |
"""
|
| 395 |
)
|
| 396 |
|
|
@@ -398,9 +300,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 398 |
|
| 399 |
chatbot = gr.Chatbot(
|
| 400 |
|
| 401 |
-
height=900
|
| 402 |
-
|
| 403 |
-
elem_id="chatbox"
|
| 404 |
|
| 405 |
)
|
| 406 |
|
|
@@ -408,16 +308,10 @@ with gr.Blocks(css=css) as demo:
|
|
| 408 |
|
| 409 |
textbox = gr.Textbox(
|
| 410 |
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
lines=4
|
| 414 |
-
|
| 415 |
-
)
|
| 416 |
-
|
| 417 |
|
|
|
|
| 418 |
|
| 419 |
-
clear = gr.Button(
|
| 420 |
-
"🗑 پاک کردن گفتگو"
|
| 421 |
)
|
| 422 |
|
| 423 |
|
|
@@ -446,22 +340,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
-
clear.click(
|
| 450 |
-
|
| 451 |
-
lambda: [],
|
| 452 |
-
|
| 453 |
-
None,
|
| 454 |
-
|
| 455 |
-
chatbot
|
| 456 |
-
|
| 457 |
-
)
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
# =========================
|
| 462 |
-
# RUN
|
| 463 |
-
# =========================
|
| 464 |
-
|
| 465 |
|
| 466 |
demo.launch(
|
| 467 |
|
|
|
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
MODEL_ID = "ddfws/Rezaeian-StatsAI"
|
| 10 |
|
| 11 |
|
| 12 |
+
|
| 13 |
print("Loading tokenizer...")
|
| 14 |
|
| 15 |
|
|
|
|
| 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 |
+
|
| 36 |
)
|
| 37 |
|
| 38 |
|
| 39 |
+
|
| 40 |
model.eval()
|
| 41 |
|
| 42 |
|
|
|
|
| 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):
|
|
|
|
| 114 |
|
| 115 |
|
| 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 |
|
| 163 |
truncation=True,
|
| 164 |
|
| 165 |
+
max_length=4096
|
| 166 |
|
| 167 |
)
|
| 168 |
|
|
|
|
| 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 |
|
| 201 |
answer = tokenizer.decode(
|
| 202 |
|
| 203 |
+
output[0][inputs["input_ids"].shape[-1]:],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
skip_special_tokens=True
|
| 206 |
|
|
|
|
| 212 |
|
| 213 |
|
| 214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
def chat(message, history):
|
| 217 |
|
|
|
|
| 240 |
|
| 241 |
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
css = """
|
| 245 |
|
|
|
|
| 252 |
}
|
| 253 |
|
| 254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
.message{
|
| 257 |
|
|
|
|
| 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 |
"""
|
| 291 |
# 🤖 Rezaeian StatsAI
|
| 292 |
|
| 293 |
+
دستیار آمار و احتمال مهندسی
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
| 295 |
+
حل تشریحی به سبک استاد دانشگاه
|
| 296 |
"""
|
| 297 |
)
|
| 298 |
|
|
|
|
| 300 |
|
| 301 |
chatbot = gr.Chatbot(
|
| 302 |
|
| 303 |
+
height=900
|
|
|
|
|
|
|
| 304 |
|
| 305 |
)
|
| 306 |
|
|
|
|
| 308 |
|
| 309 |
textbox = gr.Textbox(
|
| 310 |
|
| 311 |
+
lines=3,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
+
placeholder="سوال خود را وارد کنید..."
|
| 314 |
|
|
|
|
|
|
|
| 315 |
)
|
| 316 |
|
| 317 |
|
|
|
|
| 340 |
|
| 341 |
|
| 342 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
demo.launch(
|
| 345 |
|