Spaces:
Running
Running
from pathlib import Path | |
import streamlit as st | |
import os | |
import requests | |
from form16_parser import build_parser, UnsupportedForm16Error | |
badges = """[](https://github.com/INF800/form16-parser) | |
[](https://github.com/mkenney/software-guides/blob/master/STABILITY-BADGES.md#alpha) | |
[](https://github.com/INF800/form16-parser) | |
[]() | |
""" | |
def main(): | |
st.title("Form16 Parser") | |
st.markdown(f"{badges}") | |
st.write("Source code available @ https://github.com/INF800/form16-parser") | |
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf") | |
Path("temp_files").mkdir(exist_ok=True, parents=True) | |
if uploaded_file is None: | |
# If no file is uploaded, load the default PDF file | |
default_pdf_url = "https://assets1.cleartax-cdn.com/cleartax/images/1655725194_sampleform16.pdf" | |
path = Path("temp_files/sampleform16.pdf") | |
# if not path.exists(): | |
default_pdf_response = requests.get(default_pdf_url) | |
with open(str(path), "wb") as f: | |
f.write(default_pdf_response.content) | |
st.write(f"Parsing {default_pdf_url}.") | |
st.write("Try your own form 16 by uploading above.") | |
parser = build_parser() | |
parsed = parser.parse(str(path), return_output=True) | |
st.write("Parsed data (structured):") | |
st.write(parsed) | |
else: | |
# Save the uploaded file to a temporary directory | |
with open(os.path.join("temp_files", uploaded_file.name), "wb") as f: | |
f.write(uploaded_file.getvalue()) | |
filepath = os.path.join("temp_files", uploaded_file.name) | |
parser = build_parser() | |
try: | |
parsed = parser.parse(filepath, return_output=True) | |
st.write("Parsed data (structured):") | |
st.write(parsed) | |
except UnsupportedForm16Error as e: | |
st.write(e) | |
if __name__ == "__main__": | |
main() | |