Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,145 +2,356 @@ import gradio as gr
|
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
MODEL_ID = "ddfws/Rezaeian-StatsAI"
|
| 9 |
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
print("Loading tokenizer...")
|
| 12 |
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 14 |
-
MODEL_ID
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
|
| 18 |
print("Loading model...")
|
| 19 |
|
|
|
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
MODEL_ID,
|
| 22 |
device_map="auto",
|
| 23 |
-
|
|
|
|
| 24 |
)
|
| 25 |
|
|
|
|
| 26 |
model.eval()
|
| 27 |
|
|
|
|
| 28 |
print("MODEL READY")
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
text = text.replace("\n", "<br>")
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
@spaces.GPU
|
| 48 |
def generate(message, history):
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
prompt += f"User: {user}\nAssistant: {assistant}\n"
|
| 56 |
|
| 57 |
|
| 58 |
-
prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
inputs = tokenizer(
|
| 62 |
prompt,
|
| 63 |
-
return_tensors="pt"
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
|
|
|
|
| 67 |
inputs = {
|
| 68 |
-
|
| 69 |
-
for
|
| 70 |
}
|
| 71 |
|
| 72 |
|
|
|
|
| 73 |
with torch.no_grad():
|
| 74 |
|
| 75 |
-
|
|
|
|
| 76 |
**inputs,
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
top_p=0.9,
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
skip_special_tokens=True
|
| 87 |
)
|
| 88 |
|
| 89 |
|
| 90 |
-
answer = result.split("Assistant:")[-1]
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
return format_answer(answer)
|
| 94 |
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
demo = gr.ChatInterface(
|
|
|
|
| 98 |
fn=generate,
|
| 99 |
-
title="Rezaeian StatsAI",
|
| 100 |
-
description="دستیار هوشمند آمار و احتمال مهندسی"
|
| 101 |
-
)
|
| 102 |
|
| 103 |
|
|
|
|
| 104 |
|
| 105 |
-
demo.launch(
|
| 106 |
-
server_name="0.0.0.0",
|
| 107 |
-
css="""
|
| 108 |
|
| 109 |
-
|
| 110 |
-
direction: rtl;
|
| 111 |
-
max-width: 1200px !important;
|
| 112 |
-
}
|
| 113 |
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
text-align: right !important;
|
| 118 |
-
font-family: Tahoma, Arial, sans-serif !important;
|
| 119 |
-
font-size:18px !important;
|
| 120 |
-
line-height:2;
|
| 121 |
-
}
|
| 122 |
|
|
|
|
| 123 |
|
| 124 |
-
|
| 125 |
-
direction: rtl !important;
|
| 126 |
-
text-align:right !important;
|
| 127 |
-
font-size:18px !important;
|
| 128 |
-
}
|
| 129 |
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
direction:ltr !important;
|
| 133 |
-
text-align:center !important;
|
| 134 |
-
font-family: Cambria Math, Times New Roman, serif !important;
|
| 135 |
-
font-size:20px !important;
|
| 136 |
-
padding:12px;
|
| 137 |
-
margin:12px;
|
| 138 |
-
}
|
| 139 |
|
|
|
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
"""
|
| 146 |
)
|
|
|
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
from transformers import (
|
| 6 |
+
AutoTokenizer,
|
| 7 |
+
AutoModelForCausalLM
|
| 8 |
+
)
|
| 9 |
|
| 10 |
|
| 11 |
MODEL_ID = "ddfws/Rezaeian-StatsAI"
|
| 12 |
|
| 13 |
|
| 14 |
+
# ==============================
|
| 15 |
+
# Load Model
|
| 16 |
+
# ==============================
|
| 17 |
+
|
| 18 |
print("Loading tokenizer...")
|
| 19 |
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 21 |
+
MODEL_ID,
|
| 22 |
+
trust_remote_code=True
|
| 23 |
)
|
| 24 |
|
| 25 |
|
| 26 |
print("Loading model...")
|
| 27 |
|
| 28 |
+
|
| 29 |
model = AutoModelForCausalLM.from_pretrained(
|
| 30 |
MODEL_ID,
|
| 31 |
device_map="auto",
|
| 32 |
+
torch_dtype=torch.float16,
|
| 33 |
+
trust_remote_code=True
|
| 34 |
)
|
| 35 |
|
| 36 |
+
|
| 37 |
model.eval()
|
| 38 |
|
| 39 |
+
|
| 40 |
print("MODEL READY")
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
+
# ==============================
|
| 45 |
+
# System Prompt
|
| 46 |
+
# ==============================
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
SYSTEM_PROMPT = """
|
| 50 |
+
|
| 51 |
+
تو یک استاد تمام دانشگاه تهران در رشته آمار و احتمال مهندسی هستی.
|
| 52 |
+
|
| 53 |
+
قوانین پاسخ:
|
| 54 |
+
|
| 55 |
+
1- پاسخها کاملاً رسمی و دانشگاهی باشند.
|
| 56 |
+
|
| 57 |
+
2- برای دانشجوی کارشناسی مهندسی توضیح بده.
|
| 58 |
+
|
| 59 |
+
3- در حل مسائل همیشه این ساختار را رعایت کن:
|
| 60 |
+
|
| 61 |
+
روش حل:
|
| 62 |
+
(نام توزیع، قضیه، فرمول یا روش آماری)
|
| 63 |
+
|
| 64 |
+
تعریف و روابط:
|
| 65 |
+
(فرمولها و توضیحات لازم)
|
| 66 |
+
|
| 67 |
+
حل مرحله به مرحله:
|
| 68 |
+
(تمام محاسبات بدون حذف)
|
| 69 |
+
|
| 70 |
+
نتیجه نهایی:
|
| 71 |
+
(پاسخ واضح)
|
| 72 |
|
| 73 |
+
4- هیچ مرحله محاسباتی را حذف نکن.
|
|
|
|
| 74 |
|
| 75 |
+
5- اگر سؤال مربوط به احتمال است:
|
| 76 |
+
- فضای نمونه
|
| 77 |
+
- رویدادها
|
| 78 |
+
- قوانین احتمال
|
| 79 |
+
- توزیع مناسب
|
| 80 |
+
را مشخص کن.
|
| 81 |
|
| 82 |
+
6- اگر سؤال مربوط به آمار مهندسی است:
|
| 83 |
+
- فرضیات
|
| 84 |
+
- آزمون آماری
|
| 85 |
+
- سطح معنی داری
|
| 86 |
+
- نتیجه گیری
|
| 87 |
+
را بیان کن.
|
| 88 |
|
| 89 |
+
7- فرمولها را با LaTeX بنویس.
|
| 90 |
|
| 91 |
+
8- از جوابهای کوتاه و ناقص خودداری کن.
|
| 92 |
+
|
| 93 |
+
"""
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
# ==============================
|
| 98 |
+
# Generate
|
| 99 |
+
# ==============================
|
| 100 |
|
| 101 |
|
| 102 |
@spaces.GPU
|
| 103 |
def generate(message, history):
|
| 104 |
|
| 105 |
+
|
| 106 |
+
messages = []
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
messages.append(
|
| 110 |
+
{
|
| 111 |
+
"role":"system",
|
| 112 |
+
"content":SYSTEM_PROMPT
|
| 113 |
+
}
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
for user_msg, bot_msg in history:
|
| 118 |
+
|
| 119 |
+
messages.append(
|
| 120 |
+
{
|
| 121 |
+
"role":"user",
|
| 122 |
+
"content":user_msg
|
| 123 |
+
}
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
messages.append(
|
| 127 |
+
{
|
| 128 |
+
"role":"assistant",
|
| 129 |
+
"content":bot_msg
|
| 130 |
+
}
|
| 131 |
+
)
|
| 132 |
|
| 133 |
|
| 134 |
+
messages.append(
|
| 135 |
+
{
|
| 136 |
+
"role":"user",
|
| 137 |
+
"content":message
|
| 138 |
+
}
|
| 139 |
+
)
|
| 140 |
|
|
|
|
| 141 |
|
| 142 |
|
| 143 |
+
prompt = tokenizer.apply_chat_template(
|
| 144 |
+
messages,
|
| 145 |
+
tokenize=False,
|
| 146 |
+
add_generation_prompt=True
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
|
| 150 |
|
| 151 |
inputs = tokenizer(
|
| 152 |
prompt,
|
| 153 |
+
return_tensors="pt",
|
| 154 |
+
truncation=True,
|
| 155 |
+
max_length=8192
|
| 156 |
)
|
| 157 |
|
| 158 |
|
| 159 |
+
|
| 160 |
inputs = {
|
| 161 |
+
key:value.to(model.device)
|
| 162 |
+
for key,value in inputs.items()
|
| 163 |
}
|
| 164 |
|
| 165 |
|
| 166 |
+
|
| 167 |
with torch.no_grad():
|
| 168 |
|
| 169 |
+
outputs = model.generate(
|
| 170 |
+
|
| 171 |
**inputs,
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
# جواب بلند برای حل تشریحی
|
| 175 |
+
max_new_tokens=2048,
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
# مناسب ریاضی
|
| 179 |
+
temperature=0.25,
|
| 180 |
+
|
| 181 |
+
|
| 182 |
top_p=0.9,
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
do_sample=True,
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
repetition_penalty=1.15,
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
eos_token_id=tokenizer.eos_token_id
|
| 192 |
+
|
| 193 |
)
|
| 194 |
|
| 195 |
|
| 196 |
+
|
| 197 |
+
answer_tokens = outputs[
|
| 198 |
+
0
|
| 199 |
+
][
|
| 200 |
+
inputs["input_ids"].shape[-1]:
|
| 201 |
+
]
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
answer = tokenizer.decode(
|
| 206 |
+
answer_tokens,
|
| 207 |
skip_special_tokens=True
|
| 208 |
)
|
| 209 |
|
| 210 |
|
|
|
|
| 211 |
|
| 212 |
+
return answer
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
# ==============================
|
| 219 |
+
# Interface
|
| 220 |
+
# ==============================
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
custom_css = """
|
| 224 |
+
|
| 225 |
+
.gradio-container {
|
| 226 |
+
|
| 227 |
+
direction: rtl;
|
| 228 |
+
|
| 229 |
+
font-family:
|
| 230 |
+
Tahoma,
|
| 231 |
+
Arial;
|
| 232 |
+
|
| 233 |
+
}
|
| 234 |
|
|
|
|
| 235 |
|
| 236 |
|
| 237 |
+
.message {
|
| 238 |
+
|
| 239 |
+
direction: rtl !important;
|
| 240 |
+
|
| 241 |
+
text-align:right !important;
|
| 242 |
+
|
| 243 |
+
font-size:18px !important;
|
| 244 |
+
|
| 245 |
+
line-height:2;
|
| 246 |
+
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
textarea {
|
| 252 |
+
|
| 253 |
+
direction:rtl !important;
|
| 254 |
+
|
| 255 |
+
text-align:right !important;
|
| 256 |
+
|
| 257 |
+
font-size:18px !important;
|
| 258 |
+
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
code {
|
| 264 |
+
|
| 265 |
+
direction:ltr;
|
| 266 |
+
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
footer {
|
| 272 |
+
|
| 273 |
+
display:none !important;
|
| 274 |
+
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
"""
|
| 279 |
+
|
| 280 |
|
| 281 |
demo = gr.ChatInterface(
|
| 282 |
+
|
| 283 |
fn=generate,
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
|
| 286 |
+
title="Rezaeian StatsAI 🤖",
|
| 287 |
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
+
description="""
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
+
دستیار هوشمند آمار و احتمال مهندسی
|
| 292 |
|
| 293 |
+
سطح:
|
| 294 |
+
کارشناسی مهندسی
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
|
| 296 |
+
قابلیتها:
|
| 297 |
|
| 298 |
+
✅ احتمال و ترکیبیات
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
|
| 300 |
+
✅ متغیرهای تصادفی
|
| 301 |
|
| 302 |
+
✅ توزیعهای گسسته و پیوسته
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
+
✅ تخمین و آزمون فرض
|
| 305 |
|
| 306 |
+
✅ رگرسیون
|
| 307 |
+
|
| 308 |
+
✅ ANOVA
|
| 309 |
+
|
| 310 |
+
✅ قابلیت اطمینان
|
| 311 |
+
|
| 312 |
+
حل کاملاً مرحله به مرحله مانند استاد دانشگاه
|
| 313 |
+
|
| 314 |
+
""",
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
chatbot=gr.Chatbot(
|
| 318 |
+
|
| 319 |
+
height=750,
|
| 320 |
+
|
| 321 |
+
rtl=True
|
| 322 |
+
|
| 323 |
+
),
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
textbox=gr.Textbox(
|
| 327 |
+
|
| 328 |
+
placeholder=
|
| 329 |
+
"سؤال آمار و احتمال مهندسی خود را وارد کنید...",
|
| 330 |
+
|
| 331 |
+
|
| 332 |
+
rtl=True,
|
| 333 |
+
|
| 334 |
+
|
| 335 |
+
lines=3
|
| 336 |
+
|
| 337 |
+
),
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
css=custom_css
|
| 341 |
+
|
| 342 |
+
)
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
# ==============================
|
| 347 |
+
# Launch
|
| 348 |
+
# ==============================
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
demo.launch(
|
| 352 |
+
|
| 353 |
+
server_name="0.0.0.0",
|
| 354 |
+
|
| 355 |
+
server_port=7860
|
| 356 |
|
|
|
|
| 357 |
)
|