Spaces:
Paused
Paused
Rahul Bhoyar
commited on
Commit
·
8225db2
1
Parent(s):
8e9bdaf
Uploaded files
Browse files- app.py +46 -37
- app_archive.py +53 -0
app.py
CHANGED
|
@@ -14,40 +14,49 @@ def read_pdf(uploaded_file):
|
|
| 14 |
text += pdf_reader.pages[page_num].extract_text()
|
| 15 |
return text
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
st.
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
st.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
st.success("
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
text += pdf_reader.pages[page_num].extract_text()
|
| 15 |
return text
|
| 16 |
|
| 17 |
+
def querying(query_engine):
|
| 18 |
+
progress_container = st.empty()
|
| 19 |
+
query = st.text_input("Enter the Query for PDF:")
|
| 20 |
+
submit = st.button("Generate The response for the query")
|
| 21 |
+
|
| 22 |
+
if submit:
|
| 23 |
+
progress_container.text("Fetching the response...")
|
| 24 |
+
response = query_engine.query(query)
|
| 25 |
+
st.write(f"**Response:** {response}")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# docs = document_search.similarity_search(query_text)
|
| 29 |
+
# output = chain.run(input_documents=docs, question=query_text)
|
| 30 |
+
# st.write(output)
|
| 31 |
+
|
| 32 |
+
def main():
|
| 33 |
+
st.title("PdfQuerier using LLAMA by Rahul Bhoyar")
|
| 34 |
+
hf_token = st.text_input("Enter your Hugging Face token:")
|
| 35 |
+
llm = HuggingFaceInferenceAPI(model_name="HuggingFaceH4/zephyr-7b-alpha", token=hf_token)
|
| 36 |
+
uploaded_file = st.file_uploader("Choose a PDF file", type=["pdf"])
|
| 37 |
+
|
| 38 |
+
if uploaded_file is not None:
|
| 39 |
+
file_contents = read_pdf(uploaded_file)
|
| 40 |
+
documents = Document(text=file_contents)
|
| 41 |
+
documents = [documents]
|
| 42 |
+
st.success("Documents loaded successfully!")
|
| 43 |
+
|
| 44 |
+
embed_model_uae = HuggingFaceEmbedding(model_name="WhereIsAI/UAE-Large-V1")
|
| 45 |
+
service_context = ServiceContext.from_defaults(llm=llm, chunk_size=800, chunk_overlap=20, embed_model=embed_model_uae)
|
| 46 |
+
|
| 47 |
+
# Indexing the documents
|
| 48 |
+
progress_container = st.empty()
|
| 49 |
+
progress_container.text("Creating VectorStoreIndex...")
|
| 50 |
+
# Download embeddings from OpenAI
|
| 51 |
+
|
| 52 |
+
index = VectorStoreIndex.from_documents(documents, service_context=service_context, show_progress=True)
|
| 53 |
+
index.storage_context.persist()
|
| 54 |
+
query_engine = index.as_query_engine()
|
| 55 |
+
st.success("VectorStoreIndex created successfully!")
|
| 56 |
+
|
| 57 |
+
querying(query_engine)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
main()
|
| 62 |
+
|
app_archive.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PyPDF2 import PdfReader
|
| 3 |
+
from llama_index.llms import HuggingFaceInferenceAPI
|
| 4 |
+
from llama_index import VectorStoreIndex
|
| 5 |
+
from llama_index.embeddings import HuggingFaceEmbedding
|
| 6 |
+
from llama_index import ServiceContext
|
| 7 |
+
from llama_index.schema import Document
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def read_pdf(uploaded_file):
|
| 11 |
+
pdf_reader = PdfReader(uploaded_file)
|
| 12 |
+
text = ""
|
| 13 |
+
for page_num in range(len(pdf_reader.pages)):
|
| 14 |
+
text += pdf_reader.pages[page_num].extract_text()
|
| 15 |
+
return text
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
st.title("PdfQuerier using LLAMA by Rahul Bhoyar")
|
| 20 |
+
hf_token = st.text_input("Enter your Hugging Face token:")
|
| 21 |
+
llm = HuggingFaceInferenceAPI(model_name="HuggingFaceH4/zephyr-7b-alpha", token=hf_token)
|
| 22 |
+
st.markdown("Query your pdf file data with using this chatbot.")
|
| 23 |
+
uploaded_file = st.file_uploader("Choose a PDF file", type=["pdf"])
|
| 24 |
+
|
| 25 |
+
# Creation of Embedding model
|
| 26 |
+
embed_model_uae = HuggingFaceEmbedding(model_name="WhereIsAI/UAE-Large-V1")
|
| 27 |
+
service_context = ServiceContext.from_defaults(llm=llm, chunk_size=800, chunk_overlap=20, embed_model=embed_model_uae)
|
| 28 |
+
|
| 29 |
+
if uploaded_file is not None:
|
| 30 |
+
file_contents = read_pdf(uploaded_file)
|
| 31 |
+
documents = Document(text=file_contents)
|
| 32 |
+
documents = [documents]
|
| 33 |
+
st.success("Documents loaded successfully!")
|
| 34 |
+
|
| 35 |
+
# Indexing the documents
|
| 36 |
+
progress_container = st.empty()
|
| 37 |
+
progress_container.text("Creating VectorStoreIndex...")
|
| 38 |
+
# Code to create VectorStoreIndex
|
| 39 |
+
index = VectorStoreIndex.from_documents(documents, service_context=service_context, show_progress=True)
|
| 40 |
+
# Persist Storage Context
|
| 41 |
+
index.storage_context.persist()
|
| 42 |
+
st.success("VectorStoreIndex created successfully!")
|
| 43 |
+
# Create Query Engine
|
| 44 |
+
query = st.text_input("Ask a question:")
|
| 45 |
+
query_engine = index.as_query_engine()
|
| 46 |
+
|
| 47 |
+
if query:
|
| 48 |
+
# Run Query
|
| 49 |
+
progress_container.text("Fetching the response...")
|
| 50 |
+
response = query_engine.query(query)
|
| 51 |
+
st.markdown(f"**Response:** {response}")
|
| 52 |
+
|
| 53 |
+
|