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