Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,24 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import spaces
|
| 3 |
import torch
|
| 4 |
-
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
|
| 7 |
|
| 8 |
-
|
| 9 |
|
| 10 |
|
| 11 |
print("Loading tokenizer...")
|
| 12 |
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 14 |
-
|
| 15 |
)
|
| 16 |
|
| 17 |
|
| 18 |
print("Loading model...")
|
| 19 |
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
-
|
| 22 |
device_map="auto",
|
| 23 |
-
dtype=torch.float16
|
| 24 |
)
|
| 25 |
|
| 26 |
model.eval()
|
|
@@ -28,58 +26,220 @@ model.eval()
|
|
| 28 |
print("MODEL READY")
|
| 29 |
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
def generate(message, history):
|
| 33 |
|
| 34 |
-
|
| 35 |
|
| 36 |
-
for user,
|
| 37 |
-
prompt += f"User: {user}\nAssistant: {assistant}\n"
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
inputs = tokenizer(
|
| 44 |
prompt,
|
| 45 |
return_tensors="pt"
|
| 46 |
-
)
|
| 47 |
|
| 48 |
|
| 49 |
-
inputs = {
|
| 50 |
-
k: v.to(model.device)
|
| 51 |
-
for k, v in inputs.items()
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
|
| 55 |
with torch.no_grad():
|
| 56 |
|
| 57 |
output = model.generate(
|
| 58 |
**inputs,
|
| 59 |
-
max_new_tokens=
|
| 60 |
-
temperature=0.
|
| 61 |
top_p=0.9,
|
| 62 |
-
do_sample=True
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
skip_special_tokens=True
|
| 69 |
)
|
| 70 |
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
|
| 76 |
demo = gr.ChatInterface(
|
|
|
|
| 77 |
fn=generate,
|
|
|
|
| 78 |
title="Rezaeian StatsAI",
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
)
|
| 81 |
|
| 82 |
|
|
|
|
| 83 |
demo.launch(
|
| 84 |
-
server_name="0.0.0.0"
|
|
|
|
|
|
|
| 85 |
)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
|
| 6 |
+
MODEL_PATH = "ddfws/Rezaeian-StatsAI"
|
| 7 |
|
| 8 |
|
| 9 |
print("Loading tokenizer...")
|
| 10 |
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 12 |
+
MODEL_PATH
|
| 13 |
)
|
| 14 |
|
| 15 |
|
| 16 |
print("Loading model...")
|
| 17 |
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
+
MODEL_PATH,
|
| 20 |
device_map="auto",
|
| 21 |
+
dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 22 |
)
|
| 23 |
|
| 24 |
model.eval()
|
|
|
|
| 26 |
print("MODEL READY")
|
| 27 |
|
| 28 |
|
| 29 |
+
def format_text(text):
|
| 30 |
+
|
| 31 |
+
# فرمول های رایج آماری
|
| 32 |
+
replacements = {
|
| 33 |
+
"β0": r"$\beta_0$",
|
| 34 |
+
"β1": r"$\beta_1$",
|
| 35 |
+
"σ2": r"$\sigma^2$",
|
| 36 |
+
"R2": r"$R^2$",
|
| 37 |
+
"xbar": r"$\bar{x}$",
|
| 38 |
+
"mu": r"$\mu$",
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
for a,b in replacements.items():
|
| 42 |
+
text = text.replace(a,b)
|
| 43 |
+
|
| 44 |
+
return text
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
def generate(message, history):
|
| 49 |
|
| 50 |
+
messages=[]
|
| 51 |
|
| 52 |
+
for user, bot in history:
|
|
|
|
| 53 |
|
| 54 |
+
messages.append(
|
| 55 |
+
{
|
| 56 |
+
"role":"user",
|
| 57 |
+
"content":user
|
| 58 |
+
}
|
| 59 |
+
)
|
| 60 |
|
| 61 |
+
messages.append(
|
| 62 |
+
{
|
| 63 |
+
"role":"assistant",
|
| 64 |
+
"content":bot
|
| 65 |
+
}
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
messages.append(
|
| 70 |
+
{
|
| 71 |
+
"role":"user",
|
| 72 |
+
"content":message
|
| 73 |
+
}
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
prompt = tokenizer.apply_chat_template(
|
| 78 |
+
messages,
|
| 79 |
+
tokenize=False,
|
| 80 |
+
add_generation_prompt=True
|
| 81 |
+
)
|
| 82 |
|
| 83 |
|
| 84 |
inputs = tokenizer(
|
| 85 |
prompt,
|
| 86 |
return_tensors="pt"
|
| 87 |
+
).to(model.device)
|
| 88 |
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
with torch.no_grad():
|
| 92 |
|
| 93 |
output = model.generate(
|
| 94 |
**inputs,
|
| 95 |
+
max_new_tokens=700,
|
| 96 |
+
temperature=0.6,
|
| 97 |
top_p=0.9,
|
| 98 |
+
do_sample=True,
|
| 99 |
+
repetition_penalty=1.1
|
| 100 |
)
|
| 101 |
|
| 102 |
|
| 103 |
+
|
| 104 |
+
answer = tokenizer.decode(
|
| 105 |
+
output[0][inputs.input_ids.shape[1]:],
|
| 106 |
skip_special_tokens=True
|
| 107 |
)
|
| 108 |
|
| 109 |
|
| 110 |
+
answer = format_text(answer)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
return answer
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
css = """
|
| 119 |
+
|
| 120 |
+
/* حذف موارد اضافی */
|
| 121 |
+
|
| 122 |
+
footer {
|
| 123 |
+
display:none !important;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
.gradio-container {
|
| 128 |
+
|
| 129 |
+
max-width:1100px !important;
|
| 130 |
+
direction:rtl;
|
| 131 |
+
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
/* پیام ها */
|
| 136 |
+
|
| 137 |
+
.message {
|
| 138 |
+
|
| 139 |
+
direction:rtl !important;
|
| 140 |
+
text-align:right !important;
|
| 141 |
+
unicode-bidi:plaintext;
|
| 142 |
+
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
|
| 146 |
+
/* متن فارسی */
|
| 147 |
+
|
| 148 |
+
.markdown {
|
| 149 |
+
|
| 150 |
+
direction:rtl;
|
| 151 |
+
text-align:right;
|
| 152 |
+
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
/* ورودی */
|
| 157 |
+
|
| 158 |
+
textarea {
|
| 159 |
+
|
| 160 |
+
direction:rtl !important;
|
| 161 |
+
text-align:right !important;
|
| 162 |
+
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
/* فرمول ها */
|
| 167 |
+
|
| 168 |
+
.katex {
|
| 169 |
+
|
| 170 |
+
direction:ltr !important;
|
| 171 |
+
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
.MathJax {
|
| 176 |
+
|
| 177 |
+
direction:ltr !important;
|
| 178 |
+
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
/* کد و انگلیسی */
|
| 183 |
+
|
| 184 |
+
code, pre {
|
| 185 |
+
|
| 186 |
+
direction:ltr !important;
|
| 187 |
+
text-align:left !important;
|
| 188 |
+
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
"""
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
head = """
|
| 195 |
+
|
| 196 |
+
<script>
|
| 197 |
+
window.MathJax = {
|
| 198 |
+
tex: {
|
| 199 |
+
inlineMath: [['$', '$']],
|
| 200 |
+
displayMath: [['$$','$$']]
|
| 201 |
+
}
|
| 202 |
+
};
|
| 203 |
+
</script>
|
| 204 |
+
|
| 205 |
+
<script
|
| 206 |
+
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
|
| 207 |
+
</script>
|
| 208 |
+
|
| 209 |
+
"""
|
| 210 |
|
| 211 |
|
| 212 |
demo = gr.ChatInterface(
|
| 213 |
+
|
| 214 |
fn=generate,
|
| 215 |
+
|
| 216 |
title="Rezaeian StatsAI",
|
| 217 |
+
|
| 218 |
+
description=
|
| 219 |
+
"دستیار هوش مصنوعی آمار و احتمال مهندسی",
|
| 220 |
+
|
| 221 |
+
css=css,
|
| 222 |
+
|
| 223 |
+
head=head,
|
| 224 |
+
|
| 225 |
+
chatbot=gr.Chatbot(
|
| 226 |
+
height=650,
|
| 227 |
+
show_copy_button=True
|
| 228 |
+
),
|
| 229 |
+
|
| 230 |
+
examples=[
|
| 231 |
+
"فرمول میانگین نمونه را بنویس",
|
| 232 |
+
"رگرسیون خطی را با فرمول توضیح بده",
|
| 233 |
+
"آزمون t چیست؟",
|
| 234 |
+
"انحراف معیار را محاسبه کن"
|
| 235 |
+
]
|
| 236 |
+
|
| 237 |
)
|
| 238 |
|
| 239 |
|
| 240 |
+
|
| 241 |
demo.launch(
|
| 242 |
+
server_name="0.0.0.0",
|
| 243 |
+
server_port=7860,
|
| 244 |
+
show_api=False
|
| 245 |
)
|