fffiloni commited on
Commit
310367e
1 Parent(s): 0cc73a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -3,27 +3,37 @@ import gradio as gr
3
  from langchain.document_loaders import OnlinePDFLoader
4
 
5
  from langchain.text_splitter import CharacterTextSplitter
6
- text_splitter = CharacterTextSplitter(chunk_size=350, chunk_overlap=0)
7
 
8
  from langchain.llms import HuggingFaceHub
9
- flan_ul2 = HuggingFaceHub(repo_id="google/flan-ul2", model_kwargs={"temperature":0.1, "max_new_tokens":300})
10
 
11
  from langchain.embeddings import HuggingFaceHubEmbeddings
12
- embeddings = HuggingFaceHubEmbeddings()
13
 
14
  from langchain.vectorstores import Chroma
15
 
16
  from langchain.chains import RetrievalQA
 
 
 
 
 
 
 
 
 
17
  def loading_pdf():
18
  return "Loading..."
 
19
  def pdf_changes(pdf_doc):
 
20
  loader = OnlinePDFLoader(pdf_doc.name)
21
  documents = loader.load()
 
22
  texts = text_splitter.split_documents(documents)
 
23
  db = Chroma.from_documents(texts, embeddings)
24
  retriever = db.as_retriever()
25
  global qa
26
- qa = RetrievalQA.from_chain_type(llm=flan_ul2, chain_type="stuff", retriever=retriever, return_source_documents=True)
27
  return "Ready"
28
 
29
  def add_text(history, text):
 
3
  from langchain.document_loaders import OnlinePDFLoader
4
 
5
  from langchain.text_splitter import CharacterTextSplitter
 
6
 
7
  from langchain.llms import HuggingFaceHub
 
8
 
9
  from langchain.embeddings import HuggingFaceHubEmbeddings
 
10
 
11
  from langchain.vectorstores import Chroma
12
 
13
  from langchain.chains import RetrievalQA
14
+
15
+ global llm
16
+
17
+ def define_llm_model(repo_id):
18
+ llm = HuggingFaceHub(repo_id=repo_id, model_kwargs={"temperature":0.1, "max_new_tokens":300})
19
+ return "LLM model loaded"
20
+
21
+ define_llm_model("google/flan-ul2")
22
+
23
  def loading_pdf():
24
  return "Loading..."
25
+
26
  def pdf_changes(pdf_doc):
27
+
28
  loader = OnlinePDFLoader(pdf_doc.name)
29
  documents = loader.load()
30
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
31
  texts = text_splitter.split_documents(documents)
32
+ embeddings = HuggingFaceHubEmbeddings()
33
  db = Chroma.from_documents(texts, embeddings)
34
  retriever = db.as_retriever()
35
  global qa
36
+ qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True)
37
  return "Ready"
38
 
39
  def add_text(history, text):