beriond commited on
Commit
e375a46
·
verified ·
1 Parent(s): 4e024eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -12
app.py CHANGED
@@ -17,12 +17,7 @@ memory = ConversationBufferMemory(memory_key="chat_history", return_messages=Tru
17
 
18
  # 🔹 Função de carregamento do documento (TXT com codificação explícita)
19
  def carregar_documento_txt(path):
20
- # ATENÇÃO: Adicionado o parâmetro 'encoding' ao TextLoader
21
- # Tente 'latin-1' ou 'iso-8859-1' primeiro, são os mais comuns para português
22
  loader = TextLoader(path, encoding="latin-1")
23
- # Se 'latin-1' não funcionar, tente 'cp1252' (Windows-1252)
24
- # loader = TextLoader(path, encoding="cp1252")
25
-
26
  documentos = loader.load()
27
 
28
  splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
@@ -32,7 +27,7 @@ def carregar_documento_txt(path):
32
  vectorstore = FAISS.from_documents(documentos_divididos, embeddings)
33
  return vectorstore
34
 
35
- # 🔹 Carrega documento TXT do root
36
  vectorstore = carregar_documento_txt("meu_documento.txt")
37
 
38
  # 🔹 Cria cadeia de resposta com memória + busca por similaridade
@@ -42,21 +37,24 @@ chat_chain = ConversationalRetrievalChain.from_llm(
42
  memory=memory,
43
  )
44
 
45
- # 🔹 Função de chat
46
  def responder(pergunta):
47
  return chat_chain.run({"question": pergunta})
48
 
49
  # 🔹 Interface Gradio
50
  with gr.Blocks() as demo:
51
- gr.Markdown("## 🤖 Chat IA com Documento TXT fixo (consultas baseadas em documentos)")
52
- chatbot = gr.Chatbot(type='tuples')
 
53
  entrada = gr.Textbox(placeholder="Digite sua pergunta…", show_label=False)
54
 
55
- def interagir(msg, history):
56
  resposta = responder(msg)
57
- history.append([msg, resposta])
58
  return "", history
59
 
60
  entrada.submit(interagir, [entrada, chatbot], [entrada, chatbot])
61
 
62
- demo.launch()
 
 
 
17
 
18
  # 🔹 Função de carregamento do documento (TXT com codificação explícita)
19
  def carregar_documento_txt(path):
 
 
20
  loader = TextLoader(path, encoding="latin-1")
 
 
 
21
  documentos = loader.load()
22
 
23
  splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
 
27
  vectorstore = FAISS.from_documents(documentos_divididos, embeddings)
28
  return vectorstore
29
 
30
+ # 🔹 Carrega documento TXT
31
  vectorstore = carregar_documento_txt("meu_documento.txt")
32
 
33
  # 🔹 Cria cadeia de resposta com memória + busca por similaridade
 
37
  memory=memory,
38
  )
39
 
40
+ # 🔹 Função de resposta
41
  def responder(pergunta):
42
  return chat_chain.run({"question": pergunta})
43
 
44
  # 🔹 Interface Gradio
45
  with gr.Blocks() as demo:
46
+ gr.Markdown("## 🤖 Chat IA com Documento TXT fixo")
47
+
48
+ chatbot = gr.Chatbot()
49
  entrada = gr.Textbox(placeholder="Digite sua pergunta…", show_label=False)
50
 
51
+ def interagir(msg, history=[]):
52
  resposta = responder(msg)
53
+ history.append((msg, resposta))
54
  return "", history
55
 
56
  entrada.submit(interagir, [entrada, chatbot], [entrada, chatbot])
57
 
58
+ # 🔹 Lança interface sem API
59
+ demo.launch(share=False, show_api=False)
60
+