Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
|
|
@@ -29,17 +30,47 @@ print("MODEL READY")
|
|
| 29 |
|
| 30 |
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
@spaces.GPU
|
| 33 |
def generate(message, history):
|
| 34 |
|
| 35 |
-
prompt = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
for user, assistant in history:
|
| 38 |
-
prompt += f"User: {user}\n"
|
| 39 |
-
prompt += f"Assistant: {assistant}\n"
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
|
| 45 |
inputs = tokenizer(
|
|
@@ -49,8 +80,8 @@ def generate(message, history):
|
|
| 49 |
|
| 50 |
|
| 51 |
inputs = {
|
| 52 |
-
k:
|
| 53 |
-
for k,
|
| 54 |
}
|
| 55 |
|
| 56 |
|
|
@@ -59,109 +90,120 @@ def generate(message, history):
|
|
| 59 |
output = model.generate(
|
| 60 |
**inputs,
|
| 61 |
max_new_tokens=512,
|
| 62 |
-
temperature=0.
|
| 63 |
top_p=0.9,
|
| 64 |
-
do_sample=True,
|
| 65 |
repetition_penalty=1.1
|
| 66 |
)
|
| 67 |
|
| 68 |
|
| 69 |
-
|
| 70 |
output[0],
|
| 71 |
skip_special_tokens=True
|
| 72 |
)
|
| 73 |
|
| 74 |
|
| 75 |
-
answer =
|
| 76 |
-
|
| 77 |
|
| 78 |
-
return
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
css = """
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
margin: auto !important;
|
| 88 |
|
|
|
|
|
|
|
| 89 |
}
|
| 90 |
|
| 91 |
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
direction: rtl !important;
|
| 95 |
-
text-align: right !important;
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
}
|
| 98 |
|
| 99 |
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
direction: rtl !important;
|
| 103 |
-
text-align: right !important;
|
| 104 |
|
|
|
|
|
|
|
| 105 |
}
|
| 106 |
|
| 107 |
-
|
| 108 |
"""
|
| 109 |
|
| 110 |
|
| 111 |
with gr.Blocks() as demo:
|
| 112 |
|
| 113 |
|
| 114 |
-
gr.
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
chatbot = gr.Chatbot(
|
| 124 |
-
height=
|
| 125 |
)
|
| 126 |
|
| 127 |
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
textbox = gr.Textbox(
|
| 131 |
-
placeholder="سوال خود را بنویسید...",
|
| 132 |
-
scale=8
|
| 133 |
-
)
|
| 134 |
|
|
|
|
| 135 |
|
| 136 |
send = gr.Button(
|
| 137 |
"ارسال"
|
| 138 |
)
|
| 139 |
|
| 140 |
-
|
| 141 |
clear = gr.Button(
|
| 142 |
"پاک کردن"
|
| 143 |
)
|
| 144 |
|
| 145 |
|
| 146 |
-
|
| 147 |
send.click(
|
| 148 |
generate,
|
| 149 |
inputs=[textbox, chatbot],
|
| 150 |
-
outputs=
|
| 151 |
)
|
| 152 |
|
| 153 |
|
| 154 |
textbox.submit(
|
| 155 |
generate,
|
| 156 |
inputs=[textbox, chatbot],
|
| 157 |
-
outputs=
|
| 158 |
)
|
| 159 |
|
| 160 |
|
| 161 |
clear.click(
|
| 162 |
-
lambda:
|
| 163 |
-
|
| 164 |
-
|
| 165 |
)
|
| 166 |
|
| 167 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
+
import re
|
| 5 |
|
| 6 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
+
def clean_text(text):
|
| 34 |
+
|
| 35 |
+
# مرتب کردن فاصله های فرمول
|
| 36 |
+
text = text.replace(" ,", ",")
|
| 37 |
+
text = text.replace(" .", ".")
|
| 38 |
+
|
| 39 |
+
# نمایش بهتر فرمول ها
|
| 40 |
+
text = text.replace("$", "")
|
| 41 |
+
|
| 42 |
+
return text
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
@spaces.GPU
|
| 47 |
def generate(message, history):
|
| 48 |
|
| 49 |
+
prompt = """
|
| 50 |
+
شما Rezaeian StatsAI هستید.
|
| 51 |
+
دستیار تخصصی آمار هستید.
|
| 52 |
+
فرمول ها را دقیق و خوانا بنویسید.
|
| 53 |
+
برای فارسی راست چین پاسخ دهید.
|
| 54 |
+
"""
|
| 55 |
|
| 56 |
for user, assistant in history:
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
prompt += f"""
|
| 59 |
+
کاربر:
|
| 60 |
+
{user}
|
| 61 |
|
| 62 |
+
پاسخ:
|
| 63 |
+
{assistant}
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
prompt += f"""
|
| 68 |
+
|
| 69 |
+
کاربر:
|
| 70 |
+
{message}
|
| 71 |
+
|
| 72 |
+
پاسخ:
|
| 73 |
+
"""
|
| 74 |
|
| 75 |
|
| 76 |
inputs = tokenizer(
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
inputs = {
|
| 83 |
+
k:v.to(model.device)
|
| 84 |
+
for k,v in inputs.items()
|
| 85 |
}
|
| 86 |
|
| 87 |
|
|
|
|
| 90 |
output = model.generate(
|
| 91 |
**inputs,
|
| 92 |
max_new_tokens=512,
|
| 93 |
+
temperature=0.6,
|
| 94 |
top_p=0.9,
|
|
|
|
| 95 |
repetition_penalty=1.1
|
| 96 |
)
|
| 97 |
|
| 98 |
|
| 99 |
+
result = tokenizer.decode(
|
| 100 |
output[0],
|
| 101 |
skip_special_tokens=True
|
| 102 |
)
|
| 103 |
|
| 104 |
|
| 105 |
+
answer = result.split("پاسخ:")[-1]
|
|
|
|
| 106 |
|
| 107 |
+
return clean_text(answer)
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
css = """
|
| 112 |
|
| 113 |
+
body{
|
| 114 |
+
direction:rtl;
|
| 115 |
+
}
|
|
|
|
| 116 |
|
| 117 |
+
.gradio-container{
|
| 118 |
+
max-width:1200px !important;
|
| 119 |
}
|
| 120 |
|
| 121 |
|
| 122 |
+
.chatbot{
|
| 123 |
+
font-size:18px !important;
|
| 124 |
+
height:700px !important;
|
| 125 |
+
}
|
| 126 |
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
.message{
|
| 129 |
+
direction:rtl !important;
|
| 130 |
+
text-align:right !important;
|
| 131 |
+
font-family:Tahoma, Arial !important;
|
| 132 |
}
|
| 133 |
|
| 134 |
|
| 135 |
+
textarea{
|
| 136 |
+
direction:rtl !important;
|
| 137 |
+
font-size:18px !important;
|
| 138 |
+
}
|
| 139 |
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
footer{
|
| 142 |
+
display:none !important;
|
| 143 |
}
|
| 144 |
|
|
|
|
| 145 |
"""
|
| 146 |
|
| 147 |
|
| 148 |
with gr.Blocks() as demo:
|
| 149 |
|
| 150 |
|
| 151 |
+
gr.HTML("""
|
| 152 |
+
<h1 style="
|
| 153 |
+
text-align:center;
|
| 154 |
+
direction:rtl;">
|
| 155 |
+
🤖 Rezaeian StatsAI
|
| 156 |
+
</h1>
|
| 157 |
+
|
| 158 |
+
<p style="
|
| 159 |
+
text-align:center;
|
| 160 |
+
direction:rtl;
|
| 161 |
+
font-size:18px;">
|
| 162 |
+
دستیار هوشمند آمار و فرمول های آماری
|
| 163 |
+
</p>
|
| 164 |
+
""")
|
| 165 |
|
| 166 |
|
| 167 |
chatbot = gr.Chatbot(
|
| 168 |
+
height=700
|
| 169 |
)
|
| 170 |
|
| 171 |
|
| 172 |
+
textbox = gr.Textbox(
|
| 173 |
+
placeholder="سوال آماری خود را بنویسید...",
|
| 174 |
+
lines=2
|
| 175 |
+
)
|
| 176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
+
with gr.Row():
|
| 179 |
|
| 180 |
send = gr.Button(
|
| 181 |
"ارسال"
|
| 182 |
)
|
| 183 |
|
|
|
|
| 184 |
clear = gr.Button(
|
| 185 |
"پاک کردن"
|
| 186 |
)
|
| 187 |
|
| 188 |
|
|
|
|
| 189 |
send.click(
|
| 190 |
generate,
|
| 191 |
inputs=[textbox, chatbot],
|
| 192 |
+
outputs=chatbot
|
| 193 |
)
|
| 194 |
|
| 195 |
|
| 196 |
textbox.submit(
|
| 197 |
generate,
|
| 198 |
inputs=[textbox, chatbot],
|
| 199 |
+
outputs=chatbot
|
| 200 |
)
|
| 201 |
|
| 202 |
|
| 203 |
clear.click(
|
| 204 |
+
lambda: None,
|
| 205 |
+
None,
|
| 206 |
+
chatbot
|
| 207 |
)
|
| 208 |
|
| 209 |
|