Download app.py from sidhtang/kyc_: direct link, hf CLI and curl.
- Browser
- Download file 9.24 kB
-
https://huggingface.co/spaces/sidhtang/kyc_/resolve/main/app.py
- Command line
-
hf download hf://spaces/sidhtang/kyc_/app.py
-
curl -L -o app.py https://huggingface.co/spaces/sidhtang/kyc_/resolve/main/app.py
9.24 kB
| import gradio as gr | |
| import datetime | |
| import json | |
| from PIL import Image | |
| import numpy as np | |
| class KYCForm: | |
| def __init__(self): | |
| self.form_data = { | |
| 'personal_info': {}, | |
| 'contact_info': {}, | |
| 'document_info': {} | |
| } | |
| self.current_step = 1 | |
| self.states = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY'] | |
| self.document_types = ['Passport', 'Driver License', 'National ID'] | |
| def get_progress(self): | |
| return f"Step {self.current_step} of 4" | |
| def validate_date(self, date_str): | |
| try: | |
| # Try to parse the date string (YYYY-MM-DD format) | |
| datetime.datetime.strptime(date_str, '%Y-%m-%d') | |
| return True | |
| except ValueError: | |
| return False | |
| def update_personal_info(self, given_name, family_name, date_of_birth, tax_id): | |
| if not all([given_name, family_name, date_of_birth, tax_id]): | |
| return { | |
| gr.update(value=self.current_step), # step | |
| gr.update(value="Please fill in all required fields"), # error | |
| None, # next_tab | |
| gr.update(visible=True), # error_visible | |
| self.get_progress() # progress | |
| } | |
| if not self.validate_date(date_of_birth): | |
| return { | |
| gr.update(value=self.current_step), | |
| gr.update(value="Please enter a valid date in YYYY-MM-DD format"), | |
| None, | |
| gr.update(visible=True), | |
| self.get_progress() | |
| } | |
| self.form_data['personal_info'] = { | |
| 'given_name': given_name, | |
| 'family_name': family_name, | |
| 'date_of_birth': date_of_birth, | |
| 'tax_id': tax_id | |
| } | |
| self.current_step = 2 | |
| return { | |
| gr.update(value=self.current_step), # step | |
| gr.update(value=""), # error | |
| gr.update(selected="contact_tab"), # next_tab | |
| gr.update(visible=False), # error_visible | |
| self.get_progress() # progress | |
| } | |
| def update_contact_info(self, street_address, city, state, postal_code): | |
| if not all([street_address, city, state, postal_code]): | |
| return { | |
| gr.update(value=self.current_step), | |
| gr.update(value="Please fill in all required fields"), | |
| None, | |
| gr.update(visible=True), | |
| self.get_progress() | |
| } | |
| self.form_data['contact_info'] = { | |
| 'street_address': street_address, | |
| 'city': city, | |
| 'state': state, | |
| 'postal_code': postal_code | |
| } | |
| self.current_step = 3 | |
| return { | |
| gr.update(value=self.current_step), | |
| gr.update(value=""), | |
| gr.update(selected="document_tab"), | |
| gr.update(visible=False), | |
| self.get_progress() | |
| } | |
| def update_document_info(self, document_type, document_number, document_file): | |
| if not all([document_type, document_number, document_file is not None]): | |
| return { | |
| gr.update(value=self.current_step), | |
| gr.update(value="Please fill in all required fields and upload a document"), | |
| None, | |
| gr.update(visible=True), | |
| self.get_progress() | |
| } | |
| self.form_data['document_info'] = { | |
| 'document_type': document_type, | |
| 'document_number': document_number, | |
| 'document_uploaded': True | |
| } | |
| self.current_step = 4 | |
| return { | |
| gr.update(value=self.current_step), | |
| gr.update(value=""), | |
| gr.update(selected="review_tab"), | |
| gr.update(visible=False), | |
| self.get_progress() | |
| } | |
| def go_back(self, current_tab): | |
| tab_mapping = { | |
| "contact_tab": ("personal_tab", 1), | |
| "document_tab": ("contact_tab", 2), | |
| "review_tab": ("document_tab", 3) | |
| } | |
| if current_tab in tab_mapping: | |
| prev_tab, step = tab_mapping[current_tab] | |
| self.current_step = step | |
| return { | |
| gr.update(value=step), | |
| gr.update(value=""), | |
| gr.update(selected=prev_tab), | |
| gr.update(visible=False), | |
| self.get_progress() | |
| } | |
| return None | |
| def submit_form(self): | |
| return json.dumps(self.form_data, indent=2) | |
| def create_interface(): | |
| kyc = KYCForm() | |
| with gr.Blocks(css=""" | |
| .gradio-container { max-width: 800px; margin: auto; } | |
| .error-message { color: red; } | |
| .helper-text { font-size: 0.8em; color: #666; } | |
| """) as interface: | |
| gr.Markdown("# KYC Form Application") | |
| # Hidden state trackers | |
| step = gr.Number(value=1, visible=False) | |
| error = gr.Textbox(visible=False) | |
| error_visible = gr.Checkbox(visible=False, value=False) | |
| # Progress indicator | |
| progress = gr.Markdown(value=kyc.get_progress()) | |
| with gr.Row(): | |
| error_box = gr.Markdown(visible=False, elem_classes=["error-message"]) | |
| with gr.Tabs() as tabs: | |
| # Personal Information Tab | |
| with gr.Tab("Personal Information", id="personal_tab"): | |
| given_name = gr.Textbox(label="Given Name*", placeholder="Enter your given name") | |
| family_name = gr.Textbox(label="Family Name*", placeholder="Enter your family name") | |
| date_of_birth = gr.Textbox( | |
| label="Date of Birth* (YYYY-MM-DD)", | |
| placeholder="e.g., 1990-01-31" | |
| ) | |
| gr.Markdown("*Please enter date in YYYY-MM-DD format", elem_classes=["helper-text"]) | |
| tax_id = gr.Textbox(label="Tax ID (SSN)*", placeholder="Enter your SSN") | |
| personal_next = gr.Button("Next") | |
| # Contact Information Tab | |
| with gr.Tab("Contact Information", id="contact_tab"): | |
| street_address = gr.Textbox(label="Street Address*", placeholder="Enter your street address") | |
| city = gr.Textbox(label="City*", placeholder="Enter your city") | |
| state = gr.Dropdown(label="State*", choices=kyc.states) | |
| postal_code = gr.Textbox(label="Postal Code*", placeholder="Enter your postal code") | |
| with gr.Row(): | |
| contact_back = gr.Button("Back") | |
| contact_next = gr.Button("Next") | |
| # Document Information Tab | |
| with gr.Tab("Document Verification", id="document_tab"): | |
| document_type = gr.Dropdown(label="Document Type*", choices=kyc.document_types) | |
| document_number = gr.Textbox(label="Document Number*", placeholder="Enter document number") | |
| document_file = gr.File(label="Upload Document*", file_types=["image", "pdf"]) | |
| with gr.Row(): | |
| document_back = gr.Button("Back") | |
| document_next = gr.Button("Next") | |
| # Review Tab | |
| with gr.Tab("Review", id="review_tab"): | |
| review_text = gr.JSON(label="Review your information") | |
| with gr.Row(): | |
| review_back = gr.Button("Back") | |
| submit = gr.Button("Submit", variant="primary") | |
| # Event handlers | |
| personal_next.click( | |
| kyc.update_personal_info, | |
| inputs=[given_name, family_name, date_of_birth, tax_id], | |
| outputs=[step, error, tabs, error_visible, progress] | |
| ).then( | |
| lambda x: x, | |
| inputs=[error], | |
| outputs=[error_box] | |
| ) | |
| contact_next.click( | |
| kyc.update_contact_info, | |
| inputs=[street_address, city, state, postal_code], | |
| outputs=[step, error, tabs, error_visible, progress] | |
| ).then( | |
| lambda x: x, | |
| inputs=[error], | |
| outputs=[error_box] | |
| ) | |
| document_next.click( | |
| kyc.update_document_info, | |
| inputs=[document_type, document_number, document_file], | |
| outputs=[step, error, tabs, error_visible, progress] | |
| ).then( | |
| lambda x: x, | |
| inputs=[error], | |
| outputs=[error_box] | |
| ) | |
| contact_back.click( | |
| lambda: kyc.go_back("contact_tab"), | |
| outputs=[step, error, tabs, error_visible, progress] | |
| ) | |
| document_back.click( | |
| lambda: kyc.go_back("document_tab"), | |
| outputs=[step, error, tabs, error_visible, progress] | |
| ) | |
| review_back.click( | |
| lambda: kyc.go_back("review_tab"), | |
| outputs=[step, error, tabs, error_visible, progress] | |
| ) | |
| submit.click( | |
| kyc.submit_form, | |
| outputs=review_text | |
| ) | |
| return interface | |
| if __name__ == "__main__": | |
| interface = create_interface() | |
| interface.launch() |