CoolFace
Apppublic

AIfenaike/Query_Doc

sourceHugging Faceopenrailupdated 3y agoView on Hugging Face
0likes
app.py36 linesDownload Raw Back to root
1from langchain.embeddings.openai import OpenAIEmbeddings2from langchain.vectorstores import Chroma3from langchain.text_splitter import CharacterTextSplitter4from langchain.chains.question_answering import load_qa_chain5from langchain.llms import OpenAI6import os7 8with open("Harry_Potter1 _Sorcerer's_Stone.txt") as f:9    harry_potter_text = f.read()10 11text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")12texts = text_splitter.split_text(harry_potter_text)13 14embeddings = OpenAIEmbeddings()15 16docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()17 18chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")19 20def make_inference(query):21    docs = docsearch.get_relevant_documents(query)22    return(chain.run(input_documents=docs, question=query))23 24if __name__ == "__main__":25    # make a gradio interface26    import gradio as gr27 28    gr.Interface(29        make_inference,30        [31            gr.inputs.Textbox(lines=2, label="Query"),32        ],33        gr.outputs.Textbox(label="Response"),34        title="Query My Document📄",35        description="Query My Document📄: is a tool that allows you to ask questions about a document. In this case - J.k. Rowling's: Harry Potter and the Sorcerer's Stone.",36    ).launch()