Update app.py
Browse files
app.py
CHANGED
|
@@ -73,13 +73,10 @@ def on_user_message(user_message: str, state: Dict):
|
|
| 73 |
if not user_message.strip():
|
| 74 |
return [], state, gr.update(value="")
|
| 75 |
messages = state["messages"]
|
| 76 |
-
|
| 77 |
if not state["system_used"]:
|
| 78 |
messages.insert(0, {"role": "system", "content": SYSTEM_PROMPT})
|
| 79 |
state["system_used"] = True
|
| 80 |
-
|
| 81 |
messages.append({"role": "user", "content": user_message})
|
| 82 |
-
|
| 83 |
try:
|
| 84 |
client = get_client()
|
| 85 |
response = client.chat_completion(
|
|
@@ -92,10 +89,8 @@ def on_user_message(user_message: str, state: Dict):
|
|
| 92 |
assistant_reply = response.choices[0].message["content"].strip()
|
| 93 |
except:
|
| 94 |
assistant_reply = mock_predict(user_message)
|
| 95 |
-
|
| 96 |
messages.append({"role": "assistant", "content": assistant_reply})
|
| 97 |
state["messages"] = messages
|
| 98 |
-
|
| 99 |
chat_history = []
|
| 100 |
for msg in messages:
|
| 101 |
if msg["role"] != "system" and msg["content"].strip():
|
|
@@ -106,56 +101,28 @@ def on_user_message(user_message: str, state: Dict):
|
|
| 106 |
f'</span></div>'
|
| 107 |
)
|
| 108 |
chat_history.append({"role": msg["role"], "content": html})
|
| 109 |
-
|
| 110 |
return chat_history, state, gr.update(value="")
|
| 111 |
|
| 112 |
# -----------------------------
|
| 113 |
# Build UI
|
| 114 |
# -----------------------------
|
| 115 |
def build_ui():
|
| 116 |
-
|
| 117 |
css = """
|
| 118 |
body {background-color:#000000; color:#00aaff;}
|
| 119 |
-
|
| 120 |
-
.chat-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
.chat-box {
|
| 128 |
-
flex:2;
|
| 129 |
-
overflow-y:auto;
|
| 130 |
-
padding:10px;
|
| 131 |
-
border:1px solid #00aaff;
|
| 132 |
-
border-radius:5px;
|
| 133 |
-
background:#000;
|
| 134 |
-
height:250px !important;
|
| 135 |
-
}
|
| 136 |
-
|
| 137 |
-
.ads-box {
|
| 138 |
-
flex:1;
|
| 139 |
-
display:flex;
|
| 140 |
-
flex-direction:column;
|
| 141 |
-
gap:10px;
|
| 142 |
}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
background-color:#111;
|
| 146 |
-
color:#fff;
|
| 147 |
-
flex:1;
|
| 148 |
-
display:flex;
|
| 149 |
-
justify-content:center;
|
| 150 |
-
align-items:center;
|
| 151 |
-
font-weight:bold;
|
| 152 |
-
border:1px solid #00aaff;
|
| 153 |
-
border-radius:5px;
|
| 154 |
-
height:120px;
|
| 155 |
}
|
| 156 |
-
|
| 157 |
-
/* Окно ввода текста — одинаковое с чатом */
|
| 158 |
.input-styled textarea {
|
|
|
|
| 159 |
background:#000000 !important;
|
| 160 |
color:#00aaff !important;
|
| 161 |
border:1px solid #00aaff !important;
|
|
@@ -163,13 +130,15 @@ def build_ui():
|
|
| 163 |
padding:10px !important;
|
| 164 |
font-size:14px !important;
|
| 165 |
font-family:Arial, sans-serif !important;
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
| 170 |
}
|
| 171 |
"""
|
| 172 |
-
|
| 173 |
autoscroll_js = """
|
| 174 |
<script>
|
| 175 |
const chatObserver = new MutationObserver((mutations) => {
|
|
@@ -179,12 +148,10 @@ def build_ui():
|
|
| 179 |
document.querySelectorAll('.chat-box').forEach(el => chatObserver.observe(el, {childList:true, subtree:true}));
|
| 180 |
</script>
|
| 181 |
"""
|
| 182 |
-
|
| 183 |
initial_bot_message = (
|
| 184 |
-
"Привет! Я
|
| 185 |
-
"
|
| 186 |
)
|
| 187 |
-
|
| 188 |
chat_history_initial = [
|
| 189 |
{
|
| 190 |
"role": "assistant",
|
|
@@ -195,51 +162,36 @@ def build_ui():
|
|
| 195 |
),
|
| 196 |
}
|
| 197 |
]
|
| 198 |
-
|
| 199 |
with gr.Blocks() as app:
|
| 200 |
gr.HTML(f"<style>{css}</style>")
|
| 201 |
gr.HTML(autoscroll_js)
|
| 202 |
-
|
| 203 |
with gr.Row(elem_classes="chat-container"):
|
| 204 |
with gr.Column(scale=3):
|
| 205 |
-
|
| 206 |
-
chat = gr.Chatbot(
|
| 207 |
-
value=chat_history_initial,
|
| 208 |
-
elem_classes="chat-box"
|
| 209 |
-
)
|
| 210 |
-
|
| 211 |
input_box = gr.Textbox(
|
| 212 |
placeholder="Введите сообщение…",
|
|
|
|
| 213 |
elem_classes="input-styled",
|
| 214 |
-
|
| 215 |
-
lines=5,
|
| 216 |
-
label=None
|
| 217 |
)
|
| 218 |
-
|
| 219 |
state = gr.State(reset_state())
|
| 220 |
-
|
| 221 |
input_box.submit(
|
| 222 |
on_user_message,
|
| 223 |
inputs=[input_box, state],
|
| 224 |
outputs=[chat, state, input_box]
|
| 225 |
)
|
| 226 |
-
|
| 227 |
gr.Button("Очистить чат").click(
|
| 228 |
lambda: (chat_history_initial, reset_state(), gr.update(value="")),
|
| 229 |
None,
|
| 230 |
[chat, state, input_box]
|
| 231 |
)
|
| 232 |
-
|
| 233 |
with gr.Column(scale=1):
|
| 234 |
with gr.Row(elem_classes="ads-box"):
|
| 235 |
gr.HTML('<div class="ad">Здесь может быть ваша реклама</div>')
|
| 236 |
gr.HTML('<div class="ad">Здесь может быть ваша реклама</div>')
|
| 237 |
-
|
| 238 |
return app
|
| 239 |
|
| 240 |
-
|
| 241 |
app = build_ui()
|
| 242 |
-
|
| 243 |
if __name__ == "__main__":
|
| 244 |
app.queue(max_size=5)
|
| 245 |
-
app.launch(server_name="0.0.0.0", server_port=7860, debug=True, share=False)
|
|
|
|
| 73 |
if not user_message.strip():
|
| 74 |
return [], state, gr.update(value="")
|
| 75 |
messages = state["messages"]
|
|
|
|
| 76 |
if not state["system_used"]:
|
| 77 |
messages.insert(0, {"role": "system", "content": SYSTEM_PROMPT})
|
| 78 |
state["system_used"] = True
|
|
|
|
| 79 |
messages.append({"role": "user", "content": user_message})
|
|
|
|
| 80 |
try:
|
| 81 |
client = get_client()
|
| 82 |
response = client.chat_completion(
|
|
|
|
| 89 |
assistant_reply = response.choices[0].message["content"].strip()
|
| 90 |
except:
|
| 91 |
assistant_reply = mock_predict(user_message)
|
|
|
|
| 92 |
messages.append({"role": "assistant", "content": assistant_reply})
|
| 93 |
state["messages"] = messages
|
|
|
|
| 94 |
chat_history = []
|
| 95 |
for msg in messages:
|
| 96 |
if msg["role"] != "system" and msg["content"].strip():
|
|
|
|
| 101 |
f'</span></div>'
|
| 102 |
)
|
| 103 |
chat_history.append({"role": msg["role"], "content": html})
|
|
|
|
| 104 |
return chat_history, state, gr.update(value="")
|
| 105 |
|
| 106 |
# -----------------------------
|
| 107 |
# Build UI
|
| 108 |
# -----------------------------
|
| 109 |
def build_ui():
|
|
|
|
| 110 |
css = """
|
| 111 |
body {background-color:#000000; color:#00aaff;}
|
| 112 |
+
.chat-container {display:flex; flex-direction:row; gap:20px; height:600px;}
|
| 113 |
+
.chat-box { flex:2; overflow-y:auto; padding:10px; border:1px solid #00aaff; border-radius:5px; background:#000; width:100%; }
|
| 114 |
+
.ads-box {flex:1; display:flex; flex-direction:column; gap:10px; height:600px;}
|
| 115 |
+
.ad { background-color:#111; color:#fff; flex:1; display:flex; justify-content:center; align-items:center; font-weight:bold; border:1px solid #00aaff; border-radius:5px; }
|
| 116 |
+
/* СТИЛЬ ОКНА ВВОДА — КАК ОКНО ЧАТА */
|
| 117 |
+
.input-styled {
|
| 118 |
+
width:100% !important;
|
| 119 |
+
max-width: none !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
}
|
| 121 |
+
.input-styled .wrap {
|
| 122 |
+
width:100% !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
}
|
|
|
|
|
|
|
| 124 |
.input-styled textarea {
|
| 125 |
+
width:100% !important;
|
| 126 |
background:#000000 !important;
|
| 127 |
color:#00aaff !important;
|
| 128 |
border:1px solid #00aaff !important;
|
|
|
|
| 130 |
padding:10px !important;
|
| 131 |
font-size:14px !important;
|
| 132 |
font-family:Arial, sans-serif !important;
|
| 133 |
+
resize: vertical;
|
| 134 |
+
min-height: 40px;
|
| 135 |
+
}
|
| 136 |
+
.input-styled label {
|
| 137 |
+
color: #00aaff !important;
|
| 138 |
+
font-family: Arial, sans-serif;
|
| 139 |
+
font-size: 14px;
|
| 140 |
}
|
| 141 |
"""
|
|
|
|
| 142 |
autoscroll_js = """
|
| 143 |
<script>
|
| 144 |
const chatObserver = new MutationObserver((mutations) => {
|
|
|
|
| 148 |
document.querySelectorAll('.chat-box').forEach(el => chatObserver.observe(el, {childList:true, subtree:true}));
|
| 149 |
</script>
|
| 150 |
"""
|
|
|
|
| 151 |
initial_bot_message = (
|
| 152 |
+
"Привет! Я консультант Ozon. Могу помочь с товарами, заказами и продажами. "
|
| 153 |
+
"С чем хотите помочь первым?"
|
| 154 |
)
|
|
|
|
| 155 |
chat_history_initial = [
|
| 156 |
{
|
| 157 |
"role": "assistant",
|
|
|
|
| 162 |
),
|
| 163 |
}
|
| 164 |
]
|
|
|
|
| 165 |
with gr.Blocks() as app:
|
| 166 |
gr.HTML(f"<style>{css}</style>")
|
| 167 |
gr.HTML(autoscroll_js)
|
|
|
|
| 168 |
with gr.Row(elem_classes="chat-container"):
|
| 169 |
with gr.Column(scale=3):
|
| 170 |
+
chat = gr.Chatbot(value=chat_history_initial, elem_classes="chat-box")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
input_box = gr.Textbox(
|
| 172 |
placeholder="Введите сообщение…",
|
| 173 |
+
label="Сообщение",
|
| 174 |
elem_classes="input-styled",
|
| 175 |
+
lines=1
|
|
|
|
|
|
|
| 176 |
)
|
|
|
|
| 177 |
state = gr.State(reset_state())
|
|
|
|
| 178 |
input_box.submit(
|
| 179 |
on_user_message,
|
| 180 |
inputs=[input_box, state],
|
| 181 |
outputs=[chat, state, input_box]
|
| 182 |
)
|
|
|
|
| 183 |
gr.Button("Очистить чат").click(
|
| 184 |
lambda: (chat_history_initial, reset_state(), gr.update(value="")),
|
| 185 |
None,
|
| 186 |
[chat, state, input_box]
|
| 187 |
)
|
|
|
|
| 188 |
with gr.Column(scale=1):
|
| 189 |
with gr.Row(elem_classes="ads-box"):
|
| 190 |
gr.HTML('<div class="ad">Здесь может быть ваша реклама</div>')
|
| 191 |
gr.HTML('<div class="ad">Здесь может быть ваша реклама</div>')
|
|
|
|
| 192 |
return app
|
| 193 |
|
|
|
|
| 194 |
app = build_ui()
|
|
|
|
| 195 |
if __name__ == "__main__":
|
| 196 |
app.queue(max_size=5)
|
| 197 |
+
app.launch(server_name="0.0.0.0", server_port=7860, debug=True, share=False)
|