Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import getpass
|
3 |
+
import streamlit as st
|
4 |
+
from langchain.document_loaders import PyPDFLoader
|
5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
7 |
+
from langchain.vectorstores import Chroma
|
8 |
+
from langchain import HuggingFaceHub
|
9 |
+
from langchain.chains import RetrievalQA
|
10 |
+
# __import__('pysqlite3')
|
11 |
+
# import sys
|
12 |
+
# sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
|
13 |
+
|
14 |
+
|
15 |
+
# load huggingface api key
|
16 |
+
hubtok = os.environ["HUGGINGFACE_HUB_TOKEN"]
|
17 |
+
|
18 |
+
# use streamlit file uploader to ask user for file
|
19 |
+
# file = st.file_uploader("Upload PDF")
|
20 |
+
|
21 |
+
|
22 |
+
path = "https://vedpuran.files.wordpress.com/2013/04/455_gita_roman.pdf"
|
23 |
+
loader = PyPDFLoader(path)
|
24 |
+
pages = loader.load()
|
25 |
+
|
26 |
+
# st.write(pages)
|
27 |
+
|
28 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=20)
|
29 |
+
docs = splitter.split_documents(pages)
|
30 |
+
|
31 |
+
embeddings = HuggingFaceEmbeddings()
|
32 |
+
doc_search = Chroma.from_documents(docs, embeddings)
|
33 |
+
|
34 |
+
repo_id = "tiiuae/falcon-7b"
|
35 |
+
llm = HuggingFaceHub(repo_id=repo_id, huggingfacehub_api_token=hubtok, model_kwargs={'temperature': 0.2,'max_length': 1000})
|
36 |
+
|
37 |
+
from langchain.schema import retriever
|
38 |
+
retireval_chain = RetrievalQA.from_chain_type(llm, chain_type="stuff", retriever=doc_search.as_retriever())
|
39 |
+
|
40 |
+
if query := st.chat_input("Enter a question: "):
|
41 |
+
with st.chat_message("assistant"):
|
42 |
+
st.write(retireval_chain.run(query))
|