Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,75 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from langchain import LLMChain, ConversationChain
|
| 3 |
+
from langchain.memory import ConversationBufferMemory
|
| 4 |
+
from langchain.llms import HuggingFacePipeline
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
from langchain.document_loaders import PyPDFLoader, CSVLoader
|
| 7 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 9 |
+
from langchain.vectorstores import FAISS
|
| 10 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 11 |
+
from datasets import load_dataset
|
| 12 |
|
| 13 |
+
# Modelo Hugging Face
|
| 14 |
+
pipe = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct", max_new_tokens=200, temperature=0.7)
|
| 15 |
+
llm = HuggingFacePipeline(pipeline=pipe)
|
|
|
|
| 16 |
|
| 17 |
+
memory = ConversationBufferMemory()
|
| 18 |
|
| 19 |
+
# PDF loader e vetores
|
| 20 |
+
def init_pdf(path):
|
| 21 |
+
loader = PyPDFLoader(path)
|
| 22 |
+
docs = loader.load_and_split()
|
| 23 |
+
embeddings = HuggingFaceEmbeddings()
|
| 24 |
+
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 25 |
+
return vectorstore
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# CSV loader e vetores (ou pandas agent)
|
| 28 |
+
def init_csv(path):
|
| 29 |
+
loader = CSVLoader(path)
|
| 30 |
+
docs = loader.load_and_split()
|
| 31 |
+
embeddings = HuggingFaceEmbeddings()
|
| 32 |
+
vectorstore = FAISS.from_documents(docs, embeddings)
|
| 33 |
+
return vectorstore
|
| 34 |
|
| 35 |
+
# Fluxo de retriever
|
| 36 |
+
def make_rag_chain(vectorstore):
|
| 37 |
+
retriever = vectorstore.as_retriever()
|
| 38 |
+
return ConversationalRetrievalChain(llm=llm, retriever=retriever, memory=memory)
|
| 39 |
|
| 40 |
+
pdf_chain = None
|
| 41 |
+
csv_chain = None
|
| 42 |
|
| 43 |
+
def upload_pdf(uploaded_pdf):
|
| 44 |
+
global pdf_chain
|
| 45 |
+
vectorstore = init_pdf(uploaded_pdf.name)
|
| 46 |
+
pdf_chain = make_rag_chain(vectorstore)
|
| 47 |
+
return "PDF carregado com sucesso."
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
def upload_csv(uploaded_csv):
|
| 50 |
+
global csv_chain
|
| 51 |
+
vectorstore = init_csv(uploaded_csv.name)
|
| 52 |
+
csv_chain = make_rag_chain(vectorstore)
|
| 53 |
+
return "CSV carregado com sucesso."
|
| 54 |
|
| 55 |
+
def chat(message):
|
| 56 |
+
if pdf_chain:
|
| 57 |
+
resp = pdf_chain.run({"question":message})
|
| 58 |
+
elif csv_chain:
|
| 59 |
+
resp = csv_chain.run({"question":message})
|
| 60 |
+
else:
|
| 61 |
+
chain = ConversationChain(llm=llm, memory=memory)
|
| 62 |
+
resp = chain.predict(input=message)
|
| 63 |
+
return resp
|
| 64 |
|
| 65 |
+
with gr.Blocks() as demo:
|
| 66 |
+
gr.Markdown("## Chatbot com memória — PDF, CSV ou conversa simples")
|
| 67 |
+
pdf_file = gr.File(label="Upload PDF", file_types=['.pdf'])
|
| 68 |
+
csv_file = gr.File(label="Upload CSV", file_types=['.csv'])
|
| 69 |
+
pdf_file.upload(upload_pdf, inputs=pdf_file, outputs="text")
|
| 70 |
+
csv_file.upload(upload_csv, inputs=csv_file, outputs="text")
|
| 71 |
+
chatbot = gr.Chatbot()
|
| 72 |
+
msg = gr.Textbox(placeholder="Digite aqui sua pergunta…")
|
| 73 |
+
msg.submit(lambda m: (msg.clear(), chatbot.append((m, chat(m)))), outputs=chatbot)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
demo.launch()
|
|
|
|
|
|