Felipe Silva commited on
Commit
aa32ce9
·
1 Parent(s): d59e0e8

Ajustando lógica do botão

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -30,13 +30,13 @@ def process_file(file):
30
 
31
  file_size = os.path.getsize(file)
32
  if file_size > MAX_FILE_SIZE:
33
- return f"O arquivo excede o limite. Por favor, realize o upload de um arquivo que contenha no máximo {MAX_FILE_SIZE/1024/1024:.1f}MB.", gr.update(interactive=False)
34
 
35
  texto_extraido = doc_converter(file)
36
  if texto_extraido is None:
37
- return "Não foi possível extrair o texto.", gr.update(interactive=False)
38
 
39
- return texto_extraido, gr.update(interactive=True)
40
 
41
  @spaces.GPU
42
  def ask_question(texto_extraido, question):
@@ -49,10 +49,15 @@ def ask_question(texto_extraido, question):
49
  # resposta = rag_chain.run(question)
50
  response = rag_chain({"query": question})
51
  resposta = response["result"]
52
- docs_text = "\n\n\n\n\n\n===================================\n\n\n\n\n\n".join([doc.page_content for doc in response["source_documents"]])
53
 
54
  return resposta, docs_text
55
 
 
 
 
 
 
56
  def launch_app():
57
  with gr.Blocks(title="RAG", theme=gr.themes.Soft(primary_hue="blue", secondary_hue="gray", neutral_hue="slate")) as demo:
58
  gr.Markdown("# 🚀 Retrieval Augmented Generation - RAG")
@@ -72,7 +77,12 @@ def launch_app():
72
  docs_sim = gr.Textbox(label="📎 Documentos similares a sua pergunta.", lines=15)
73
 
74
  # Conecta funções
75
- file_input.change(fn=process_file, inputs=file_input, outputs=[extracted_text, ask_button])
 
 
 
 
 
76
  question_input.submit(fn=ask_question, inputs=[extracted_text, question_input], outputs=[answer_output, docs_sim])
77
  ask_button.click(fn=ask_question, inputs=[extracted_text, question_input], outputs=[answer_output, docs_sim])
78
 
 
30
 
31
  file_size = os.path.getsize(file)
32
  if file_size > MAX_FILE_SIZE:
33
+ return f"O arquivo excede o limite. Por favor, realize o upload de um arquivo que contenha no máximo {MAX_FILE_SIZE/1024/1024:.1f}MB."
34
 
35
  texto_extraido = doc_converter(file)
36
  if texto_extraido is None:
37
+ return "Não foi possível extrair o texto."
38
 
39
+ return texto_extraido
40
 
41
  @spaces.GPU
42
  def ask_question(texto_extraido, question):
 
49
  # resposta = rag_chain.run(question)
50
  response = rag_chain({"query": question})
51
  resposta = response["result"]
52
+ docs_text = "\n\n\n===================================\n\n\n".join([doc.page_content for doc in response["source_documents"]])
53
 
54
  return resposta, docs_text
55
 
56
+ def update_ask_button(extracted_text, question):
57
+ if extracted_text and question.strip():
58
+ return gr.update(interactive=True)
59
+ return gr.update(interactive=False)
60
+
61
  def launch_app():
62
  with gr.Blocks(title="RAG", theme=gr.themes.Soft(primary_hue="blue", secondary_hue="gray", neutral_hue="slate")) as demo:
63
  gr.Markdown("# 🚀 Retrieval Augmented Generation - RAG")
 
77
  docs_sim = gr.Textbox(label="📎 Documentos similares a sua pergunta.", lines=15)
78
 
79
  # Conecta funções
80
+ file_input.change(fn=process_file, inputs=file_input, outputs=extracted_text)
81
+ # Sempre que o texto extraído ou a pergunta mudar, atualiza o botão
82
+ extracted_text.change(fn=update_ask_button, inputs=[extracted_text, question_input], outputs=ask_button)
83
+ question_input.change(fn=update_ask_button, inputs=[extracted_text, question_input], outputs=ask_button)
84
+
85
+ # Chama o ask_question com Enter ou botão
86
  question_input.submit(fn=ask_question, inputs=[extracted_text, question_input], outputs=[answer_output, docs_sim])
87
  ask_button.click(fn=ask_question, inputs=[extracted_text, question_input], outputs=[answer_output, docs_sim])
88