Spaces:
Sleeping
Sleeping
raymondEDS
commited on
Commit
Β·
104b6b9
1
Parent(s):
68d29ed
try work around
Browse files- src/streamlit_app.py +64 -30
src/streamlit_app.py
CHANGED
|
@@ -123,43 +123,77 @@ def show_upload_documents():
|
|
| 123 |
st.title("π€ Upload Documents")
|
| 124 |
st.markdown("---")
|
| 125 |
|
|
|
|
|
|
|
|
|
|
| 126 |
uploaded_file = st.file_uploader(
|
| 127 |
"Choose a PDF file",
|
| 128 |
type=['pdf'],
|
| 129 |
-
help="Upload PDF documents to the LMS"
|
|
|
|
| 130 |
)
|
| 131 |
|
| 132 |
if uploaded_file is not None:
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
if pdf_text.strip():
|
| 148 |
-
st.subheader("π Document Preview")
|
| 149 |
-
with st.expander("View extracted text"):
|
| 150 |
-
st.text_area("PDF Content", pdf_text, height=300)
|
| 151 |
-
|
| 152 |
-
# Upload button
|
| 153 |
-
if st.button("Upload Document"):
|
| 154 |
-
# Save document info
|
| 155 |
-
save_document_info(
|
| 156 |
-
uploaded_file.name,
|
| 157 |
-
uploaded_file.read(),
|
| 158 |
-
"PDF"
|
| 159 |
-
)
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
def show_my_documents():
|
| 165 |
"""Show uploaded documents"""
|
|
|
|
| 123 |
st.title("π€ Upload Documents")
|
| 124 |
st.markdown("---")
|
| 125 |
|
| 126 |
+
# Add information about file upload
|
| 127 |
+
st.info("π‘ **Note:** File upload is optimized for Hugging Face Spaces. If you experience issues, try refreshing the page.")
|
| 128 |
+
|
| 129 |
uploaded_file = st.file_uploader(
|
| 130 |
"Choose a PDF file",
|
| 131 |
type=['pdf'],
|
| 132 |
+
help="Upload PDF documents to the LMS (max 200MB)",
|
| 133 |
+
accept_multiple_files=False
|
| 134 |
)
|
| 135 |
|
| 136 |
if uploaded_file is not None:
|
| 137 |
+
try:
|
| 138 |
+
# Display file info
|
| 139 |
+
file_details = {
|
| 140 |
+
"Filename": uploaded_file.name,
|
| 141 |
+
"File size": f"{uploaded_file.size / 1024:.2f} KB",
|
| 142 |
+
"File type": uploaded_file.type
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
st.write("**File Details:**")
|
| 146 |
+
for key, value in file_details.items():
|
| 147 |
+
st.write(f"- {key}: {value}")
|
| 148 |
+
|
| 149 |
+
# Extract and display PDF content
|
| 150 |
+
pdf_text = extract_pdf_text(uploaded_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
if pdf_text.strip():
|
| 153 |
+
st.subheader("π Document Preview")
|
| 154 |
+
with st.expander("View extracted text"):
|
| 155 |
+
st.text_area("PDF Content", pdf_text, height=300)
|
| 156 |
+
else:
|
| 157 |
+
st.warning("β οΈ Could not extract text from this PDF. The file may be image-based or encrypted.")
|
| 158 |
+
|
| 159 |
+
# Upload button
|
| 160 |
+
if st.button("Upload Document", type="primary"):
|
| 161 |
+
try:
|
| 162 |
+
# Reset file pointer to beginning
|
| 163 |
+
uploaded_file.seek(0)
|
| 164 |
+
|
| 165 |
+
# Save document info
|
| 166 |
+
save_document_info(
|
| 167 |
+
uploaded_file.name,
|
| 168 |
+
uploaded_file.read(),
|
| 169 |
+
"PDF"
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
st.success(f"β
Document '{uploaded_file.name}' uploaded successfully!")
|
| 173 |
+
st.balloons()
|
| 174 |
+
|
| 175 |
+
# Clear the file uploader
|
| 176 |
+
st.rerun()
|
| 177 |
+
|
| 178 |
+
except Exception as e:
|
| 179 |
+
st.error(f"β Error uploading document: {str(e)}")
|
| 180 |
+
st.info("π‘ Try uploading a smaller file or refresh the page.")
|
| 181 |
+
|
| 182 |
+
except Exception as e:
|
| 183 |
+
st.error(f"β Error processing file: {str(e)}")
|
| 184 |
+
st.info("π‘ Please try uploading a different PDF file.")
|
| 185 |
+
|
| 186 |
+
# Add helpful tips
|
| 187 |
+
with st.expander("π‘ Upload Tips"):
|
| 188 |
+
st.markdown("""
|
| 189 |
+
**For best results:**
|
| 190 |
+
- Use PDF files under 200MB
|
| 191 |
+
- Ensure PDFs contain text (not just images)
|
| 192 |
+
- Avoid password-protected PDFs
|
| 193 |
+
- If upload fails, try refreshing the page
|
| 194 |
+
|
| 195 |
+
**Supported formats:** PDF only
|
| 196 |
+
""")
|
| 197 |
|
| 198 |
def show_my_documents():
|
| 199 |
"""Show uploaded documents"""
|