Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,93 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import os
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
from
|
| 6 |
-
from langchain_community.
|
| 7 |
-
from langchain_community.
|
| 8 |
-
from
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
logging.
|
| 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 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
return
|
| 64 |
-
except Exception as e:
|
| 65 |
-
st.error(f"
|
| 66 |
-
logger.exception("Exception in
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
def
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
st.
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
This is a Generative AI powered Question and Answering app that responds to questions about your PDF File.
|
| 94 |
-
"""
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
question = st.text_area("Enter your Question")
|
| 98 |
-
|
| 99 |
-
if st.button("Ask"):
|
| 100 |
-
st.info("Your Question: " + question)
|
| 101 |
-
st.info("Your Answer")
|
| 102 |
-
try:
|
| 103 |
-
answer, metadata = process_answer(question)
|
| 104 |
-
st.write(answer)
|
| 105 |
-
st.write(metadata)
|
| 106 |
-
except Exception as e:
|
| 107 |
-
st.error(f"An unexpected error occurred: {e}")
|
| 108 |
-
logger.exception("Unexpected error in main function")
|
| 109 |
-
|
| 110 |
-
if __name__ == '__main__':
|
| 111 |
main()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import logging
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 5 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain_community.vectorstores import Chroma
|
| 7 |
+
from langchain_community.llms import HuggingFacePipeline
|
| 8 |
+
from langchain.chains import RetrievalQA
|
| 9 |
+
from ingest import create_chroma_db
|
| 10 |
+
|
| 11 |
+
# Set up logging
|
| 12 |
+
logging.basicConfig(level=logging.INFO)
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
checkpoint = "LaMini-T5-738M"
|
| 16 |
+
|
| 17 |
+
@st.cache_resource
|
| 18 |
+
def load_llm():
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
| 20 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)
|
| 21 |
+
pipe = pipeline(
|
| 22 |
+
'text2text-generation',
|
| 23 |
+
model=model,
|
| 24 |
+
tokenizer=tokenizer,
|
| 25 |
+
max_length=256,
|
| 26 |
+
do_sample=True,
|
| 27 |
+
temperature=0.3,
|
| 28 |
+
top_p=0.95
|
| 29 |
+
)
|
| 30 |
+
return HuggingFacePipeline(pipeline=pipe)
|
| 31 |
+
|
| 32 |
+
def load_chroma_db():
|
| 33 |
+
chroma_dir = "chroma_db"
|
| 34 |
+
if not os.path.exists(chroma_dir):
|
| 35 |
+
st.warning("Chroma database not found. Creating a new one...")
|
| 36 |
+
create_chroma_db()
|
| 37 |
+
|
| 38 |
+
if not os.path.exists(chroma_dir):
|
| 39 |
+
st.error("Failed to create the Chroma database. Please check the 'docs' directory and try again.")
|
| 40 |
+
raise RuntimeError("Chroma database creation failed.")
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 44 |
+
db = Chroma.load_local(chroma_dir, embeddings)
|
| 45 |
+
return db.as_retriever()
|
| 46 |
+
except Exception as e:
|
| 47 |
+
st.error(f"Failed to load Chroma database: {e}")
|
| 48 |
+
logger.exception("Exception in load_chroma_db")
|
| 49 |
+
raise
|
| 50 |
+
|
| 51 |
+
def process_answer(instruction):
|
| 52 |
+
try:
|
| 53 |
+
retriever = load_chroma_db()
|
| 54 |
+
llm = load_llm()
|
| 55 |
+
qa = RetrievalQA.from_chain_type(
|
| 56 |
+
llm=llm,
|
| 57 |
+
chain_type="stuff",
|
| 58 |
+
retriever=retriever,
|
| 59 |
+
return_source_documents=True
|
| 60 |
+
)
|
| 61 |
+
generated_text = qa.invoke(instruction)
|
| 62 |
+
answer = generated_text['result']
|
| 63 |
+
return answer, generated_text
|
| 64 |
+
except Exception as e:
|
| 65 |
+
st.error(f"An error occurred while processing the answer: {e}")
|
| 66 |
+
logger.exception("Exception in process_answer")
|
| 67 |
+
return "An error occurred while processing your request.", {}
|
| 68 |
+
|
| 69 |
+
def main():
|
| 70 |
+
st.title("Search Your PDF ππ")
|
| 71 |
+
|
| 72 |
+
with st.expander("About the App"):
|
| 73 |
+
st.markdown(
|
| 74 |
+
"""
|
| 75 |
+
This is a Generative AI powered Question and Answering app that responds to questions about your PDF File.
|
| 76 |
+
"""
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
question = st.text_area("Enter your Question")
|
| 80 |
+
|
| 81 |
+
if st.button("Ask"):
|
| 82 |
+
st.info("Your Question: " + question)
|
| 83 |
+
st.info("Your Answer")
|
| 84 |
+
try:
|
| 85 |
+
answer, metadata = process_answer(question)
|
| 86 |
+
st.write(answer)
|
| 87 |
+
st.write(metadata)
|
| 88 |
+
except Exception as e:
|
| 89 |
+
st.error(f"An unexpected error occurred: {e}")
|
| 90 |
+
logger.exception("Unexpected error in main function")
|
| 91 |
+
|
| 92 |
+
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
main()
|