Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import cv2 | |
| import pytesseract | |
| import numpy as np | |
| import pandas as pd | |
| from extractor import extract_multi_records | |
| st.set_page_config(page_title="Smart Multi-Document Data Extractor", layout="wide") | |
| st.title("π Smart Multi-Document Data Extractor") | |
| # OCR Function (Improved) | |
| def extract_text(img): | |
| gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
| gray = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)[1] | |
| return pytesseract.image_to_string(gray) | |
| # File uploader (multiple files supported) | |
| uploaded_files = st.file_uploader( | |
| "Upload Images", | |
| type=["jpg", "png", "jpeg"], | |
| accept_multiple_files=True | |
| ) | |
| if uploaded_files: | |
| all_data = [] | |
| for uploaded_file in uploaded_files: | |
| try: | |
| file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8) | |
| img = cv2.imdecode(file_bytes, 1) | |
| # Check image valid | |
| if img is None: | |
| st.error(f"β Could not read image: {uploaded_file.name}") | |
| continue | |
| # Show image | |
| st.image(img, caption=uploaded_file.name, use_container_width=True) | |
| # Extract text | |
| text = extract_text(img) | |
| if not text.strip(): | |
| st.warning(f"β οΈ No text found in {uploaded_file.name}") | |
| continue | |
| # Extract structured data | |
| records = extract_multi_records(text) | |
| if not records: | |
| st.warning(f"β οΈ No structured data found in {uploaded_file.name}") | |
| continue | |
| all_data.extend(records) | |
| except Exception as e: | |
| st.error(f"β Error processing {uploaded_file.name}: {str(e)}") | |
| # Show final data | |
| if all_data: | |
| df = pd.DataFrame(all_data) | |
| st.success("β Extraction Complete!") | |
| st.dataframe(df) | |
| # Download Excel | |
| df.to_excel("output.xlsx", index=False) | |
| with open("output.xlsx", "rb") as f: | |
| st.download_button( | |
| "π₯ Download Excel", | |
| f, | |
| file_name="output.xlsx" | |
| ) | |
| else: | |
| st.warning("β οΈ No data extracted from uploaded files.") |