Update main.py with gradio.chatinterface

This commit is contained in:
suoko 2023-10-28 08:59:30 +02:00 committed by GitHub
parent ce864163cd
commit d46dedbbfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,34 +29,49 @@ for file in os.listdir(docsUrl):
pdf_path = docsUrl + "/" + file pdf_path = docsUrl + "/" + file
loader = PyPDFLoader(pdf_path) loader = PyPDFLoader(pdf_path)
documents.extend(loader.load()) documents.extend(loader.load())
print("Found " + pdf_path)
elif file.endswith('.docx') or file.endswith('.doc'): elif file.endswith('.docx') or file.endswith('.doc'):
doc_path = docsUrl + "/" + file doc_path = docsUrl + "/" + file
loader = Docx2txtLoader(doc_path) loader = Docx2txtLoader(doc_path)
documents.extend(loader.load()) documents.extend(loader.load())
print("Found " + doc_path)
elif file.endswith('.txt') or file.endswith('.kt') or file.endswith('.json'): elif file.endswith('.txt') or file.endswith('.kt') or file.endswith('.json'):
text_path = docsUrl + "/" + file text_path = docsUrl + "/" + file
loader = TextLoader(text_path) loader = TextLoader(text_path)
documents.extend(loader.load()) documents.extend(loader.load())
print("Found " + text_path)
elif file.endswith('.html') or file.endswith('.htm'): elif file.endswith('.html') or file.endswith('.htm'):
text_path = docsUrl + "/" + file htm_path = docsUrl + "/" + file
loader = UnstructuredHTMLLoader(text_path) loader = UnstructuredHTMLLoader(htm_path)
documents.extend(loader.load()) documents.extend(loader.load())
print("Found " + htm_path)
text_splitter = CharacterTextSplitter(chunk_size=3500, chunk_overlap=20)
text_splitter = CharacterTextSplitter(chunk_size=2000, chunk_overlap=20)
all_splits = text_splitter.split_documents(documents) all_splits = text_splitter.split_documents(documents)
from langchain.embeddings import GPT4AllEmbeddings from langchain.embeddings import GPT4AllEmbeddings
from langchain.vectorstores import Chroma from langchain.vectorstores import Chroma
vectorstore = Chroma.from_documents(documents=all_splits, embedding=GPT4AllEmbeddings()) vectorstore = Chroma.from_documents(documents=all_splits, embedding=GPT4AllEmbeddings())
def greet(question):
def AI_response(question, history):
docs = vectorstore.similarity_search(question) docs = vectorstore.similarity_search(question)
len(docs) len(docs)
qachain=RetrievalQA.from_chain_type(ollama, retriever=vectorstore.as_retriever()) qachain=RetrievalQA.from_chain_type(ollama, retriever=vectorstore.as_retriever())
reply=qachain({"query": question}) #reply=qachain()
#reply=str(qachain({"query": question}))
reply=str(qachain.run(question))
return reply return reply
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo = gr.ChatInterface(AI_response, title="Put your files in folder" + docsUrl)
if __name__ == "__main__": if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860) demo.launch(server_name="0.0.0.0", server_port=7860)