Spaces:
Sleeping
Sleeping
| # standard libraries | |
| import os | |
| # third party libraries | |
| import gradio as gr | |
| import validators | |
| from validators import ValidationFailure | |
| import gspread | |
| from google.oauth2.service_account import Credentials | |
| from pydrive.auth import GoogleAuth | |
| from pydrive.drive import GoogleDrive | |
| import etl | |
| from generic import matched_super_generic | |
| cred_filename = 'heliumhealth-a05d595e5991.json' | |
| # cred_file_path = os.path.join('credentials', cred_filename) | |
| # google auth scopes | |
| scopes = ['https://www.googleapis.com/auth/spreadsheets', | |
| 'https://www.googleapis.com/auth/drive'] | |
| # create credentials | |
| credentials = Credentials.from_service_account_file( | |
| cred_filename, scopes=scopes ) | |
| # authorize google spreadsheet | |
| gc = gspread.authorize(credentials) | |
| gauth = GoogleAuth() | |
| drive = GoogleDrive(gauth) | |
| def prescription_quality_check(gs, sheet_type): | |
| """ | |
| - Loads, transforms prescription data | |
| - Performs RX Norm check for prescription data | |
| Args: | |
| gs (Object): authenticated google sheet instance | |
| sheet_type (str): sheet type always: prescription | |
| """ | |
| # load data from sheet | |
| pres_data, super_data = etl.load_data(gs, sheet_type) | |
| # process data for use | |
| pres_data, super_data = etl.preprocess_data(pres_data, super_data, sheet_type) | |
| # run the matching algorithm | |
| pres_data = pres_data.apply(matched_super_generic, | |
| axis=1, | |
| args=[ | |
| super_data, | |
| 'RX Norm [Super Generic]', | |
| 'SUPER_GENERIC', | |
| 'match']) | |
| # post process data | |
| pres_data = pres_data[['Unstructured Name', 'RX Norm [Super Generic]', 'match', 'Number']] | |
| # write output to sheet | |
| etl.output_data(gs, pres_data, sheet_type) | |
| def diagnosis_quality_check(gs, sheet_type): | |
| """ | |
| - Loads, transforms prescription data | |
| - Performs RX Norm check for prescription data | |
| Args: | |
| gs (Object): google-sheet instance | |
| sheet_type (str): sheet type always diagnosis | |
| """ | |
| # load data from sheet | |
| diag_data, super_data = etl.load_data(gs, sheet_type) | |
| # process data for use | |
| diag_data, super_data = etl.preprocess_data(diag_data, super_data, sheet_type) | |
| # run the matching algorithm | |
| diag_data = diag_data.apply(matched_super_generic, | |
| axis=1, | |
| args=[ | |
| super_data, | |
| 'ICD10 Diagnosis', | |
| 'Name', | |
| 'match']) | |
| # post processing | |
| diag_data = diag_data[['Unstructured Name', 'ICD10 Diagnosis', 'match', 'Number']] | |
| # write output to sheet | |
| etl.output_data(gs, diag_data, sheet_type) | |
| def quality_check_main(sheet_type, sheet_url): | |
| """ | |
| main check function | |
| Args: | |
| sheet_type (str): sheet type either: prescription or diagnosis | |
| sheet_url (url): url to data sheet | |
| Raises: | |
| gr.exceptions.Error: Value error for invalid urls | |
| Returns: | |
| message (str): success or failure messages. | |
| """ | |
| result = validators.url(sheet_url) | |
| # open the google sheet for reading | |
| gs = gc.open_by_url(sheet_url) | |
| # Check if the input is valid url | |
| # ToDO: Error message displayed is not explanatory - Fix it | |
| if isinstance(result, ValidationFailure): | |
| raise gr.exceptions.Error(message="Please enter a valid URL") | |
| if sheet_type == 'diagnosis': | |
| diagnosis_quality_check(gs, sheet_type) | |
| elif sheet_type == 'prescription': | |
| prescription_quality_check(gs, sheet_type) | |
| output_text = f"{sheet_type} Q/A successfully. Please check the result sheet for output" | |
| return output_text | |
| with gr.Blocks() as demo: | |
| gr.Markdown( | |
| """ | |
| # Quality Assurance App | |
| ## Instructions | |
| * Add IAM User to sheet you want to test | |
| * Gsheet tabs required for diagnosis: | |
| * Diagnosis, ICD10 Diagnosis, resultd | |
| * Gsheet tabs required for prescription: | |
| * Prescriptions, Super_generic_Prescriptions, result | |
| * Data headers required for diagnosis | |
| * Unstructured Name, ICD10 Diagnosis, Number | |
| * Data headers required for prescription | |
| * Unstructured Name, RX Norm [Super Generic], Number | |
| """ | |
| ) | |
| # inputs | |
| sheet_type = gr.Dropdown(['prescription', 'diagnosis'], label="QA Type") | |
| url = gr.Textbox(label="URL", placeholder="Enter sheet url ...") | |
| # outputs | |
| output = gr.Textbox(label="Output Box") | |
| run_btn = gr.Button("Run") | |
| run_btn.click( | |
| fn=quality_check_main, | |
| inputs=[ | |
| sheet_type, | |
| url | |
| ], | |
| outputs=output, | |
| api_name="quality_check" | |
| ) | |
| demo.launch() | |