Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,6 @@ import gradio as gr
|
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
import PyPDF2
|
| 6 |
-
from docx import Document
|
| 7 |
-
|
| 8 |
from transformers import (
|
| 9 |
AutoTokenizer,
|
| 10 |
AutoModelForCausalLM
|
|
@@ -12,9 +9,9 @@ from transformers import (
|
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
-
# =========================
|
| 16 |
# MODEL
|
| 17 |
-
# =========================
|
| 18 |
|
| 19 |
MODEL_ID = "ddfws/Rezaeian-StatsAI"
|
| 20 |
|
|
@@ -47,59 +44,58 @@ print("MODEL READY")
|
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
-
# =========================
|
| 51 |
-
#
|
| 52 |
-
# =========================
|
| 53 |
|
| 54 |
|
| 55 |
SYSTEM_PROMPT = """
|
| 56 |
|
| 57 |
-
تو
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
هد
|
| 60 |
-
آموزش کامل دانشجوی کارشناسی مهندسی.
|
| 61 |
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
- پاسخ کوتاه نده.
|
| 65 |
- مانند استاد دانشگاه توضیح بده.
|
| 66 |
- تمام مراحل حل را بنویس.
|
| 67 |
-
- هیچ فرمول
|
| 68 |
|
| 69 |
|
| 70 |
ساختار پاسخ:
|
| 71 |
|
| 72 |
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
| 88 |
|
| 89 |
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
|
| 93 |
-
|
| 94 |
|
| 95 |
Bayes
|
| 96 |
|
| 97 |
Random Variable
|
| 98 |
|
| 99 |
-
Discrete Distribution
|
| 100 |
-
|
| 101 |
-
Continuous Distribution
|
| 102 |
-
|
| 103 |
Binomial
|
| 104 |
|
| 105 |
Poisson
|
|
@@ -133,141 +129,44 @@ Reliability
|
|
| 133 |
|
| 134 |
فرمولها را با LaTeX بنویس.
|
| 135 |
|
| 136 |
-
متغیرهای:
|
| 137 |
-
|
| 138 |
-
X,Y,μ,σ,P,E,Var
|
| 139 |
|
| 140 |
-
|
| 141 |
|
|
|
|
| 142 |
|
| 143 |
"""
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
if file is None:
|
| 152 |
-
return ""
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
path = file.name
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
text = ""
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
try:
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
if path.lower().endswith(".pdf"):
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
pdf = PyPDF2.PdfReader(path)
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
for page in pdf.pages:
|
| 171 |
-
|
| 172 |
-
result = page.extract_text()
|
| 173 |
-
|
| 174 |
-
if result:
|
| 175 |
-
|
| 176 |
-
text += result + "\n"
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
elif path.lower().endswith(".txt"):
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
with open(
|
| 184 |
-
path,
|
| 185 |
-
"r",
|
| 186 |
-
encoding="utf-8"
|
| 187 |
-
) as f:
|
| 188 |
-
|
| 189 |
-
text = f.read()
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
elif path.lower().endswith(".docx"):
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
doc = Document(path)
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
for p in doc.paragraphs:
|
| 200 |
-
|
| 201 |
-
text += p.text + "\n"
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
except Exception as e:
|
| 206 |
-
|
| 207 |
-
text = "خطا در خواندن فایل: " + str(e)
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
# جلوگیری از پر شدن حافظه
|
| 212 |
-
|
| 213 |
-
return text[:40000]
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
|
| 218 |
|
| 219 |
@spaces.GPU
|
| 220 |
-
def generate(message, history
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
if document:
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
message = f"""
|
| 227 |
-
|
| 228 |
-
از سند زیر به عنوان منبع استفاده کن:
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
----------------------
|
| 232 |
-
|
| 233 |
-
{document}
|
| 234 |
-
|
| 235 |
-
----------------------
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
سوال دانشجو:
|
| 239 |
-
|
| 240 |
-
{message}
|
| 241 |
-
|
| 242 |
-
"""
|
| 243 |
-
|
| 244 |
|
| 245 |
|
| 246 |
messages = [
|
| 247 |
|
| 248 |
{
|
| 249 |
-
|
| 250 |
"role":"system",
|
| 251 |
-
|
| 252 |
"content":SYSTEM_PROMPT
|
| 253 |
-
|
| 254 |
}
|
| 255 |
|
| 256 |
]
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
-
for user, assistant in history[-
|
| 261 |
|
| 262 |
|
| 263 |
messages.append(
|
| 264 |
|
| 265 |
{
|
| 266 |
-
|
| 267 |
"role":"user",
|
| 268 |
-
|
| 269 |
"content":user
|
| 270 |
-
|
| 271 |
}
|
| 272 |
|
| 273 |
)
|
|
@@ -276,11 +175,8 @@ def generate(message, history, document):
|
|
| 276 |
messages.append(
|
| 277 |
|
| 278 |
{
|
| 279 |
-
|
| 280 |
"role":"assistant",
|
| 281 |
-
|
| 282 |
"content":assistant
|
| 283 |
-
|
| 284 |
}
|
| 285 |
|
| 286 |
)
|
|
@@ -290,11 +186,8 @@ def generate(message, history, document):
|
|
| 290 |
messages.append(
|
| 291 |
|
| 292 |
{
|
| 293 |
-
|
| 294 |
"role":"user",
|
| 295 |
-
|
| 296 |
"content":message
|
| 297 |
-
|
| 298 |
}
|
| 299 |
|
| 300 |
)
|
|
@@ -337,7 +230,6 @@ def generate(message, history, document):
|
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
-
|
| 341 |
with torch.no_grad():
|
| 342 |
|
| 343 |
|
|
@@ -345,25 +237,18 @@ def generate(message, history, document):
|
|
| 345 |
|
| 346 |
**inputs,
|
| 347 |
|
| 348 |
-
|
| 349 |
max_new_tokens=4096,
|
| 350 |
|
| 351 |
-
|
| 352 |
-
min_new_tokens=500,
|
| 353 |
-
|
| 354 |
|
| 355 |
temperature=0.25,
|
| 356 |
|
| 357 |
-
|
| 358 |
top_p=0.9,
|
| 359 |
|
| 360 |
-
|
| 361 |
do_sample=True,
|
| 362 |
|
| 363 |
-
|
| 364 |
repetition_penalty=1.1,
|
| 365 |
|
| 366 |
-
|
| 367 |
pad_token_id=tokenizer.eos_token_id
|
| 368 |
|
| 369 |
)
|
|
@@ -388,17 +273,19 @@ def generate(message, history, document):
|
|
| 388 |
|
| 389 |
|
| 390 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 391 |
|
| 392 |
-
def chat(message, history
|
| 393 |
|
| 394 |
|
| 395 |
answer = generate(
|
| 396 |
|
| 397 |
message,
|
| 398 |
|
| 399 |
-
history
|
| 400 |
-
|
| 401 |
-
document
|
| 402 |
|
| 403 |
)
|
| 404 |
|
|
@@ -418,6 +305,11 @@ def chat(message, history, document):
|
|
| 418 |
|
| 419 |
|
| 420 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
css = """
|
| 422 |
|
| 423 |
.gradio-container{
|
|
@@ -429,10 +321,9 @@ max-width:1400px!important;
|
|
| 429 |
}
|
| 430 |
|
| 431 |
|
| 432 |
-
|
| 433 |
#chatbox{
|
| 434 |
|
| 435 |
-
height:
|
| 436 |
|
| 437 |
overflow-y:auto!important;
|
| 438 |
|
|
@@ -478,6 +369,9 @@ display:none!important;
|
|
| 478 |
|
| 479 |
|
| 480 |
|
|
|
|
|
|
|
|
|
|
| 481 |
|
| 482 |
|
| 483 |
with gr.Blocks(css=css) as demo:
|
|
@@ -485,77 +379,36 @@ with gr.Blocks(css=css) as demo:
|
|
| 485 |
|
| 486 |
gr.Markdown(
|
| 487 |
"""
|
| 488 |
-
|
| 489 |
# 🤖 Rezaeian StatsAI
|
| 490 |
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
ویژه دانشجویان کارشناسی مهندسی
|
| 494 |
-
|
| 495 |
-
قابلیتها:
|
| 496 |
|
| 497 |
-
|
| 498 |
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
|
| 505 |
"""
|
| 506 |
)
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
with gr.Row():
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
with gr.Column(scale=1):
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
file = gr.File(
|
| 521 |
-
|
| 522 |
-
label="📚 آپلود سند",
|
| 523 |
-
|
| 524 |
-
file_types=[
|
| 525 |
-
|
| 526 |
-
".pdf",
|
| 527 |
-
|
| 528 |
-
".txt",
|
| 529 |
-
|
| 530 |
-
".docx"
|
| 531 |
-
|
| 532 |
-
]
|
| 533 |
|
| 534 |
-
|
| 535 |
|
|
|
|
| 536 |
|
| 537 |
-
|
| 538 |
-
"🗑 پاک کردن گفتگو"
|
| 539 |
-
)
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
with gr.Column(scale=3):
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
chatbot = gr.Chatbot(
|
| 547 |
-
|
| 548 |
-
height=850,
|
| 549 |
-
|
| 550 |
-
elem_id="chatbox"
|
| 551 |
-
|
| 552 |
-
)
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
textbox = gr.Textbox(
|
| 557 |
|
| 558 |
-
placeholder="س
|
| 559 |
|
| 560 |
lines=4
|
| 561 |
|
|
@@ -563,14 +416,8 @@ with gr.Blocks(css=css) as demo:
|
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
read_document,
|
| 569 |
-
|
| 570 |
-
inputs=file,
|
| 571 |
-
|
| 572 |
-
outputs=document_state
|
| 573 |
-
|
| 574 |
)
|
| 575 |
|
| 576 |
|
|
@@ -583,9 +430,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 583 |
|
| 584 |
textbox,
|
| 585 |
|
| 586 |
-
chatbot
|
| 587 |
-
|
| 588 |
-
document_state
|
| 589 |
|
| 590 |
],
|
| 591 |
|
|
@@ -613,6 +458,9 @@ with gr.Blocks(css=css) as demo:
|
|
| 613 |
|
| 614 |
|
| 615 |
|
|
|
|
|
|
|
|
|
|
| 616 |
|
| 617 |
|
| 618 |
demo.launch(
|
|
|
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
from transformers import (
|
| 6 |
AutoTokenizer,
|
| 7 |
AutoModelForCausalLM
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
+
# =========================
|
| 13 |
# MODEL
|
| 14 |
+
# =========================
|
| 15 |
|
| 16 |
MODEL_ID = "ddfws/Rezaeian-StatsAI"
|
| 17 |
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
+
# =========================
|
| 48 |
+
# PROMPT
|
| 49 |
+
# =========================
|
| 50 |
|
| 51 |
|
| 52 |
SYSTEM_PROMPT = """
|
| 53 |
|
| 54 |
+
تو Rezaeian-StatsAI هستی.
|
| 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 |
+
7- نکات امتحانی
|
| 85 |
|
| 86 |
+
8- نتیجه نهایی
|
| 87 |
|
| 88 |
|
| 89 |
+
موضوعات:
|
| 90 |
|
| 91 |
+
احتمال
|
| 92 |
|
| 93 |
+
قوانین احتمال
|
| 94 |
|
| 95 |
Bayes
|
| 96 |
|
| 97 |
Random Variable
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
Binomial
|
| 100 |
|
| 101 |
Poisson
|
|
|
|
| 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):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
|
| 151 |
messages = [
|
| 152 |
|
| 153 |
{
|
|
|
|
| 154 |
"role":"system",
|
|
|
|
| 155 |
"content":SYSTEM_PROMPT
|
|
|
|
| 156 |
}
|
| 157 |
|
| 158 |
]
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
+
for user, assistant in history[-5:]:
|
| 163 |
|
| 164 |
|
| 165 |
messages.append(
|
| 166 |
|
| 167 |
{
|
|
|
|
| 168 |
"role":"user",
|
|
|
|
| 169 |
"content":user
|
|
|
|
| 170 |
}
|
| 171 |
|
| 172 |
)
|
|
|
|
| 175 |
messages.append(
|
| 176 |
|
| 177 |
{
|
|
|
|
| 178 |
"role":"assistant",
|
|
|
|
| 179 |
"content":assistant
|
|
|
|
| 180 |
}
|
| 181 |
|
| 182 |
)
|
|
|
|
| 186 |
messages.append(
|
| 187 |
|
| 188 |
{
|
|
|
|
| 189 |
"role":"user",
|
|
|
|
| 190 |
"content":message
|
|
|
|
| 191 |
}
|
| 192 |
|
| 193 |
)
|
|
|
|
| 230 |
|
| 231 |
|
| 232 |
|
|
|
|
| 233 |
with torch.no_grad():
|
| 234 |
|
| 235 |
|
|
|
|
| 237 |
|
| 238 |
**inputs,
|
| 239 |
|
|
|
|
| 240 |
max_new_tokens=4096,
|
| 241 |
|
| 242 |
+
min_new_tokens=700,
|
|
|
|
|
|
|
| 243 |
|
| 244 |
temperature=0.25,
|
| 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 |
)
|
|
|
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
+
# =========================
|
| 277 |
+
# CHAT
|
| 278 |
+
# =========================
|
| 279 |
+
|
| 280 |
|
| 281 |
+
def chat(message, history):
|
| 282 |
|
| 283 |
|
| 284 |
answer = generate(
|
| 285 |
|
| 286 |
message,
|
| 287 |
|
| 288 |
+
history
|
|
|
|
|
|
|
| 289 |
|
| 290 |
)
|
| 291 |
|
|
|
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
+
# =========================
|
| 309 |
+
# STYLE
|
| 310 |
+
# =========================
|
| 311 |
+
|
| 312 |
+
|
| 313 |
css = """
|
| 314 |
|
| 315 |
.gradio-container{
|
|
|
|
| 321 |
}
|
| 322 |
|
| 323 |
|
|
|
|
| 324 |
#chatbox{
|
| 325 |
|
| 326 |
+
height:900px!important;
|
| 327 |
|
| 328 |
overflow-y:auto!important;
|
| 329 |
|
|
|
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
+
# =========================
|
| 373 |
+
# UI
|
| 374 |
+
# =========================
|
| 375 |
|
| 376 |
|
| 377 |
with gr.Blocks(css=css) as demo:
|
|
|
|
| 379 |
|
| 380 |
gr.Markdown(
|
| 381 |
"""
|
|
|
|
| 382 |
# 🤖 Rezaeian StatsAI
|
| 383 |
|
| 384 |
+
دستیار هوشمند آمار و احتمال مهندسی
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
|
| 386 |
+
حل مسائل به سبک استاد دانشگاه تهران
|
| 387 |
|
| 388 |
+
✔ توضیح کامل
|
| 389 |
+
✔ فرمول
|
| 390 |
+
✔ حل مرحلهای
|
| 391 |
+
✔ مثال مشابه
|
| 392 |
+
✔ نکات امتحانی
|
| 393 |
|
| 394 |
"""
|
| 395 |
)
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
+
chatbot = gr.Chatbot(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 400 |
|
| 401 |
+
height=900,
|
| 402 |
|
| 403 |
+
elem_id="chatbox"
|
| 404 |
|
| 405 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
textbox = gr.Textbox(
|
| 410 |
|
| 411 |
+
placeholder="سؤال آمار و احتمال خود را وارد کنید",
|
| 412 |
|
| 413 |
lines=4
|
| 414 |
|
|
|
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
+
clear = gr.Button(
|
| 420 |
+
"🗑 پاک کردن گفتگو"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
)
|
| 422 |
|
| 423 |
|
|
|
|
| 430 |
|
| 431 |
textbox,
|
| 432 |
|
| 433 |
+
chatbot
|
|
|
|
|
|
|
| 434 |
|
| 435 |
],
|
| 436 |
|
|
|
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
+
# =========================
|
| 462 |
+
# RUN
|
| 463 |
+
# =========================
|
| 464 |
|
| 465 |
|
| 466 |
demo.launch(
|