Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,24 +28,15 @@ model.eval()
|
|
| 28 |
print("MODEL READY")
|
| 29 |
|
| 30 |
|
|
|
|
| 31 |
@spaces.GPU
|
| 32 |
def generate(message, history):
|
| 33 |
|
| 34 |
prompt = ""
|
| 35 |
|
| 36 |
-
for
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
if item["role"] == "user":
|
| 41 |
-
prompt += f"User: {item['content']}\n"
|
| 42 |
-
|
| 43 |
-
else:
|
| 44 |
-
prompt += f"Assistant: {item['content']}\n"
|
| 45 |
-
|
| 46 |
-
else:
|
| 47 |
-
prompt += f"User: {item[0]}\n"
|
| 48 |
-
prompt += f"Assistant: {item[1]}\n"
|
| 49 |
|
| 50 |
|
| 51 |
prompt += f"User: {message}\nAssistant:"
|
|
@@ -75,13 +66,13 @@ def generate(message, history):
|
|
| 75 |
)
|
| 76 |
|
| 77 |
|
| 78 |
-
|
| 79 |
output[0],
|
| 80 |
skip_special_tokens=True
|
| 81 |
)
|
| 82 |
|
| 83 |
|
| 84 |
-
answer =
|
| 85 |
|
| 86 |
|
| 87 |
return answer.strip()
|
|
@@ -90,11 +81,6 @@ def generate(message, history):
|
|
| 90 |
|
| 91 |
css = """
|
| 92 |
|
| 93 |
-
body {
|
| 94 |
-
direction: rtl;
|
| 95 |
-
}
|
| 96 |
-
|
| 97 |
-
|
| 98 |
.gradio-container {
|
| 99 |
|
| 100 |
max-width: 900px !important;
|
|
@@ -122,7 +108,7 @@ textarea {
|
|
| 122 |
"""
|
| 123 |
|
| 124 |
|
| 125 |
-
with gr.Blocks(
|
| 126 |
|
| 127 |
|
| 128 |
gr.Markdown(
|
|
@@ -135,70 +121,38 @@ with gr.Blocks(css=css) as demo:
|
|
| 135 |
|
| 136 |
|
| 137 |
chatbot = gr.Chatbot(
|
| 138 |
-
height=450
|
| 139 |
-
type="messages"
|
| 140 |
)
|
| 141 |
|
| 142 |
|
| 143 |
with gr.Row():
|
| 144 |
|
| 145 |
-
|
| 146 |
textbox = gr.Textbox(
|
| 147 |
placeholder="سوال خود را بنویسید...",
|
| 148 |
-
scale=8
|
| 149 |
-
container=False
|
| 150 |
)
|
| 151 |
|
| 152 |
|
| 153 |
send = gr.Button(
|
| 154 |
-
"ارسال"
|
| 155 |
-
scale=1
|
| 156 |
)
|
| 157 |
|
| 158 |
|
| 159 |
clear = gr.Button(
|
| 160 |
-
"پاک کردن"
|
| 161 |
-
scale=1
|
| 162 |
)
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
-
def chat(message, history):
|
| 167 |
-
|
| 168 |
-
answer = generate(
|
| 169 |
-
message,
|
| 170 |
-
history
|
| 171 |
-
)
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
history = history + [
|
| 175 |
-
|
| 176 |
-
{
|
| 177 |
-
"role": "user",
|
| 178 |
-
"content": message
|
| 179 |
-
},
|
| 180 |
-
|
| 181 |
-
{
|
| 182 |
-
"role": "assistant",
|
| 183 |
-
"content": answer
|
| 184 |
-
}
|
| 185 |
-
|
| 186 |
-
]
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
return "", history
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
send.click(
|
| 194 |
-
|
| 195 |
inputs=[textbox, chatbot],
|
| 196 |
outputs=[textbox, chatbot]
|
| 197 |
)
|
| 198 |
|
| 199 |
|
| 200 |
textbox.submit(
|
| 201 |
-
|
| 202 |
inputs=[textbox, chatbot],
|
| 203 |
outputs=[textbox, chatbot]
|
| 204 |
)
|
|
@@ -213,5 +167,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 213 |
|
| 214 |
|
| 215 |
demo.launch(
|
| 216 |
-
server_name="0.0.0.0"
|
|
|
|
| 217 |
)
|
|
|
|
| 28 |
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 |
prompt += f"User: {message}\nAssistant:"
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
|
| 69 |
+
text = tokenizer.decode(
|
| 70 |
output[0],
|
| 71 |
skip_special_tokens=True
|
| 72 |
)
|
| 73 |
|
| 74 |
|
| 75 |
+
answer = text.split("Assistant:")[-1]
|
| 76 |
|
| 77 |
|
| 78 |
return answer.strip()
|
|
|
|
| 81 |
|
| 82 |
css = """
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
.gradio-container {
|
| 85 |
|
| 86 |
max-width: 900px !important;
|
|
|
|
| 108 |
"""
|
| 109 |
|
| 110 |
|
| 111 |
+
with gr.Blocks() as demo:
|
| 112 |
|
| 113 |
|
| 114 |
gr.Markdown(
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
chatbot = gr.Chatbot(
|
| 124 |
+
height=450
|
|
|
|
| 125 |
)
|
| 126 |
|
| 127 |
|
| 128 |
with gr.Row():
|
| 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=[textbox, chatbot]
|
| 151 |
)
|
| 152 |
|
| 153 |
|
| 154 |
textbox.submit(
|
| 155 |
+
generate,
|
| 156 |
inputs=[textbox, chatbot],
|
| 157 |
outputs=[textbox, chatbot]
|
| 158 |
)
|
|
|
|
| 167 |
|
| 168 |
|
| 169 |
demo.launch(
|
| 170 |
+
server_name="0.0.0.0",
|
| 171 |
+
css=css
|
| 172 |
)
|