Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
import torch
|
| 4 |
-
import re
|
| 5 |
|
| 6 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
|
|
@@ -30,46 +29,38 @@ print("MODEL READY")
|
|
| 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 |
-
|
| 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 |
|
|
@@ -90,9 +81,9 @@ def generate(message, history):
|
|
| 90 |
output = model.generate(
|
| 91 |
**inputs,
|
| 92 |
max_new_tokens=512,
|
| 93 |
-
temperature=0.
|
| 94 |
top_p=0.9,
|
| 95 |
-
|
| 96 |
)
|
| 97 |
|
| 98 |
|
|
@@ -102,113 +93,44 @@ def generate(message, history):
|
|
| 102 |
)
|
| 103 |
|
| 104 |
|
| 105 |
-
answer = result.split("
|
| 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 |
-
|
| 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 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
outputs=chatbot
|
| 200 |
-
)
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
-
|
| 212 |
-
server_name="0.0.0.0",
|
| 213 |
-
css=css
|
| 214 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import spaces
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
@spaces.GPU
|
| 33 |
def generate(message, history):
|
| 34 |
|
| 35 |
prompt = """
|
| 36 |
+
You are Rezaeian StatsAI.
|
| 37 |
+
You are a professional statistics assistant.
|
| 38 |
+
|
| 39 |
+
Rules:
|
| 40 |
+
- Answer in Persian when user writes Persian.
|
| 41 |
+
- Write statistical formulas clearly.
|
| 42 |
+
- Keep numbers and English letters separated correctly.
|
| 43 |
+
- Use readable mathematical notation.
|
| 44 |
"""
|
| 45 |
|
|
|
|
| 46 |
|
| 47 |
+
for user, assistant in history:
|
| 48 |
prompt += f"""
|
| 49 |
+
|
| 50 |
+
User:
|
| 51 |
{user}
|
| 52 |
|
| 53 |
+
Assistant:
|
| 54 |
{assistant}
|
| 55 |
"""
|
| 56 |
|
| 57 |
|
| 58 |
prompt += f"""
|
| 59 |
|
| 60 |
+
User:
|
| 61 |
{message}
|
| 62 |
|
| 63 |
+
Assistant:
|
| 64 |
"""
|
| 65 |
|
| 66 |
|
|
|
|
| 81 |
output = model.generate(
|
| 82 |
**inputs,
|
| 83 |
max_new_tokens=512,
|
| 84 |
+
temperature=0.7,
|
| 85 |
top_p=0.9,
|
| 86 |
+
do_sample=True
|
| 87 |
)
|
| 88 |
|
| 89 |
|
|
|
|
| 93 |
)
|
| 94 |
|
| 95 |
|
| 96 |
+
answer = result.split("Assistant:")[-1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
return answer
|
| 99 |
|
|
|
|
|
|
|
|
|
|
| 100 |
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
demo = gr.ChatInterface(
|
| 103 |
+
fn=generate,
|
| 104 |
+
title="Rezaeian StatsAI",
|
| 105 |
+
description="دستیار هوشمند آمار و احتمال مهندسی"
|
| 106 |
+
)
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
+
demo.launch(
|
| 111 |
+
server_name="0.0.0.0",
|
| 112 |
+
css="""
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
.gradio-container {
|
| 115 |
+
direction: rtl;
|
| 116 |
+
max-width: 1200px !important;
|
| 117 |
+
}
|
| 118 |
|
| 119 |
+
textarea {
|
| 120 |
+
direction: rtl !important;
|
| 121 |
+
text-align: right !important;
|
| 122 |
+
font-size:18px !important;
|
| 123 |
+
}
|
| 124 |
|
| 125 |
+
.message {
|
| 126 |
+
direction: rtl !important;
|
| 127 |
+
text-align:right !important;
|
| 128 |
+
font-size:18px !important;
|
| 129 |
+
}
|
| 130 |
|
| 131 |
+
footer {
|
| 132 |
+
display:none !important;
|
| 133 |
+
}
|
| 134 |
|
| 135 |
+
"""
|
|
|
|
|
|
|
| 136 |
)
|