Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -43,35 +43,47 @@ st.title("Blah-2")
|
|
43 |
# Step 1: Choose PDF Source
|
44 |
pdf_source = st.radio("Upload or provide a link to a PDF:", ["Enter a PDF URL", "Upload a PDF file"], index=0, horizontal=True)
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
if
|
49 |
-
st.session_state.pdf_path = "temp.pdf"
|
50 |
-
with open(st.session_state.pdf_path, "wb") as f:
|
51 |
-
f.write(uploaded_file.getbuffer())
|
52 |
-
st.session_state.pdf_loaded = False
|
53 |
-
st.session_state.chunked = False
|
54 |
-
st.session_state.vector_created = False
|
55 |
-
|
56 |
-
elif pdf_source == "Enter a PDF URL":
|
57 |
-
pdf_url = st.text_input("Enter PDF URL:", value = "https://arxiv.org/pdf/2406.06998")
|
58 |
-
if pdf_url and not st.session_state.pdf_path:
|
59 |
with st.spinner("Downloading PDF..."):
|
60 |
try:
|
61 |
-
response = requests.get(pdf_url)
|
62 |
if response.status_code == 200:
|
63 |
st.session_state.pdf_path = "temp.pdf"
|
64 |
with open(st.session_state.pdf_path, "wb") as f:
|
65 |
f.write(response.content)
|
|
|
|
|
66 |
st.session_state.pdf_loaded = False
|
67 |
st.session_state.chunked = False
|
68 |
st.session_state.vector_created = False
|
|
|
69 |
st.success("β
PDF Downloaded Successfully!")
|
70 |
else:
|
71 |
st.error("β Failed to download PDF. Check the URL.")
|
72 |
except Exception as e:
|
73 |
st.error(f"β Error downloading PDF: {e}")
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
# Step 2: Load & Process PDF (Only Once)
|
76 |
if st.session_state.pdf_path and not st.session_state.pdf_loaded:
|
77 |
with st.spinner("Loading PDF..."):
|
|
|
43 |
# Step 1: Choose PDF Source
|
44 |
pdf_source = st.radio("Upload or provide a link to a PDF:", ["Enter a PDF URL", "Upload a PDF file"], index=0, horizontal=True)
|
45 |
|
46 |
+
# Function to download and process the PDF
|
47 |
+
def download_pdf():
|
48 |
+
if st.session_state.pdf_url and not st.session_state.pdf_path:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
with st.spinner("Downloading PDF..."):
|
50 |
try:
|
51 |
+
response = requests.get(st.session_state.pdf_url)
|
52 |
if response.status_code == 200:
|
53 |
st.session_state.pdf_path = "temp.pdf"
|
54 |
with open(st.session_state.pdf_path, "wb") as f:
|
55 |
f.write(response.content)
|
56 |
+
|
57 |
+
# Reset processing state
|
58 |
st.session_state.pdf_loaded = False
|
59 |
st.session_state.chunked = False
|
60 |
st.session_state.vector_created = False
|
61 |
+
|
62 |
st.success("β
PDF Downloaded Successfully!")
|
63 |
else:
|
64 |
st.error("β Failed to download PDF. Check the URL.")
|
65 |
except Exception as e:
|
66 |
st.error(f"β Error downloading PDF: {e}")
|
67 |
|
68 |
+
if pdf_source == "Upload a PDF file":
|
69 |
+
uploaded_file = st.file_uploader("Upload your PDF file", type="pdf")
|
70 |
+
if uploaded_file:
|
71 |
+
st.session_state.pdf_path = "temp.pdf"
|
72 |
+
with open(st.session_state.pdf_path, "wb") as f:
|
73 |
+
f.write(uploaded_file.getbuffer())
|
74 |
+
st.session_state.pdf_loaded = False
|
75 |
+
st.session_state.chunked = False
|
76 |
+
st.session_state.vector_created = False
|
77 |
+
|
78 |
+
elif pdf_source == "Enter a PDF URL":
|
79 |
+
# β
Text input with Enter support
|
80 |
+
st.text_input("Enter PDF URL:", value="https://arxiv.org/pdf/2406.06998", key="pdf_url", on_change=download_pdf)
|
81 |
+
|
82 |
+
# β
Button support
|
83 |
+
if st.button("Load PDF"):
|
84 |
+
download_pdf()
|
85 |
+
|
86 |
+
|
87 |
# Step 2: Load & Process PDF (Only Once)
|
88 |
if st.session_state.pdf_path and not st.session_state.pdf_loaded:
|
89 |
with st.spinner("Loading PDF..."):
|