AnatoliiG commited on
Commit
bf97fda
·
1 Parent(s): c581315

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -2
app.py CHANGED
@@ -79,7 +79,7 @@ async def chat_completions(request: Request):
79
 
80
 
81
  def user_input(user_message, history):
82
- # Gradio 5: История - это список словарей. Добавляем сообщение пользователя.
83
  if history is None:
84
  history = []
85
  return "", history + [{"role": "user", "content": user_message}]
@@ -91,12 +91,22 @@ def bot_response(history, system_prompt, temperature, max_tokens):
91
  yield history
92
  return
93
 
 
94
  messages = [{"role": "system", "content": system_prompt}]
95
 
 
96
  relevant_history = history[-10:] if len(history) > 10 else history
 
97
  for msg in relevant_history:
98
- messages.append({"role": msg["role"], "content": msg["content"]})
 
 
 
 
 
 
99
 
 
100
  history.append({"role": "assistant", "content": ""})
101
 
102
  partial_text = ""
@@ -112,6 +122,7 @@ def bot_response(history, system_prompt, temperature, max_tokens):
112
  delta = chunk["choices"][0]["delta"]
113
  if "content" in delta:
114
  partial_text += delta["content"]
 
115
  history[-1]["content"] = partial_text
116
  yield history
117
 
@@ -154,6 +165,7 @@ with gr.Blocks(theme=theme, css=custom_css, title="Qwen Coder Pro") as demo:
154
  label="Conversation",
155
  elem_id="chatbot",
156
  avatar_images=(None, "https://api.iconify.design/noto:robot.svg"),
 
157
  )
158
 
159
  msg = gr.Textbox(
@@ -168,6 +180,7 @@ with gr.Blocks(theme=theme, css=custom_css, title="Qwen Coder Pro") as demo:
168
  submit_btn.click(user_input, [msg, chatbot], [msg, chatbot], queue=False).then(
169
  bot_response, [chatbot, system_prompt, temperature, max_tokens], chatbot
170
  )
 
171
  clear_btn.click(lambda: [], None, chatbot, queue=False)
172
 
173
  app = mount_gradio_app(app, demo, path="/")
 
79
 
80
 
81
  def user_input(user_message, history):
82
+ # Если история пуста, инициализируем список
83
  if history is None:
84
  history = []
85
  return "", history + [{"role": "user", "content": user_message}]
 
91
  yield history
92
  return
93
 
94
+ # Формируем сообщения для Llama
95
  messages = [{"role": "system", "content": system_prompt}]
96
 
97
+ # Берем последние 10 сообщений для контекста
98
  relevant_history = history[-10:] if len(history) > 10 else history
99
+
100
  for msg in relevant_history:
101
+ content = msg["content"]
102
+
103
+ if isinstance(content, list):
104
+ content = "\n".join(str(item) for item in content)
105
+ # --------------------------
106
+
107
+ messages.append({"role": msg["role"], "content": str(content)})
108
 
109
+ # Добавляем пустой ответ ассистента в историю для стриминга
110
  history.append({"role": "assistant", "content": ""})
111
 
112
  partial_text = ""
 
122
  delta = chunk["choices"][0]["delta"]
123
  if "content" in delta:
124
  partial_text += delta["content"]
125
+ # Обновляем последнее сообщение
126
  history[-1]["content"] = partial_text
127
  yield history
128
 
 
165
  label="Conversation",
166
  elem_id="chatbot",
167
  avatar_images=(None, "https://api.iconify.design/noto:robot.svg"),
168
+ type="messages",
169
  )
170
 
171
  msg = gr.Textbox(
 
180
  submit_btn.click(user_input, [msg, chatbot], [msg, chatbot], queue=False).then(
181
  bot_response, [chatbot, system_prompt, temperature, max_tokens], chatbot
182
  )
183
+ # Исправлена очистка для type="messages" (возвращаем пустой список)
184
  clear_btn.click(lambda: [], None, chatbot, queue=False)
185
 
186
  app = mount_gradio_app(app, demo, path="/")