Spaces:
Sleeping
Sleeping
File size: 2,873 Bytes
6dcf428 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import arxiv
import gradio as gr
from langchain.document_loaders import OnlinePDFLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.llms import HuggingFaceHub
from langchain.embeddings import HuggingFaceHubEmbeddings
from langchain.vectorstores import Chroma
from langchain.chains import RetrievalQA
client = arxiv.Client()
def loading_paper(): return 'Loading...'
def paper_changes(paper_id):
paper = next(arxiv.Client().results(arxiv.Search(id_list=[paper_id])))
loader = PyPDFLoader(paper.download_pdf())
documents = loader.load_and_split()
text_splitter = CharacterTextSplitter(chunk_size=1024, chunk_overlap=0)
texts = text_splitter.split_documents(documents)
embeddings = HuggingFaceHubEmbeddings()
db = Chroma.from_documents(texts, embeddings)
retriever = db.as_retriever()
llm = HuggingFaceHub(repo_id='mistralai/Mistral-7B-v0.1', model_kwargs={'temperature': 0.5, 'max_new_tokens': 2096})
global qa
qa = RetrievalQA.from_chain_type(llm=llm, chain_type='stuff', retriever=retriever, return_source_documents=True)
return 'Ready!!'
def paper_changes(paper_id):
paper = next(arxiv.Client().results(arxiv.Search(id_list=[paper_id])))
loader = PyPDFLoader(paper.download_pdf())
documents = loader.load_and_split()
text_splitter = CharacterTextSplitter(chunk_size=1024, chunk_overlap=0)
texts = text_splitter.split_documents(documents)
embeddings = HuggingFaceHubEmbeddings()
db = Chroma.from_documents(texts, embeddings)
retriever = db.as_retriever()
llm = HuggingFaceHub(repo_id='mistralai/Mistral-7B-v0.1', model_kwargs={'temperature': 0.5, 'max_new_tokens': 2096})
global qa
qa = RetrievalQA.from_chain_type(llm=llm, chain_type='stuff', retriever=retriever, return_source_documents=True)
return 'Ready!!'
def bot(history):
response = infer(history[-1][0])
history[-1][1] = response['result']
return history
def infer(question):
result = qa({'query': question})
return result
with gr.Blocks() as demo:
with gr.Column():
chatbot = gr.Chatbot([], elem_id='chatbot')
with gr.Row():
paper_id = gr.Textbox(label='ArXiv Paper Id', placeholder='1706.03762')
langchain_status = gr.Textbox(label='Status', placeholder='', interactive=False)
load_paper = gr.Button('Load Paper to LangChain')
with gr.Row():
question = gr.Textbox(label='Question', placeholder='Type your query...')
submit_btn = gr.Button('Submit')
load_paper.click(paper_changes, inputs=[paper_id], outputs=[langchain_status], queue=False)
question.submit(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(bot, chatbot, chatbot)
demo.launch(share=True) |