beriond commited on
Commit
36401d1
路
verified 路
1 Parent(s): adb5e9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -66
app.py CHANGED
@@ -3,14 +3,25 @@ from transformers import pipeline
3
  from langchain_community.llms import HuggingFacePipeline
4
  from langchain_community.embeddings import HuggingFaceEmbeddings
5
  from langchain_community.vectorstores import FAISS
6
- from langchain_community.document_loaders import TextLoader
7
  from langchain.text_splitter import RecursiveCharacterTextSplitter
8
  from langchain.chains import ConversationalRetrievalChain
9
  from langchain.memory import ConversationBufferMemory
10
- from langchain.agents import Tool, initialize_agent
11
- from langchain.agents.agent_types import AgentType
12
 
13
- # Modelo com melhor performance para agentes
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  pipe = pipeline(
15
  "text2text-generation",
16
  model="declare-lab/flan-alpaca-base",
@@ -20,81 +31,39 @@ pipe = pipeline(
20
  )
21
  llm = HuggingFacePipeline(pipeline=pipe)
22
 
23
- # Mem贸ria limitada para evitar prompts muito longos
24
  memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True, k=3)
25
 
26
- # Carrega e divide documento em partes menores
27
- def carregar_documento_txt(path):
28
- loader = TextLoader(path, encoding="latin-1")
29
- documentos = loader.load()
30
- splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
31
- documentos_divididos = splitter.split_documents(documentos)
32
- embeddings = HuggingFaceEmbeddings()
33
- vectorstore = FAISS.from_documents(documentos_divididos, embeddings)
34
- return vectorstore
35
-
36
- vectorstore = carregar_documento_txt("meu_documento.txt")
37
-
38
- # Cadeia de busca com mem贸ria
39
  chat_chain = ConversationalRetrievalChain.from_llm(
40
  llm=llm,
41
  retriever=vectorstore.as_retriever(),
42
  memory=memory,
43
  )
44
 
45
- # Ferramentas do agente
46
- def responder_documento(pergunta):
47
- return chat_chain.run({"question": pergunta})
48
-
49
- def responder_geral(pergunta):
50
- return llm.invoke(pergunta)
51
-
52
- tools = [
53
- Tool(
54
- name="Responder com Documento",
55
- func=responder_documento,
56
- description="Use para responder perguntas espec铆ficas sobre o documento."
57
- ),
58
- Tool(
59
- name="Responder Geral",
60
- func=responder_geral,
61
- description="Use para perguntas gerais ou bate-papo."
62
- )
63
- ]
64
-
65
- # Agente com tratamento de parsing
66
- agent = initialize_agent(
67
- tools=tools,
68
- llm=llm,
69
- agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
70
- verbose=False,
71
- handle_parsing_errors=True,
72
- )
73
 
74
- # Interface Gradio
75
  with gr.Blocks() as demo:
76
- gr.Markdown("## 馃 Chat IA com Agente e Documento TXT")
 
 
 
77
 
78
- chatbot = gr.Chatbot(label="Chat com Agente e Documento")
79
- entrada = gr.Textbox(placeholder="Digite sua pergunta e aperte Enter", show_label=False)
80
- estado_chat = gr.State([])
 
81
 
82
- def interagir(msg, history):
83
- try:
84
- resposta = agent.invoke(msg)
85
- # Extrai texto se for dict
86
- if isinstance(resposta, dict):
87
- resposta = resposta.get('output', str(resposta))
88
- elif not isinstance(resposta, str):
89
- resposta = str(resposta)
90
- except Exception as e:
91
- resposta = f"Erro ao gerar resposta: {e}"
92
- history = history + [(msg, resposta)]
93
- return "", history
94
 
95
- entrada.submit(interagir, [entrada, estado_chat], [entrada, chatbot])
96
- gr.Button("Enviar").click(interagir, [entrada, estado_chat], [entrada, chatbot])
97
 
98
- demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False)
99
 
100
 
 
3
  from langchain_community.llms import HuggingFacePipeline
4
  from langchain_community.embeddings import HuggingFaceEmbeddings
5
  from langchain_community.vectorstores import FAISS
6
+ from langchain.document_loaders import PyPDFLoader
7
  from langchain.text_splitter import RecursiveCharacterTextSplitter
8
  from langchain.chains import ConversationalRetrievalChain
9
  from langchain.memory import ConversationBufferMemory
 
 
10
 
11
+ # Carrega e divide o documento PDF fixo (local)
12
+ def carregar_documento_pdf(path):
13
+ loader = PyPDFLoader(path)
14
+ documentos = loader.load()
15
+ splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
16
+ documentos_divididos = splitter.split_documents(documentos)
17
+ embeddings = HuggingFaceEmbeddings()
18
+ vectorstore = FAISS.from_documents(documentos_divididos, embeddings)
19
+ return vectorstore
20
+
21
+ # Inicializa vectorstore com PDF fixo
22
+ vectorstore = carregar_documento_pdf("meu_documento_fixo.pdf")
23
+
24
+ # Inicializa pipeline do Flan-Alpaca
25
  pipe = pipeline(
26
  "text2text-generation",
27
  model="declare-lab/flan-alpaca-base",
 
31
  )
32
  llm = HuggingFacePipeline(pipeline=pipe)
33
 
34
+ # Mem贸ria limitada para evitar prompts muito longos (k=3 煤ltimas mensagens)
35
  memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True, k=3)
36
 
37
+ # Cadeia de Perguntas & Respostas com mem贸ria
 
 
 
 
 
 
 
 
 
 
 
 
38
  chat_chain = ConversationalRetrievalChain.from_llm(
39
  llm=llm,
40
  retriever=vectorstore.as_retriever(),
41
  memory=memory,
42
  )
43
 
44
+ # Fun莽茫o para responder pergunta com contexto de mem贸ria
45
+ def responder_pergunta(pergunta):
46
+ if not pergunta or not pergunta.strip():
47
+ return "Por favor, insira uma pergunta v谩lida."
48
+ resposta = chat_chain.run({"question": pergunta})
49
+ return resposta
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ # Interface Gradio com chat e mem贸ria
52
  with gr.Blocks() as demo:
53
+ gr.Markdown("## 馃 Chat com PDF fixo - Flan Alpaca + LangChain + FAISS + Mem贸ria")
54
+ chatbot = gr.Chatbot(label="Conversa com mem贸ria")
55
+ pergunta_input = gr.Textbox(placeholder="Digite sua pergunta aqui...", label="Pergunta")
56
+ enviar_btn = gr.Button("Enviar")
57
 
58
+ def interagir(pergunta, chat_hist):
59
+ resposta = responder_pergunta(pergunta)
60
+ chat_hist = chat_hist + [(pergunta, resposta)]
61
+ return "", chat_hist
62
 
63
+ enviar_btn.click(interagir, inputs=[pergunta_input, chatbot], outputs=[pergunta_input, chatbot])
64
+ pergunta_input.submit(interagir, inputs=[pergunta_input, chatbot], outputs=[pergunta_input, chatbot])
 
 
 
 
 
 
 
 
 
 
65
 
66
+ demo.launch(server_name="0.0.0.0", server_port=7860)
 
67
 
 
68
 
69