Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
subprocess.check_call(['pip', 'install', 'camelot-py[cv]']) # Install Camelot with OpenCV support
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import camelot
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import json
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
def extract_tables_from_file(file_path):
|
| 10 |
+
# Extract tables from the PDF
|
| 11 |
+
tables = camelot.read_pdf(file_path, pages='1-end', multiple_tables=True)
|
| 12 |
+
|
| 13 |
+
# Table 1 spans from pages 1 to 3
|
| 14 |
+
table1_dfs = [tables[i].df for i in range(min(3, len(tables)))]
|
| 15 |
+
if table1_dfs:
|
| 16 |
+
table1_df = pd.concat(table1_dfs, ignore_index=True)
|
| 17 |
+
table1_filename = "table1.csv"
|
| 18 |
+
table1_df.to_csv(table1_filename, index=False)
|
| 19 |
+
|
| 20 |
+
# Assume Table 2 is on the last page
|
| 21 |
+
table2_df = tables[-1].df
|
| 22 |
+
table2_filename = "table2.csv"
|
| 23 |
+
table2_df.to_csv(table2_filename, index=False)
|
| 24 |
+
|
| 25 |
+
# Prepare context JSON
|
| 26 |
+
context = {
|
| 27 |
+
'table1': {'description': 'Table 1 extracted from pages 1 to 3', 'csv_path': table1_filename},
|
| 28 |
+
'table2': {'description': 'Table 2 extracted from the last page', 'csv_path': table2_filename},
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
context_filename = "tables_context.json"
|
| 32 |
+
with open(context_filename, 'w') as json_file:
|
| 33 |
+
json.dump(context, json_file, indent=4)
|
| 34 |
+
|
| 35 |
+
return table1_filename, table2_filename, context_filename
|
| 36 |
+
|
| 37 |
+
# Streamlit Web App
|
| 38 |
+
st.title("PDF Table Extractor")
|
| 39 |
+
|
| 40 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
| 41 |
+
if uploaded_file is not None:
|
| 42 |
+
# Save the uploaded file temporarily
|
| 43 |
+
temp_file_path = "temp_uploaded_file.pdf"
|
| 44 |
+
|
| 45 |
+
with open(temp_file_path, "wb") as f:
|
| 46 |
+
f.write(uploaded_file.getbuffer())
|
| 47 |
+
|
| 48 |
+
# Extract tables from the uploaded PDF file
|
| 49 |
+
try:
|
| 50 |
+
table1, table2, context = extract_tables_from_file(temp_file_path)
|
| 51 |
+
os.remove(temp_file_path) # Clean up the temporary file
|
| 52 |
+
|
| 53 |
+
st.success("Extraction complete.")
|
| 54 |
+
|
| 55 |
+
# Provide download links for the output files
|
| 56 |
+
st.download_button(
|
| 57 |
+
label="Download Table 1 CSV",
|
| 58 |
+
data=open(table1, 'rb').read(),
|
| 59 |
+
file_name=table1,
|
| 60 |
+
mime='text/csv'
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
st.download_button(
|
| 64 |
+
label="Download Table 2 CSV",
|
| 65 |
+
data=open(table2, 'rb').read(),
|
| 66 |
+
file_name=table2,
|
| 67 |
+
mime='text/csv'
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
st.download_button(
|
| 71 |
+
label="Download Context JSON",
|
| 72 |
+
data=open(context, 'rb').read(),
|
| 73 |
+
file_name=context,
|
| 74 |
+
mime='application/json'
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
except Exception as e:
|
| 78 |
+
st.error(f"An error occurred: {e}")
|