Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,31 +28,19 @@ model.eval()
|
|
| 28 |
print("MODEL READY")
|
| 29 |
|
| 30 |
|
| 31 |
-
|
| 32 |
-
def format_answer(text):
|
| 33 |
-
|
| 34 |
-
# تبدیل چند فرمول رایج آماری به LaTeX
|
| 35 |
-
text = text.replace("β0", "$\\beta_0$")
|
| 36 |
-
text = text.replace("β1", "$\\beta_1$")
|
| 37 |
-
text = text.replace("R2", "$R^2$")
|
| 38 |
-
text = text.replace("σ2", "$\\sigma^2$")
|
| 39 |
-
text = text.replace("x̄", "$\\bar{x}$")
|
| 40 |
-
|
| 41 |
-
return text
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
@spaces.GPU
|
| 46 |
def generate(message, history):
|
| 47 |
|
| 48 |
prompt = ""
|
| 49 |
|
| 50 |
-
for
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
|
| 58 |
prompt += f"User: {message}\nAssistant:"
|
|
@@ -65,24 +53,20 @@ def generate(message, history):
|
|
| 65 |
|
| 66 |
|
| 67 |
inputs = {
|
| 68 |
-
k:v.to(model.device)
|
| 69 |
-
for k,v in inputs.items()
|
| 70 |
}
|
| 71 |
|
| 72 |
|
| 73 |
with torch.no_grad():
|
| 74 |
|
| 75 |
output = model.generate(
|
| 76 |
-
|
| 77 |
**inputs,
|
| 78 |
-
|
| 79 |
max_new_tokens=512,
|
| 80 |
-
|
| 81 |
temperature=0.7,
|
| 82 |
-
|
| 83 |
top_p=0.9,
|
| 84 |
-
|
| 85 |
-
|
| 86 |
)
|
| 87 |
|
| 88 |
|
|
@@ -94,62 +78,91 @@ def generate(message, history):
|
|
| 94 |
|
| 95 |
answer = result.split("Assistant:")[-1]
|
| 96 |
|
| 97 |
-
|
| 98 |
-
return format_answer(answer)
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
css = """
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
display:none !important;
|
| 106 |
}
|
| 107 |
|
| 108 |
-
|
| 109 |
.gradio-container {
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
|
|
|
|
|
|
|
|
|
|
| 113 |
}
|
| 114 |
|
|
|
|
| 115 |
|
| 116 |
-
.message {
|
| 117 |
|
| 118 |
-
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
unicode-bidi:plaintext;
|
| 123 |
|
| 124 |
-
|
|
|
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
direction:rtl !important;
|
| 130 |
|
| 131 |
-
|
| 132 |
|
| 133 |
-
}
|
| 134 |
|
|
|
|
| 135 |
|
| 136 |
-
|
| 137 |
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
|
| 143 |
|
| 144 |
-
"""
|
| 145 |
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
|
| 155 |
|
|
|
|
| 28 |
print("MODEL READY")
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
@spaces.GPU
|
| 32 |
def generate(message, history):
|
| 33 |
|
| 34 |
prompt = ""
|
| 35 |
|
| 36 |
+
for item in history:
|
| 37 |
+
if isinstance(item, dict):
|
| 38 |
+
if item["role"] == "user":
|
| 39 |
+
prompt += f"User: {item['content']}\n"
|
| 40 |
+
else:
|
| 41 |
+
prompt += f"Assistant: {item['content']}\n"
|
| 42 |
+
else:
|
| 43 |
+
prompt += f"User: {item[0]}\nAssistant: {item[1]}\n"
|
| 44 |
|
| 45 |
|
| 46 |
prompt += f"User: {message}\nAssistant:"
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
inputs = {
|
| 56 |
+
k: v.to(model.device)
|
| 57 |
+
for k, v in inputs.items()
|
| 58 |
}
|
| 59 |
|
| 60 |
|
| 61 |
with torch.no_grad():
|
| 62 |
|
| 63 |
output = model.generate(
|
|
|
|
| 64 |
**inputs,
|
|
|
|
| 65 |
max_new_tokens=512,
|
|
|
|
| 66 |
temperature=0.7,
|
|
|
|
| 67 |
top_p=0.9,
|
| 68 |
+
do_sample=True,
|
| 69 |
+
repetition_penalty=1.1
|
| 70 |
)
|
| 71 |
|
| 72 |
|
|
|
|
| 78 |
|
| 79 |
answer = result.split("Assistant:")[-1]
|
| 80 |
|
| 81 |
+
return answer.strip()
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
css = """
|
| 86 |
+
body {
|
| 87 |
+
direction: rtl;
|
|
|
|
| 88 |
}
|
| 89 |
|
|
|
|
| 90 |
.gradio-container {
|
| 91 |
+
direction: rtl;
|
| 92 |
+
text-align: right;
|
| 93 |
+
}
|
| 94 |
|
| 95 |
+
textarea {
|
| 96 |
+
direction: rtl !important;
|
| 97 |
+
text-align: right !important;
|
| 98 |
+
}
|
| 99 |
|
| 100 |
+
.message {
|
| 101 |
+
direction: rtl;
|
| 102 |
+
text-align: right;
|
| 103 |
}
|
| 104 |
|
| 105 |
+
"""
|
| 106 |
|
|
|
|
| 107 |
|
| 108 |
+
with gr.Blocks(css=css) as demo:
|
| 109 |
|
| 110 |
+
gr.Markdown(
|
| 111 |
+
"""
|
| 112 |
+
# 🤖 Rezaeian StatsAI
|
| 113 |
+
### دستیار هوش مصنوعی آمار و تحلیل داده
|
| 114 |
+
"""
|
| 115 |
+
)
|
| 116 |
|
|
|
|
| 117 |
|
| 118 |
+
chatbot = gr.Chatbot(
|
| 119 |
+
height=600
|
| 120 |
+
)
|
| 121 |
|
| 122 |
|
| 123 |
+
msg = gr.Textbox(
|
| 124 |
+
placeholder="سوال خود را بنویسید...",
|
| 125 |
+
lines=2
|
| 126 |
+
)
|
| 127 |
|
|
|
|
| 128 |
|
| 129 |
+
clear = gr.Button("پاک کردن")
|
| 130 |
|
|
|
|
| 131 |
|
| 132 |
+
def respond(message, history):
|
| 133 |
|
| 134 |
+
answer = generate(message, history)
|
| 135 |
|
| 136 |
+
history.append(
|
| 137 |
+
{
|
| 138 |
+
"role":"user",
|
| 139 |
+
"content":message
|
| 140 |
+
}
|
| 141 |
+
)
|
| 142 |
|
| 143 |
+
history.append(
|
| 144 |
+
{
|
| 145 |
+
"role":"assistant",
|
| 146 |
+
"content":answer
|
| 147 |
+
}
|
| 148 |
+
)
|
| 149 |
|
| 150 |
+
return "", history
|
| 151 |
|
|
|
|
| 152 |
|
| 153 |
|
| 154 |
+
msg.submit(
|
| 155 |
+
respond,
|
| 156 |
+
[msg, chatbot],
|
| 157 |
+
[msg, chatbot]
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
clear.click(
|
| 162 |
+
lambda: [],
|
| 163 |
+
None,
|
| 164 |
+
chatbot
|
| 165 |
+
)
|
| 166 |
|
| 167 |
|
| 168 |
|