ubermenchh commited on
Commit
742e3f8
1 Parent(s): 4eb428c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -21
app.py CHANGED
@@ -1,30 +1,28 @@
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 HuggingFaceEmbeddings
7
- from langchain.vectorstores import Chroma
8
- from langchain.chains import RetrievalQA
9
 
10
- repo_id = 'mistralai/Mistral-7B-v0.1'
11
- client = arxiv.Client()
12
 
13
  def loading_paper(): return 'Loading...'
14
 
15
  def paper_changes(paper_id):
16
  paper = next(arxiv.Client().results(arxiv.Search(id_list=[paper_id])))
17
- loader = OnlinePDFLoader(paper.download_pdf())
18
- documents = loader.load()
19
- text_splitter = CharacterTextSplitter(chunk_size=1024, chunk_overlap=0)
20
- texts = text_splitter.split_documents(documents)
21
- embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
22
- db = Chroma.from_documents(texts, embeddings, persist_directory="chroma_db")
23
- retriever = db.as_retriever()
24
- llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={'temperature': 0.5, 'max_new_tokens': 2096})
25
- global qa
26
- qa = RetrievalQA.from_chain_type(llm=llm, chain_type='stuff', retriever=retriever, return_source_documents=True)
27
- return 'Ready!!'
28
 
29
  def add_text(history, text):
30
  history = history + [(text, None)]
@@ -32,12 +30,12 @@ def add_text(history, text):
32
 
33
  def bot(history):
34
  response = infer(history[-1][0])
35
- history[-1][1] = response['result']
36
  return history
37
 
38
  def infer(question):
39
- result = qa({'query': question})
40
- return result
41
 
42
  with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
43
  with gr.Column():
 
1
  import arxiv
2
  import gradio as gr
3
+ from llama_index import (
4
+ VectorStoreIndex,
5
+ ServiceContext,
6
+ SimpleDirectoryReader,
7
+ Document
8
+ )
9
  from langchain.llms import HuggingFaceHub
10
+ from llama_index.llms import LangChainLLM
 
 
11
 
12
+ repo_id = 'HuggingFaceH4/zephyr-7b-beta'
 
13
 
14
  def loading_paper(): return 'Loading...'
15
 
16
  def paper_changes(paper_id):
17
  paper = next(arxiv.Client().results(arxiv.Search(id_list=[paper_id])))
18
+ docs = SimpleDirectoryReader(input_files=[paper.download_pdf()]).load_data()
19
+ doc = Document(text='\n\n'.join([doc.text for doc in docs]))
20
+ llm = LangChainLLM(llm=HuggingFaceHub(repo_id=repo_id, model_kwargs={'temperature': 0.3}))
21
+ service_context = ServiceContext.from_defaults(llm=llm, embed_model="local:BAAI/bge-small-en-v1.5")
22
+ index = VectorStoreIndex.from_documents([doc], service_context=service_context)
23
+ global query_engine
24
+ query_engine = index.as_query_engine()
25
+ return 'Ready!!!'
 
 
 
26
 
27
  def add_text(history, text):
28
  history = history + [(text, None)]
 
30
 
31
  def bot(history):
32
  response = infer(history[-1][0])
33
+ history[-1][1] = response
34
  return history
35
 
36
  def infer(question):
37
+ response = query_engine.query(question)
38
+ return response
39
 
40
  with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
41
  with gr.Column():