Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,78 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import cv2
|
| 3 |
-
import pytesseract
|
| 4 |
-
import numpy as np
|
| 5 |
-
import pandas as pd
|
| 6 |
-
from extractor import extract_multi_records
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
import pytesseract
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from extractor import extract_multi_records
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="Smart Multi-Document Data Extractor", layout="wide")
|
| 9 |
+
|
| 10 |
+
st.title("π Smart Multi-Document Data Extractor")
|
| 11 |
+
|
| 12 |
+
# OCR Function (Improved)
|
| 13 |
+
def extract_text(img):
|
| 14 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 15 |
+
gray = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)[1]
|
| 16 |
+
return pytesseract.image_to_string(gray)
|
| 17 |
+
|
| 18 |
+
# File uploader (multiple files supported)
|
| 19 |
+
uploaded_files = st.file_uploader(
|
| 20 |
+
"Upload Images",
|
| 21 |
+
type=["jpg", "png", "jpeg"],
|
| 22 |
+
accept_multiple_files=True
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
if uploaded_files:
|
| 26 |
+
|
| 27 |
+
all_data = []
|
| 28 |
+
|
| 29 |
+
for uploaded_file in uploaded_files:
|
| 30 |
+
try:
|
| 31 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 32 |
+
img = cv2.imdecode(file_bytes, 1)
|
| 33 |
+
|
| 34 |
+
# Check image valid
|
| 35 |
+
if img is None:
|
| 36 |
+
st.error(f"β Could not read image: {uploaded_file.name}")
|
| 37 |
+
continue
|
| 38 |
+
|
| 39 |
+
# Show image
|
| 40 |
+
st.image(img, caption=uploaded_file.name, use_container_width=True)
|
| 41 |
+
|
| 42 |
+
# Extract text
|
| 43 |
+
text = extract_text(img)
|
| 44 |
+
|
| 45 |
+
if not text.strip():
|
| 46 |
+
st.warning(f"β οΈ No text found in {uploaded_file.name}")
|
| 47 |
+
continue
|
| 48 |
+
|
| 49 |
+
# Extract structured data
|
| 50 |
+
records = extract_multi_records(text)
|
| 51 |
+
|
| 52 |
+
if not records:
|
| 53 |
+
st.warning(f"β οΈ No structured data found in {uploaded_file.name}")
|
| 54 |
+
continue
|
| 55 |
+
|
| 56 |
+
all_data.extend(records)
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
st.error(f"β Error processing {uploaded_file.name}: {str(e)}")
|
| 60 |
+
|
| 61 |
+
# Show final data
|
| 62 |
+
if all_data:
|
| 63 |
+
df = pd.DataFrame(all_data)
|
| 64 |
+
|
| 65 |
+
st.success("β
Extraction Complete!")
|
| 66 |
+
st.dataframe(df)
|
| 67 |
+
|
| 68 |
+
# Download Excel
|
| 69 |
+
df.to_excel("output.xlsx", index=False)
|
| 70 |
+
|
| 71 |
+
with open("output.xlsx", "rb") as f:
|
| 72 |
+
st.download_button(
|
| 73 |
+
"π₯ Download Excel",
|
| 74 |
+
f,
|
| 75 |
+
file_name="output.xlsx"
|
| 76 |
+
)
|
| 77 |
+
else:
|
| 78 |
+
st.warning("β οΈ No data extracted from uploaded files.")
|