import gradio as gr from api import check_id, check_credit, check_mrz ID_EXAMPLES = [[f"images/id/{i}.jpg"] for i in range(1, 8)] CREDIT_EXAMPLES = [["images/bank/demo1.jpg"], ["images/bank/demo2.png"], ["images/bank/demo3.png"]] MRZ_EXAMPLES = [["images/mrz_barcode/demo1.png"], ["images/mrz_barcode/demo2.png"]] def _format_result_html(result: dict) -> str: if not result or "error" in result: return "" rows = [] for key, value in result.items(): if isinstance(value, dict): inner = "".join( f'{k}' f'{v}' for k, v in value.items() ) rows.append( f'' f'{key}{inner}' ) else: rows.append( f'{key}' f'{value}' ) return f"""
{''.join(rows)}
""" def process_image(image, doc_type: str): if image is None: return ( '
' "Upload or select an example image
", {"error": "No image provided"}, ) check_fn = {"ID": check_id, "Credit Card": check_credit, "MRZ": check_mrz}.get(doc_type, check_id) result = check_fn(image) if "error" in result: return ( f'
{result["error"]}
', result, ) return _format_result_html(result), result def create_interface(): with gr.Blocks( title="MiniAiLive ID Document Reader", theme=gr.themes.Soft(primary_hue="blue", neutral_hue="slate"), css="footer {display: none !important;}", ) as demo: gr.Markdown( """ # 🥇 MiniAiLive ID Document Reader **Advanced AI-Powered ID Document Recognition Technology** For more details, please visit website Upload a document image or select an example below to extract information. """ ) doc_type = gr.Radio( choices=["ID", "Credit Card", "MRZ"], value="ID", label="Document Type", ) with gr.Row(equal_height=False): with gr.Column(scale=1, min_width=400): image_input = gr.Image(label="Upload Image") submit_btn = gr.Button("Read Document", variant="primary", size="lg") with gr.Column(visible=True) as id_col: gr.Examples( examples=ID_EXAMPLES, inputs=image_input, label="ID Card Examples", ) with gr.Column(visible=False) as credit_col: gr.Examples( examples=CREDIT_EXAMPLES, inputs=image_input, label="Credit Card Examples", ) with gr.Column(visible=False) as mrz_col: gr.Examples( examples=MRZ_EXAMPLES, inputs=image_input, label="MRZ / Barcode Examples", ) with gr.Column(scale=1, min_width=400): result_html = gr.HTML(label="Extracted Data") raw_json = gr.JSON(label="Raw Response") doc_type.change( fn=lambda choice: ( gr.update(visible=choice == "ID"), gr.update(visible=choice == "Credit Card"), gr.update(visible=choice == "MRZ"), ), inputs=doc_type, outputs=[id_col, credit_col, mrz_col], ) submit_btn.click( fn=process_image, inputs=[image_input, doc_type], outputs=[result_html, raw_json], ) return demo