teslatony commited on
Commit
3ea2ad2
·
verified ·
1 Parent(s): 95fc597

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -20
app.py CHANGED
@@ -73,10 +73,13 @@ 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
  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,8 +92,10 @@ def on_user_message(user_message: str, state: Dict):
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,38 +106,70 @@ def on_user_message(user_message: str, state: Dict):
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; max-width:70%; }
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
- max-width:70% !important;
119
- background:#000000 !important;
120
- border:1px solid #00aaff !important;
121
- border-radius:5px !important;
122
- padding:10px !important;
123
- font-size:14px !important;
124
- font-family:Arial, sans-serif !important;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  }
 
 
126
  .input-styled textarea {
127
- color:#00aaff !important;
128
  background:#000000 !important;
 
129
  border:1px solid #00aaff !important;
130
  border-radius:5px !important;
131
  padding:10px !important;
132
  font-size:14px !important;
133
  font-family:Arial, sans-serif !important;
 
 
 
 
134
  }
135
  """
 
136
  autoscroll_js = """
137
  <script>
138
  const chatObserver = new MutationObserver((mutations) => {
@@ -142,10 +179,12 @@ def build_ui():
142
  document.querySelectorAll('.chat-box').forEach(el => chatObserver.observe(el, {childList:true, subtree:true}));
143
  </script>
144
  """
 
145
  initial_bot_message = (
146
- "Привет! Я консультант Ozon. Могу помочь с товарами, заказами и продажами. "
147
- "С чем хотите помочь первым?"
148
  )
 
149
  chat_history_initial = [
150
  {
151
  "role": "assistant",
@@ -156,35 +195,51 @@ def build_ui():
156
  ),
157
  }
158
  ]
 
159
  with gr.Blocks() as app:
160
  gr.HTML(f"<style>{css}</style>")
161
  gr.HTML(autoscroll_js)
 
162
  with gr.Row(elem_classes="chat-container"):
163
  with gr.Column(scale=3):
164
- chat = gr.Chatbot(value=chat_history_initial, elem_classes="chat-box")
 
 
 
 
 
165
  input_box = gr.Textbox(
166
  placeholder="Введите сообщение…",
167
- label="Сообщение",
168
- elem_classes="input-styled"
 
 
169
  )
 
170
  state = gr.State(reset_state())
 
171
  input_box.submit(
172
  on_user_message,
173
  inputs=[input_box, state],
174
  outputs=[chat, state, input_box]
175
  )
 
176
  gr.Button("Очистить чат").click(
177
  lambda: (chat_history_initial, reset_state(), gr.update(value="")),
178
  None,
179
  [chat, state, input_box]
180
  )
 
181
  with gr.Column(scale=1):
182
  with gr.Row(elem_classes="ads-box"):
183
  gr.HTML('<div class="ad">Здесь может быть ваша реклама</div>')
184
  gr.HTML('<div class="ad">Здесь может быть ваша реклама</div>')
 
185
  return app
186
 
 
187
  app = build_ui()
 
188
  if __name__ == "__main__":
189
  app.queue(max_size=5)
190
- 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
+
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
  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
  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-container {
121
+ display:flex;
122
+ flex-direction:row;
123
+ gap:20px;
124
+ height:auto;
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
+ .ad {
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;
162
  border-radius:5px !important;
163
  padding:10px !important;
164
  font-size:14px !important;
165
  font-family:Arial, sans-serif !important;
166
+ height:250px !important;
167
+ resize:none !important;
168
+ width:100% !important;
169
+ box-sizing:border-box !important;
170
  }
171
  """
172
+
173
  autoscroll_js = """
174
  <script>
175
  const chatObserver = new MutationObserver((mutations) => {
 
179
  document.querySelectorAll('.chat-box').forEach(el => chatObserver.observe(el, {childList:true, subtree:true}));
180
  </script>
181
  """
182
+
183
  initial_bot_message = (
184
+ "Привет! Я твой консультант по Ozon. Помогаю с товарами, аналитикой, карточками, продажами "
185
+ "и сложными ситуациями. С чего начнем?"
186
  )
187
+
188
  chat_history_initial = [
189
  {
190
  "role": "assistant",
 
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
+ multiline=True,
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)