Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def main():
|
2 |
+
load_dotenv()
|
3 |
+
|
4 |
+
st.set_page_config(
|
5 |
+
page_title="PDF Insights AI",
|
6 |
+
page_icon=":books:",
|
7 |
+
layout="wide"
|
8 |
+
)
|
9 |
+
st.write(css, unsafe_allow_html=True)
|
10 |
+
|
11 |
+
|
12 |
+
# Welcome section
|
13 |
+
st.title("π PDF Insights AI")
|
14 |
+
st.markdown("""
|
15 |
+
### Unlock the Knowledge in Your PDFs
|
16 |
+
- π€ AI-powered document analysis
|
17 |
+
- π¬ Ask questions about your uploaded documents
|
18 |
+
- π Support for multiple PDF files
|
19 |
+
""")
|
20 |
+
|
21 |
+
if "conversation" not in st.session_state:
|
22 |
+
st.session_state.conversation = None
|
23 |
+
if "chat_history" not in st.session_state:
|
24 |
+
st.session_state.chat_history = []
|
25 |
+
|
26 |
+
# File upload section
|
27 |
+
with st.sidebar:
|
28 |
+
st.header("π€ Upload Documents")
|
29 |
+
pdf_docs = st.file_uploader(
|
30 |
+
"Upload your PDFs here",
|
31 |
+
type=['pdf'],
|
32 |
+
accept_multiple_files=True,
|
33 |
+
help="Upload PDF files to analyze. Max file size: 200MB"
|
34 |
+
)
|
35 |
+
|
36 |
+
# File validation
|
37 |
+
if pdf_docs:
|
38 |
+
for doc in pdf_docs:
|
39 |
+
if doc.size > 200 * 1024 * 1024: # 200 MB
|
40 |
+
st.error(f"File {doc.name} is too large. Maximum file size is 200MB.")
|
41 |
+
pdf_docs.remove(doc)
|
42 |
+
|
43 |
+
if st.button("Process Documents", type="primary"):
|
44 |
+
if not pdf_docs:
|
45 |
+
st.warning("Please upload at least one PDF file.")
|
46 |
+
else:
|
47 |
+
with st.spinner("Processing your documents..."):
|
48 |
+
try:
|
49 |
+
# get pdf text
|
50 |
+
content, metadata = prepare_docs(pdf_docs)
|
51 |
+
|
52 |
+
# get the text chunks
|
53 |
+
split_docs = get_text_chunks(content, metadata)
|
54 |
+
|
55 |
+
# create vector store
|
56 |
+
vectorstore = ingest_into_vectordb(split_docs)
|
57 |
+
|
58 |
+
# create conversation chain
|
59 |
+
st.session_state.conversation = get_conversation_chain(vectorstore)
|
60 |
+
|
61 |
+
st.success("Documents processed successfully! You can now ask questions.")
|
62 |
+
except Exception as e:
|
63 |
+
st.error(f"An error occurred while processing documents: {str(e)}")
|
64 |
+
|
65 |
+
# Question input section
|
66 |
+
user_question = st.text_input(
|
67 |
+
"π Ask a question about your documents",
|
68 |
+
placeholder="What insights can you provide from these documents?"
|
69 |
+
)
|
70 |
+
|
71 |
+
if user_question:
|
72 |
+
if st.session_state.conversation is None:
|
73 |
+
st.warning("Please upload and process documents first.")
|
74 |
+
else:
|
75 |
+
handle_userinput(user_question)
|