DHEIVER commited on
Commit
5c221dc
·
verified ·
1 Parent(s): 8518c0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -118,18 +118,12 @@ Responda com base no contexto quando relevante."""
118
 
119
  # Adiciona mensagens do histórico, garantindo alternância entre "user" e "assistant"
120
  for user_msg, assistant_msg in history:
121
- if user_msg:
122
- messages.append({"role": "user", "content": user_msg})
123
- if assistant_msg:
124
- messages.append({"role": "assistant", "content": assistant_msg})
125
 
126
  # Adiciona a nova mensagem do usuário
127
  messages.append({"role": "user", "content": rag_prompt})
128
 
129
- # Garantia de que a lista de mensagens alterne corretamente
130
- assert all(messages[i]["role"] != messages[i + 1]["role"] for i in range(len(messages) - 1)), \
131
- "As mensagens devem alternar corretamente entre 'user' e 'assistant'."
132
-
133
  response = ""
134
  try:
135
  for message_chunk in client.chat_completion(
@@ -148,6 +142,8 @@ Responda com base no contexto quando relevante."""
148
 
149
  # Função para carregar PDFs
150
  def load_pdfs(pdf_files: List[gr.File]):
 
 
151
  status = knowledge_base.load_pdfs(pdf_files)
152
  return status
153
 
@@ -168,7 +164,7 @@ with gr.Blocks(title="RAG Avançado com PDFs", theme=gr.themes.Soft()) as demo:
168
  submit_btn = gr.Button("Enviar")
169
 
170
  with gr.Column(scale=1):
171
- context_box = gr.Markdown(label="Contexto Recuperado", value="Contexto aparecerá aqui após a pergunta.")
172
 
173
  with gr.Accordion("Configurações", open=False):
174
  with gr.Row():
@@ -192,10 +188,13 @@ with gr.Blocks(title="RAG Avançado com PDFs", theme=gr.themes.Soft()) as demo:
192
  # Função para atualizar o chat
193
  def submit_message(message, history, system_message, max_tokens, temperature, top_p, k_initial, k_final):
194
  history = history or []
 
 
 
 
195
  for response, context, _ in respond(message, history, system_message, max_tokens, temperature, top_p, k_initial, k_final):
196
- history.append((message, response))
197
- yield history, context, ""
198
- yield history, context, ""
199
 
200
  # Conexões de eventos
201
  submit_btn.click(
@@ -215,4 +214,4 @@ with gr.Blocks(title="RAG Avançado com PDFs", theme=gr.themes.Soft()) as demo:
215
  )
216
 
217
  if __name__ == "__main__":
218
- demo.launch()
 
118
 
119
  # Adiciona mensagens do histórico, garantindo alternância entre "user" e "assistant"
120
  for user_msg, assistant_msg in history:
121
+ messages.append({"role": "user", "content": user_msg})
122
+ messages.append({"role": "assistant", "content": assistant_msg})
 
 
123
 
124
  # Adiciona a nova mensagem do usuário
125
  messages.append({"role": "user", "content": rag_prompt})
126
 
 
 
 
 
127
  response = ""
128
  try:
129
  for message_chunk in client.chat_completion(
 
142
 
143
  # Função para carregar PDFs
144
  def load_pdfs(pdf_files: List[gr.File]):
145
+ if not pdf_files:
146
+ return "Nenhum arquivo selecionado."
147
  status = knowledge_base.load_pdfs(pdf_files)
148
  return status
149
 
 
164
  submit_btn = gr.Button("Enviar")
165
 
166
  with gr.Column(scale=1):
167
+ context_box = gr.Markdown(label="Contexto Recuperado")
168
 
169
  with gr.Accordion("Configurações", open=False):
170
  with gr.Row():
 
188
  # Função para atualizar o chat
189
  def submit_message(message, history, system_message, max_tokens, temperature, top_p, k_initial, k_final):
190
  history = history or []
191
+ return_history = history.copy()
192
+ return_history.append((message, ""))
193
+ yield return_history, "", ""
194
+
195
  for response, context, _ in respond(message, history, system_message, max_tokens, temperature, top_p, k_initial, k_final):
196
+ return_history[-1] = (message, response)
197
+ yield return_history, context, ""
 
198
 
199
  # Conexões de eventos
200
  submit_btn.click(
 
214
  )
215
 
216
  if __name__ == "__main__":
217
+ demo.launch()