CoolFace
Apppublic

Surely99/encrypted_credit_scoring

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
app.py352 linesDownload Raw Back to root
1"""A gradio app for credit card approval prediction using FHE."""2 3import subprocess4import time5import gradio as gr6 7from settings import (8    REPO_DIR,9    ACCOUNT_MIN_MAX,10    CHILDREN_MIN_MAX,11    INCOME_MIN_MAX,12    AGE_MIN_MAX,13    FAMILY_MIN_MAX,14    INCOME_TYPES,15    OCCUPATION_TYPES,16    HOUSING_TYPES,17    EDUCATION_TYPES,18    FAMILY_STATUS,19    YEARS_EMPLOYED_BINS,20    INCOME_VALUE,21    AGE_VALUE,22)23from backend import (24    keygen_send,25    pre_process_encrypt_send_applicant,26    pre_process_encrypt_send_bank,27    pre_process_encrypt_send_credit_bureau,28    run_fhe,29    get_output_and_decrypt,30    explain_encrypt_run_decrypt,31)32 33 34subprocess.Popen(["uvicorn", "server:app"], cwd=REPO_DIR)35time.sleep(3)36 37 38demo = gr.Blocks()39 40 41print("Starting the demo...")42with demo:43 44    gr.Markdown(45        """46            <p align="center">47                <img width=200 src="https://huggingface.co/spaces/Surely99/encrypted_credit_scoring/resolve/main/images/cropped-logo-50.png">48            </p>49        """50    )51    gr.Markdown(52        """53        <h1 align="center">使用全同态加密的信用卡审批预测</h1>        54        """55    )56 57    with gr.Accordion("什么是信用卡的审批评估分?", open=False):58        gr.Markdown(59            """60            这是一个复杂的过程,涉及多个实体:申请人、银行、信用中心和信用评分机构。61            当您申请信用卡时,您会向银行提供个人和财务信息。这可能包括您的收入、就业状况和现有债务。银行使用这些信息来评估您的信用度。62            为此,他们经常求助于信用中心和信用评分机构。63            - 信用中心收集并保存有关消费者信用历史的数据。这些数据包括您过去和当前的债务以及您的信用历史长度。64            - 信用评分机构使用算法来分析来自信用中心的数据并生成信用评分。 该分数是您信用度的数字表示。 65            - 银行使用您的信用评分以及您提供的信息来对您的信用卡申请做出决定。更高的信用评分通常会增加您获得批准的机会,并可能带来更好的条款(例如更低的利率)。            66            """67        )68 69    with gr.Accordion("为什么在此过程中添加新的隐私计算处理层至关重要?", open=False):70        gr.Markdown(71            """72            因为所涉及的数据高度敏感。它包括个人信息,如您的身份证号码号码、收入和信用记录。73            不同实体之间有大量数据共享。您的信息不仅掌握在银行手中,还掌握在信用中心和评分机构手中。有权访问您数据的实体越多,数据泄露的风险就越大。这可能导致身份盗窃和金融欺诈。还有数据准确性的问题。74            信用报告中的错误可能导致不公平的低信用评分,影响您获得信贷的能力。            75            """76        )77 78    with gr.Accordion(79        "为什么全同态加密(FHE)是提升信用评分能力的解决方案?", 80        open=False,81    ):82        gr.Markdown(83            """84            全同态加密 (FHE) 被视为增强涉及申请人、银行、信用中心和信用评分机构等多方的信用评分流程的隐私和准确性的理想解决方案。85            它允许对数据进行加密和处理,而无需解密。这意味着可以共享和分析敏感数据,而无需将实际信息暴露给任何一方或处理它的服务器。86            在信用评分的背景下,这将使对个人信誉的评估更加彻底和准确。可以组合和分析来自各种来源的数据以做出更明智的决策,但每一方的数据仍保持机密。87            因此,数据泄露或泄露的风险被大大降低,解决了主要的隐私问题。            88 89            总而言之,FHE 提供了一种做出更准确的信贷资格决策的方法,同时保持严格的数据隐私,为数据实用性和机密性之间的微妙平衡提供了一个复杂的解决方案。            90            """91        )92 93    gr.Markdown(94        """95        <p align="center">96            <img src="https://fheprc.oss-cn-hangzhou.aliyuncs.com/credit.png"97        </p>98        """99    )100 101    gr.Markdown("## 步骤1:创建密钥。")102    gr.Markdown("<hr />")103    gr.Markdown("<span style='color:grey'>申请人、银行和信贷机构设置</span>")104    gr.Markdown(105        """106        - 私钥由合作计算信用评分的实体共同生成。它用于加密和解密数据,绝不会与任何其他方共享。107        - 计算密钥(evaluation key)是服务器处理加密数据所需的公钥。因此,它也被传输到服务器进行进一步处理。108        """109    )110    keygen_button = gr.Button("生成密钥并将计算密钥发送到服务器。")111    evaluation_key = gr.Textbox(112        label="计算密钥:", max_lines=2, interactive=False113    )114    client_id = gr.Textbox(label="", max_lines=2, interactive=False, visible=False)115    116    # Button generate the keys117    keygen_button.click(118        keygen_send,119        outputs=[client_id, evaluation_key, keygen_button],120    )121 122    gr.Markdown("## 步骤2:填写申请信息。")123    gr.Markdown("<hr />")124    gr.Markdown("<span style='color:grey'>申请人、银行和信贷机构设置</span>")125    gr.Markdown(126        """127        选择与您要评估的个人资料相对应的信息。此模型使用了三种信息来源:128        - 申请人的个人信息,以评估其信用卡资格;129        - 申请人的银行账户历史记录,其中提供了与该决定相关的任何类型的申请人银行信息(此处,我们考虑开户时间);130        - 以及信用中心信息,这些信息代表了任何其他信息(这里指就业历史),这些信息可以提供与决策相关的额外见解。131        132        在运行FHE推理之前,请务必在更新后加密并发送值(通过右侧的按钮)。133        """134    )135 136    with gr.Row():137        with gr.Column():138            gr.Markdown("### 步骤2.1 - 申请人信息 🧑‍💼")139            bool_inputs = gr.CheckboxGroup(140                ["汽车", "房产", "手机"], 141                label="您目前持有或拥有以下哪些资产?"142            )143            num_children = gr.Slider(144                **CHILDREN_MIN_MAX, 145                step=1, 146                label="孩子数量", 147                info="您有几个小孩子?"148            )149            household_size = gr.Slider(150                **FAMILY_MIN_MAX, 151                step=1, 152                label="家庭规模", 153                info="您家里有几口人?"154            )155            total_income = gr.Slider(156                **INCOME_MIN_MAX,157                value=INCOME_VALUE,158                label="收入", 159                info="您的年总收入是多少?"160            )161            age = gr.Slider(162                **AGE_MIN_MAX,163                value=AGE_VALUE, 164                step=1, 165                label="年龄", 166                info="今年多大了?"167            )168 169        with gr.Column():170            income_type = gr.Dropdown(171                choices=INCOME_TYPES, 172                value=INCOME_TYPES[0], 173                label="收入类型", 174                info="您的主要收入来源是什么?"175            )176            education_type = gr.Dropdown(177                choices=EDUCATION_TYPES, 178                value=EDUCATION_TYPES[0], 179                label="教育程度", 180                info="您的教育背景是什么?"181            )182            family_status = gr.Dropdown(183                choices=FAMILY_STATUS, 184                value=FAMILY_STATUS[0], 185                label="家庭", 186                info="你的家庭状况如何?"187            )188            occupation_type = gr.Dropdown(189                choices=OCCUPATION_TYPES, 190                value=OCCUPATION_TYPES[0], 191                label="职业", 192                info="你的主要职业是什么?"193            )194            housing_type = gr.Dropdown(195                choices=HOUSING_TYPES, 196                value=HOUSING_TYPES[0], 197                label="住房", 198                info="你住在什么类型的房子?"199            )200 201    with gr.Row():202        with gr.Column(scale=2):203            encrypt_button_applicant = gr.Button("加密输入并发送到服务器。")204            205            encrypted_input_applicant = gr.Textbox(206                label="加密后的输入:", max_lines=2, interactive=False207            )208 209    gr.Markdown("<hr />")210    with gr.Column():211        gr.Markdown("### 步骤2.2-银行信息 🏦")212        account_age = gr.Slider(213            **ACCOUNT_MIN_MAX, 214            step=1, 215            label="账户年龄(月)", 216            info="此人拥有该银行账户多长时间了(以月为单位)?"217        )218 219    with gr.Row():220        with gr.Column(scale=2):221            encrypt_button_bank = gr.Button("加密输入并发送到服务器。")222 223            encrypted_input_bank = gr.Textbox(224                label="加密后的输入:", max_lines=2, interactive=False225            )226 227    gr.Markdown("<hr />")228    with gr.Column():229        gr.Markdown("### 步骤2.3 - 信用中心信息 🏢")230        employed = gr.Radio(["是", "否"], label="是否在职?", value="是")231        years_employed = gr.Dropdown(232            choices=YEARS_EMPLOYED_BINS, 233            value=YEARS_EMPLOYED_BINS[0], 234            label="工作年限", 235            info="该员工已工作多长时间(年数)?"236        )237 238    with gr.Row():239        with gr.Column(scale=2):240            encrypt_button_credit_bureau = gr.Button("加密输入并发送到服务器。")241 242            encrypted_input_credit_bureau = gr.Textbox(243                label="加密后的输入:", max_lines=2, interactive=False244            )245 246    # Button to pre-process, generate the key, encrypt and send the applicant inputs from the client 247    # side to the server248    encrypt_button_applicant.click(249        pre_process_encrypt_send_applicant,250        inputs=[client_id, bool_inputs, num_children, household_size, total_income, age, \251                income_type, education_type, family_status, occupation_type, housing_type],252        outputs=[encrypted_input_applicant, encrypt_button_applicant],253    )254 255    # Button to pre-process, generate the key, encrypt and send the bank inputs from the client 256    # side to the server257    encrypt_button_bank.click(258        pre_process_encrypt_send_bank,259        inputs=[client_id, account_age],260        outputs=[encrypted_input_bank, encrypt_button_bank],261    )262 263    # Button to pre-process, generate the key, encrypt and send the credit bureau inputs from the 264    # client side to the server    265    encrypt_button_credit_bureau.click(266        pre_process_encrypt_send_credit_bureau,267        inputs=[client_id, years_employed, employed],268        outputs=[encrypted_input_credit_bureau, encrypt_button_credit_bureau],269    )270 271    gr.Markdown("## 步骤 3:运行FHE计算。")272    gr.Markdown("<hr />")273    gr.Markdown("<span style='color:grey'>服务端</span>")274    gr.Markdown(275        """276        一旦服务器收到加密的输入,它就可以计算预测,而无需解密任何值。277 278        此服务器采用已在合成数据集上训练过的[决策树](https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html)分类器模型。279        """280    )281 282    execute_fhe_button = gr.Button("运行FHE计算。")283    fhe_execution_time = gr.Textbox(284        label="FHE总执行时间(秒):", max_lines=1, interactive=False285    )286 287    # Button to send the encodings to the server using post method288    execute_fhe_button.click(run_fhe, inputs=[client_id], outputs=[fhe_execution_time, execute_fhe_button])289 290    gr.Markdown("## 步骤4:接收来自服务器的加密输出并解密。")291    gr.Markdown("<hr />")292    gr.Markdown("<span style='color:grey'>申请人、银行和信贷机构解密</span>")293    gr.Markdown(294        """295        一旦服务器完成推理,加密输出将返回给申请人。296 297        提供信息来计算信用评分的三个实体是唯一可以解密结果的实体。他们参与解密协议,该协议允许只有当所有三方都解密其份额的结果时才能解密完整结果。298        """299    )300    gr.Markdown(301        """302        下面显示的第一个值是实际加密输出的缩短字节表示。303        然后,申请人可以使用其私钥解密该值。        304        """305    )306 307    get_output_button = gr.Button("接收来自服务器的加密输出。")308    encrypted_output_representation = gr.Textbox(309        label="加密输出表示: ", max_lines=2, interactive=False310    )311    prediction_output = gr.Textbox(312        label="预测", max_lines=1, interactive=False313    )314 315    # Button to send the encodings to the server using post method316    get_output_button.click(317        get_output_and_decrypt, 318        inputs=[client_id], 319        outputs=[prediction_output, encrypted_output_representation, get_output_button],320    )321 322    gr.Markdown("## 第5步:解释预测(仅当您的信用卡可能被拒绝时)。")323    gr.Markdown("<hr />")324    gr.Markdown(325        """326        如果信用卡申请可能被拒绝,申请人可以询问最有可能需要多少年的工作经验才能增加获得信用卡批准的机会。327 328        为简单起见,上述所有步骤都合并为一个按钮。因此,以下按钮会加密来自所有三方的相同输入(工作年限除外,工作年限会有所不同),在 FHE 中运行新的预测并解密输出。329 330        如果以下状态表明要尝试新的“工作年限”输入,则只需更新步骤2中的值并直接再次运行步骤6。        331        """332    )333    explain_button = gr.Button(334        "加密输入,在FHE中计算并解密输出。"335    )336    explain_prediction = gr.Textbox(337        label="需要额外的工作年限。", interactive=False338    )339 340    # Button to explain the prediction341    explain_button.click(342        explain_encrypt_run_decrypt,343        inputs=[client_id, prediction_output, years_employed, employed],344        outputs=[explain_prediction, explain_button],345    )346 347    gr.Markdown(348        "本应用是一个隐私保护机器学习的示例,仅供参考。"349    )350 351demo.launch(share=False)352