File size: 1,382 Bytes
fc4137d
 
 
 
 
 
 
 
 
7e75cb5
 
 
fc4137d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import getpass
import streamlit as st
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import Chroma
from langchain import HuggingFaceHub
from langchain.chains import RetrievalQA
# __import__('pysqlite3')
# import sys
# sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')


# load huggingface api key
hubtok = os.environ["HUGGINGFACE_HUB_TOKEN"]

# use streamlit file uploader to ask user for file
# file = st.file_uploader("Upload PDF")


path = "https://vedpuran.files.wordpress.com/2013/04/455_gita_roman.pdf"
loader = PyPDFLoader(path)
pages = loader.load()

# st.write(pages)

splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=20)
docs = splitter.split_documents(pages)

embeddings = HuggingFaceEmbeddings()
doc_search = Chroma.from_documents(docs, embeddings)

repo_id = "tiiuae/falcon-7b"
llm = HuggingFaceHub(repo_id=repo_id, huggingfacehub_api_token=hubtok, model_kwargs={'temperature': 0.2,'max_length': 1000})

from langchain.schema import retriever
retireval_chain = RetrievalQA.from_chain_type(llm, chain_type="stuff", retriever=doc_search.as_retriever())

if query := st.chat_input("Enter a question: "):
  with st.chat_message("assistant"):
    st.write(retireval_chain.run(query))