Spaces:
Sleeping
Sleeping
Download app.py from mhpl/rr_payout_test: direct link, hf CLI and curl.
- Browser
- Download file 33.9 kB
-
https://huggingface.co/spaces/mhpl/rr_payout_test/resolve/main/app.py
- Command line
-
hf download hf://spaces/mhpl/rr_payout_test/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mhpl/rr_payout_test/resolve/main/app.py
33.9 kB
| #---------------------SALTED PASSWORD GENERATOR---------------- | |
| # Use this only when to generate a new salted password (when you want to change credentials) | |
| # Just put your desired password in the first parameter of generate_password_hash function ("str") | |
| # and run this code to get your salted password and then replace this value with your PASSWORD_HASH secret. | |
| ''' | |
| import gradio as gr | |
| from werkzeug.security import generate_password_hash, check_password_hash | |
| # Temporary credentials for preview (Change these!) | |
| USERNAME = "sample" | |
| # Run this ONCE to get your hash | |
| from werkzeug.security import generate_password_hash | |
| PASSWORD_HASH = generate_password_hash("str", method='pbkdf2:sha256:1000000') # Generate fresh hash for production | |
| print(PASSWORD_HASH) | |
| ''' | |
| #======================================================================================== | |
| import gradio as gr | |
| from werkzeug.security import check_password_hash | |
| import time | |
| import os | |
| import threading | |
| # Import processing functions from your modules | |
| from rr_cleaning.mssh import process_mssh | |
| from rr_cleaning.maatri import process_maatri | |
| from rr_cleaning.health_studio import process_health_studio | |
| from variable.ortho_variable import generate_ortho_revenue | |
| from variable.mhs_variable import generate_mhs_doctor_report | |
| from variable.mssh_maatri_variable import generate_mssh_maatri_doctor_report | |
| # ================== AUTHENTICATION SETTINGS ================== | |
| USERNAME = os.environ.get('USERNAME', 'admin') | |
| PASSWORD_HASH = os.environ.get('PASSWORD_HASH', '') | |
| # ================== CUSTOM CSS ================== | |
| app_css = """ | |
| /* Authentication styles */ | |
| .app-blur { | |
| filter: blur(5px) brightness(0.7); | |
| pointer-events: none; | |
| } | |
| .auth-dialog { | |
| position: fixed; | |
| top: 50%; | |
| left: 50%; | |
| transform: translate(-50%, -50%); | |
| background: white; | |
| padding: 2rem; | |
| border-radius: 12px; | |
| z-index: 1000; | |
| width: 450px; | |
| box-shadow: 0 0 30px rgba(0,0,0,0.2); | |
| } | |
| .auth-backdrop { | |
| position: fixed; | |
| top: 0; | |
| left: 0; | |
| right: 0; | |
| bottom: 0; | |
| background: rgba(0,0,0,0.5); | |
| z-index: 999; | |
| } | |
| .dialog-title { | |
| font-size: 1.5em; | |
| margin-bottom: 1.5rem; | |
| text-align: center; | |
| color: #333; | |
| } | |
| /* Main content styles */ | |
| ''' | |
| .main-content { | |
| min-height: calc(100vh - 6rem); /* Full viewport height minus footer */ | |
| margin-bottom: 3rem; /* Space for the fixed footer */ | |
| overflow-y: auto; /* Enable scrolling if content overflows */ | |
| } | |
| ''' | |
| .header-row { | |
| margin-bottom: 1.5rem !important; | |
| } | |
| .controls-container { | |
| margin-bottom: 1rem; | |
| display: flex; | |
| flex-direction: column; | |
| align-items: center; | |
| width: 100%; | |
| } | |
| .controls-column { | |
| width: 90%; | |
| max-width: none !important; | |
| display: flex; | |
| flex-direction: column; | |
| gap: 1rem; | |
| } | |
| .upload-box { | |
| width: 100% !important; | |
| } | |
| /* Make SBU and Service Type dropdowns same size and centered */ | |
| .sbu-box { | |
| width: 100% !important; | |
| } | |
| /* Month and Year dropdowns styling */ | |
| .month-year-row { | |
| gap: 1rem !important; | |
| align-items: flex-end; | |
| justify-content: center; | |
| width: 90% !important; | |
| max-width: none !important; | |
| margin: 0 auto; | |
| } | |
| /* File upload section styling */ | |
| .file-upload-section { | |
| display: flex; | |
| flex-direction: column; | |
| align-items: center; | |
| width: 100%; | |
| } | |
| .file-upload-container { | |
| width: 90% !important; | |
| max-width: none !important; | |
| } | |
| .file-upload-grid { | |
| display: grid; | |
| grid-template-columns: repeat(2, 1fr); | |
| gap: 1rem; | |
| margin: 1rem 0; | |
| } | |
| /* Grouping Generate button and Processing box */ | |
| .generate-section { | |
| margin: 1.5rem 0; | |
| display: flex; | |
| flex-direction: column; | |
| align-items: center; | |
| } | |
| .generate-btn { | |
| width: 100% !important; | |
| max-width: 300px; | |
| margin-bottom: 0.5rem; | |
| } | |
| /* New style for the Stop button */ | |
| .stop-btn { | |
| width: 100% !important; | |
| max-width: 300px; | |
| margin-bottom: 0.5rem; | |
| background-color: red !important; | |
| border: none !important; | |
| } | |
| /* Processing overlay re-positioning */ | |
| .processing-status { | |
| padding: 1.5rem; | |
| background: #f8f9fa; | |
| border-radius: 8px; | |
| text-align: center; | |
| width: auto; | |
| box-shadow: 0 2px 8px rgba(0,0,0,0.1); | |
| } | |
| /* Remove lines between elapsed time paragraphs */ | |
| .processing-status .markdown p { | |
| margin: 0 !important; | |
| border: none !important; | |
| padding: 2px 0 !important; | |
| } | |
| /* Note section near footer */ | |
| .note-section { | |
| margin-top: 20px; | |
| padding: 0.5rem 1rem 0.2rem 1rem; | |
| } | |
| /* Download File styled as button (green) */ | |
| .download-btn { | |
| width: 100% !important; | |
| max-width: 300px; | |
| margin-top: 1rem; | |
| } | |
| .download-btn a { | |
| background-color: #28a745 !important; | |
| color: white !important; | |
| font-weight: 600; | |
| border: none; | |
| padding: 10px; | |
| border-radius: 5px; | |
| text-decoration: none; | |
| display: inline-block; | |
| text-align: center; | |
| width: 100%; | |
| } | |
| /* Blinking text animation for processing status */ | |
| @keyframes blink { | |
| 0% { opacity: 1; } | |
| 50% { opacity: 0; } | |
| 100% { opacity: 1; } | |
| } | |
| .blinking { | |
| animation: blink 1s infinite; | |
| } | |
| /* Footer */ | |
| .copyright-footer { | |
| position: fixed; | |
| bottom: 0; | |
| left: 0; | |
| right: 0; | |
| text-align: center; | |
| padding: 1rem; | |
| background: white; | |
| border-top: 1px solid #e0e0e0; | |
| z-index: 1000; | |
| } | |
| /* ---------- Light Mode Override for Processing Box ---------- */ | |
| @media (prefers-color-scheme: light) { | |
| .processing-status { | |
| /* Use a stark white background and a subtle border for better contrast */ | |
| background: #ffffff !important; | |
| color: #000000 !important; | |
| border: 1px solid #ccc !important; | |
| } | |
| } | |
| /* -------------- Dark Mode Adaptation --------------------- */ | |
| @media (prefers-color-scheme: dark) { | |
| body { | |
| background-color: #121212; | |
| color: #e0e0e0; | |
| } | |
| .auth-dialog { | |
| background-color: #1e1e1e; | |
| color: #e0e0e0; | |
| } | |
| .processing-status { | |
| background: #2e2e2e; | |
| color: #f2f2f2; | |
| } | |
| .copyright-footer { | |
| background-color: #1e1e1e; | |
| border-top: 1px solid #333; | |
| } | |
| } | |
| /*------------ Responsive Adjustments for Mobile Devices ------------------------*/ | |
| @media (max-width: 600px) { | |
| .header-row img { | |
| height: 50px !important; | |
| margin-right: 80px !important; | |
| } | |
| .header-row h1 { | |
| font-size: 1.5rem !important; | |
| } | |
| } | |
| """ | |
| # ================== AUTHENTICATION LOGIC ================== | |
| def authenticate(username, password): | |
| return username == USERNAME and check_password_hash(PASSWORD_HASH, password) | |
| # ---------------- Global Cancellation Flag ---------------- | |
| cancellation_event = None | |
| # ================== DROPDOWN OPTIONS ================== | |
| SBU_OPTIONS = ["MSSH Kankarbagh", "Maatri", "Health Studio"] | |
| REPORT_TYPE_OPTIONS = { | |
| "MSSH Kankarbagh": [ | |
| "Cleaned Revenue Report", | |
| "Ortho Variable Payout", | |
| "Dr. Ajit Kumar", | |
| "Dr. Ashim Parwez", | |
| "Dr. Asif Iqbal", | |
| "Dr. Avinash Kumar", | |
| "Dr. Kunal Krishna", | |
| "Dr. Ravikant Sinha", | |
| "Dr. S A Siddiqui", | |
| "Dr. Sanjay Kumar", | |
| "Dr. Shashidhar Kumar", | |
| "Dr. Vikas Singh" | |
| ], | |
| "Maatri": [ | |
| "Cleaned Revenue Report", | |
| "Dr. Kumar Ashish", | |
| "Dr. Navneet Kumar", | |
| "Dr. Pooja Singh", | |
| "Dr. Rana Shamim", | |
| "Dr. Shilpi Kumari", | |
| "Dr. Shweta Shilpi", | |
| "Dr. Sushil Kumar Pathak" | |
| ], | |
| "Health Studio": [ | |
| "Cleaned Revenue Report", | |
| "Dr. Akanksha Singh", | |
| "Dr. Amit Kumar", | |
| "Dr. Avinash Shekhar Jaiswal", | |
| "Dr. Bharti Anand", | |
| "Dr. Deepti Sinha", | |
| "Dr. Farheen Fatma", | |
| "Dr. Ginny", | |
| "Dr. Govind Kumar", | |
| "Dr. Jyotsana", | |
| "Dr. Khalid Husain", | |
| "Dr. Komal Kumari", | |
| "Dr. Manvi Singh", | |
| "Dr. Raushan Bhatt", | |
| "Dr. Raushan Kumar", | |
| "Dr. Shiksha Narang", | |
| "Dr. Shreeparna Deb", | |
| "Dr. Vanshika Rai" | |
| ] | |
| } | |
| MONTH_OPTIONS = [ | |
| "January", "February", "March", "April", "May", "June", | |
| "July", "August", "September", "October", "November", "December" | |
| ] | |
| YEAR_OPTIONS = ["2025", "2026", "2027", "2028", "2029", "2030"] | |
| # Add month abbreviation mapping | |
| MONTH_ABBREVIATIONS = { | |
| "January": "Jan", | |
| "February": "Feb", | |
| "March": "March", | |
| "April": "April", | |
| "May": "May", | |
| "June": "June", | |
| "July": "July", | |
| "August": "Aug", | |
| "September": "Sep", | |
| "October": "Oct", | |
| "November": "Nov", | |
| "December": "Dec" | |
| } | |
| # ================== INTERFACE TYPE DETERMINATION ================== | |
| def get_interface_type(sbu, report_type): | |
| """Determine the interface type based on SBU and report type""" | |
| if not sbu or not report_type: | |
| return "single_file" | |
| if report_type == "Cleaned Revenue Report": | |
| return f"{sbu}_cleaned" | |
| elif sbu == "MSSH Kankarbagh" and report_type == "Ortho Variable Payout": | |
| return "mssh_ortho" | |
| elif sbu == "MSSH Kankarbagh": | |
| return "mssh_doctors" | |
| elif sbu == "Maatri": | |
| return "maatri_doctors" | |
| elif sbu == "Health Studio": | |
| return "health_studio_doctors" | |
| else: | |
| return "single_file" | |
| # ================== HELPER FUNCTIONS ================== | |
| def clear_all_files(): | |
| """Return None for all file components to clear them""" | |
| return [None] * 10 # 1 single file + 9 multi files | |
| def update_file_upload_interface(sbu, report_type, current_interface_type): | |
| """Update file upload interface based on SBU and report type selection""" | |
| try: | |
| # Determine the new interface type | |
| new_interface_type = get_interface_type(sbu, report_type) | |
| # Check if interface type has changed | |
| interface_changed = (current_interface_type != new_interface_type) | |
| # If interface hasn't changed and it's not the initial load, don't update | |
| if not interface_changed and current_interface_type is not None: | |
| return [gr.update()] * 12 + [new_interface_type] # No updates, just return current state | |
| # Default visibility states | |
| single_file_visible = False | |
| multi_file_visible = False | |
| month_year_visible = True | |
| # File labels for different interfaces | |
| ortho_file_labels = [ | |
| "Upload MSSH Revenue Report", | |
| "Upload IPD Bill Register", | |
| "Upload Doctor Visit with Package Report", | |
| "Upload Doctor Visit without Package Report", | |
| "Upload OT Procedures with Package Report", | |
| "Upload OT Procedures without Package Report", | |
| "Upload GRN Report", | |
| "Upload Item Sale Report" | |
| ] | |
| health_studio_labels = [ | |
| "Upload Health Studio Revenue Report", | |
| "Upload Maatri Revenue Report", | |
| "Upload MSSH Revenue Report", | |
| "Upload MSSH Doctor Visit with Package Report", | |
| "Upload MSSH Doctor Visit without Package Report", | |
| "Upload Maatri Doctor Visit with Package Report", | |
| "Upload Maatri Doctor Visit without Package Report", | |
| "Upload MSSH IPD Bill Register", | |
| "Upload Maatri IPD Bill Register" | |
| ] | |
| mssh_doctor_labels = [ | |
| "Upload MSSH Revenue Report", | |
| "Upload IPD Bill Register", | |
| "Upload Doctor Visit with Package Report", | |
| "Upload Doctor Visit without Package Report" | |
| ] | |
| maatri_doctor_labels = [ | |
| "Upload Maatri Revenue Report", | |
| "Upload IPD Bill Register", | |
| "Upload Doctor Visit with Package Report", | |
| "Upload Doctor Visit without Package Report" | |
| ] | |
| # Determine number of files needed and labels | |
| files_needed = 0 | |
| labels = [] | |
| if new_interface_type.endswith("_cleaned"): | |
| single_file_visible = True | |
| files_needed = 1 | |
| elif new_interface_type == "mssh_ortho": | |
| multi_file_visible = True | |
| files_needed = 8 | |
| labels = ortho_file_labels | |
| elif new_interface_type == "mssh_doctors": | |
| multi_file_visible = True | |
| files_needed = 4 | |
| labels = mssh_doctor_labels | |
| elif new_interface_type == "maatri_doctors": | |
| multi_file_visible = True | |
| files_needed = 4 | |
| labels = maatri_doctor_labels | |
| elif new_interface_type == "health_studio_doctors": | |
| multi_file_visible = True | |
| files_needed = 9 | |
| labels = health_studio_labels | |
| else: | |
| single_file_visible = True | |
| files_needed = 1 | |
| # Create file upload components | |
| file_uploads = [] | |
| for i in range(9): # Maximum 9 files | |
| if i < files_needed and multi_file_visible: | |
| label = labels[i] if i < len(labels) else f"File {i+1}" | |
| # Clear the file value when interface changes | |
| file_uploads.append(gr.File( | |
| label=label, | |
| file_types=[".xls"], | |
| file_count="single", | |
| visible=True, | |
| value=None # Clear the file | |
| )) | |
| else: | |
| file_uploads.append(gr.File( | |
| label=f"File {i+1}", | |
| file_types=[".xls"], | |
| file_count="single", | |
| visible=False, | |
| value=None # Clear the file | |
| )) | |
| # Clear single file upload when interface changes | |
| single_file_update = gr.File( | |
| visible=single_file_visible, | |
| value=None # Clear the file | |
| ) | |
| return ( | |
| single_file_update, # single_file_upload | |
| *file_uploads, # file1 through file9 | |
| gr.Dropdown(visible=month_year_visible), # month_dropdown | |
| gr.Dropdown(visible=month_year_visible), # year_dropdown | |
| new_interface_type # Update the current interface type | |
| ) | |
| except Exception as e: | |
| # Fallback to safe defaults | |
| file_uploads = [] | |
| for i in range(9): | |
| file_uploads.append(gr.File( | |
| label=f"File {i+1}", | |
| file_types=[".xls"], | |
| file_count="single", | |
| visible=False, | |
| value=None | |
| )) | |
| return ( | |
| gr.File(visible=True, value=None), # single_file_upload | |
| *file_uploads, # file1 through file9 | |
| gr.Dropdown(visible=False), # month_dropdown | |
| gr.Dropdown(visible=False), # year_dropdown | |
| "single_file" # Default interface type | |
| ) | |
| def update_report_type_dropdown(sbu): | |
| """Update report type dropdown based on SBU selection""" | |
| if sbu and sbu in REPORT_TYPE_OPTIONS: | |
| new_choices = REPORT_TYPE_OPTIONS[sbu] | |
| new_value = new_choices[0] # Set to the first option of the new SBU | |
| return gr.Dropdown( | |
| choices=new_choices, | |
| value=new_value, | |
| visible=True | |
| ), new_value | |
| return gr.Dropdown(choices=[], visible=False), None | |
| # ================== IMPROVED REPORT GENERATION ================== | |
| def generate_report(single_file, file1, file2, file3, file4, file5, file6, file7, file8, file9, | |
| sbu, report_type, month, year): | |
| global cancellation_event | |
| cancellation_event = threading.Event() # Reset cancellation flag | |
| # ===== CONVERT MONTH AND YEAR TO ABBREVIATED FORMATS ===== | |
| # Convert month to abbreviation (e.g., "February" -> "Feb") | |
| month_abbr = MONTH_ABBREVIATIONS.get(month, month) | |
| # Convert year to short format (e.g., "2025" -> "25") | |
| year_short = year[2:] if year and len(year) == 4 else year | |
| # ======================================================= | |
| # Determine which file(s) to use based on report type | |
| if report_type == "Cleaned Revenue Report": | |
| if not single_file: | |
| raise gr.Error("Please upload an Excel file") | |
| file_to_process = single_file | |
| elif sbu == "MSSH Kankarbagh" and report_type == "Ortho Variable Payout": | |
| # Ensure all 8 files are uploaded | |
| files = [file1, file2, file3, file4, file5, file6, file7, file8] | |
| uploaded_files = [f for f in files if f is not None] | |
| if len(uploaded_files) < 8: | |
| raise gr.Error("Please upload all 8 required files for Ortho Variable Payout") | |
| # ====== ADD THESE NEW CASES FOR MSSH AND MAAtri ====== | |
| elif sbu == "MSSH Kankarbagh" and report_type != "Cleaned Revenue Report": | |
| # Ensure all 4 files are uploaded | |
| files = [file1, file2, file3, file4] | |
| uploaded_files = [f for f in files if f is not None] | |
| if len(uploaded_files) < 4: | |
| raise gr.Error("Please upload all 4 required files for MSSH Doctor Report") | |
| elif sbu == "Maatri" and report_type != "Cleaned Revenue Report": | |
| # Ensure all 4 files are uploaded | |
| files = [file1, file2, file3, file4] | |
| uploaded_files = [f for f in files if f is not None] | |
| if len(uploaded_files) < 4: | |
| raise gr.Error("Please upload all 4 required files for Maatri Doctor Report") | |
| # ====== END OF NEW CASES ====== | |
| elif sbu == "Health Studio" and report_type != "Cleaned Revenue Report": | |
| # Health Studio doctor-specific processing | |
| files = [file1, file2, file3, file4, file5, file6, file7, file8, file9] | |
| uploaded_files = [f for f in files if f is not None] | |
| if len(uploaded_files) < 9: | |
| raise gr.Error("Please upload all 9 required files for Health Studio doctor payout") | |
| else: | |
| # Multi-file upload scenario | |
| files = [file1, file2, file3, file4, file5, file6, file7, file8, file9] | |
| uploaded_files = [f for f in files if f is not None] | |
| # Determine expected number of files | |
| if sbu == "Health Studio" and report_type !="Cleaned Revenue Report": | |
| expected_files = 9 | |
| elif sbu == "MSSH Kankarbagh" and report_type == "Ortho Variable Payout": | |
| expected_files = 8 | |
| else: | |
| expected_files = 4 | |
| if len(uploaded_files) < expected_files: | |
| raise gr.Error(f"Please upload all {expected_files} required files") | |
| file_to_process = uploaded_files[0] # For now, use first file (logic to be implemented later) | |
| # Initial UI update: show processing area, hide download, hide generate, and show stop button. | |
| yield { | |
| processing: gr.update(visible=True), | |
| download_file: gr.update(visible=False), | |
| status: '<span class="blinking">Processing...</span>', | |
| generate_btn: gr.update(visible=False), | |
| stop_btn: gr.update(visible=True) | |
| } | |
| start_time = time.time() | |
| result_container = [] | |
| error_container = [] | |
| processing_done = False | |
| # Processing thread function | |
| def process_task(): | |
| try: | |
| if sbu == "MSSH Kankarbagh" and report_type == "Ortho Variable Payout": | |
| # Call your new function with all 8 files + month/year | |
| res = generate_ortho_revenue( | |
| hospital_revenue_report_path=file1, | |
| inpatient_bill_register_path=file2, | |
| doctor_visit_with_package_path=file3, | |
| doctor_visit_without_package_path=file4, | |
| doctor_ot_with_package_path=file5, | |
| doctor_ot_without_package_path=file6, | |
| grn_details_report_path=file7, | |
| item_sale_report_path=file8, | |
| current_month=month_abbr, | |
| current_year=year_short | |
| ) | |
| elif sbu == "MSSH Kankarbagh" and report_type != "Cleaned Revenue Report": | |
| # MSSH doctor-specific processing | |
| res = generate_mssh_maatri_doctor_report( | |
| doctor_name=report_type, | |
| hospital_revenue_report_path=file1, | |
| inpatient_bill_register_path=file2, | |
| doctor_visit_with_package_path=file3, | |
| doctor_visit_without_package_path=file4, | |
| current_month=month_abbr, | |
| current_year=year_short | |
| ) | |
| elif sbu == "Maatri" and report_type != "Cleaned Revenue Report": | |
| # Maatri doctor-specific processing | |
| res = generate_mssh_maatri_doctor_report( | |
| doctor_name=report_type, | |
| hospital_revenue_report_path=file1, | |
| inpatient_bill_register_path=file2, | |
| doctor_visit_with_package_path=file3, | |
| doctor_visit_without_package_path=file4, | |
| current_month=month_abbr, | |
| current_year=year_short | |
| ) | |
| elif sbu == "Health Studio" and report_type !="Cleaned Revenue Report": | |
| # HEALTH STUDIO DOCTOR PROCESSING | |
| res = generate_mhs_doctor_report( | |
| doctor_name=report_type, | |
| mhs_hospital_revenue_report_path=file1, | |
| maatri_hospital_revenue_report_path=file2, | |
| print_hospital_revenue_report_path=file3, | |
| doctor_visit_report_with_package_path=file4, | |
| doctor_visit_report_without_package_path=file5, | |
| maatri_doctor_visit_report_with_package_path=file6, | |
| maatri_doctor_visit_report_without_package_path=file7, | |
| print_inpatient_bill_register_report_path=file8, | |
| maatri_print_inpatient_bill_register_report_path=file9, | |
| current_month=month_abbr, | |
| current_year=year_short | |
| ) | |
| elif sbu == "MSSH Kankarbagh": | |
| res = process_mssh(file_to_process, month_abbr, year_short) | |
| elif sbu == "Maatri": | |
| res = process_maatri(file_to_process, month_abbr, year_short) | |
| elif sbu == "Health Studio": | |
| res = process_health_studio(file_to_process, month_abbr, year_short) | |
| else: | |
| res = None | |
| result_container.append(res) | |
| except Exception as e: | |
| error_container.append(str(e)) | |
| finally: | |
| nonlocal processing_done | |
| processing_done = True | |
| # Start processing thread | |
| thread = threading.Thread(target=process_task) | |
| thread.start() | |
| # Update timer while processing | |
| while not processing_done: | |
| if cancellation_event.is_set(): | |
| elapsed = time.time() - start_time | |
| mins, secs = divmod(int(elapsed), 60) | |
| yield { | |
| time_elapsed: f"Elapsed Time: {mins:02d}:{secs:02d}", | |
| status: '<span class="blinking">⏹️ Process Stopped</span>', | |
| processing: gr.update(visible=False), | |
| download_file: gr.update(visible=False), | |
| generate_btn: gr.update(visible=True), | |
| stop_btn: gr.update(visible=False) | |
| } | |
| return | |
| elapsed = time.time() - start_time | |
| mins, secs = divmod(int(elapsed), 60) | |
| yield { | |
| time_elapsed: f"Elapsed Time: {mins:02d}:{secs:02d}", | |
| status: '<span class="blinking">Processing...</span>', | |
| generate_btn: gr.update(visible=False), | |
| stop_btn: gr.update(visible=True) | |
| } | |
| time.sleep(0.2) | |
| # Final time calculation | |
| final_elapsed = time.time() - start_time | |
| mins, secs = divmod(int(final_elapsed), 60) | |
| time_str = f"Elapsed Time: {mins:02d}:{secs:02d}" | |
| # UI Update on Success | |
| if result_container and result_container[0]: | |
| yield { | |
| time_elapsed: time_str, | |
| status: '<span class="blinking">✅ Report Generated!</span>', | |
| processing: gr.update(visible=False), | |
| download_file: gr.update(value=result_container[0], visible=True), | |
| generate_btn: gr.update(visible=True), | |
| stop_btn: gr.update(visible=False) | |
| } | |
| else: | |
| raise gr.Error("⚠️ No results generated! Please re-check the inputs provided.") | |
| # Handle errors | |
| if error_container: | |
| yield { | |
| processing: gr.update(visible=False), | |
| download_file: gr.update(visible=False) | |
| } | |
| raise gr.Error(f"⚠️ Processing Error\n{error_container[0]}\n\nPlease verify:") | |
| return | |
| # Handle empty results | |
| if not result_container or not result_container[0]: | |
| yield { | |
| processing: gr.update(visible=False), | |
| download_file: gr.update(visible=False) | |
| } | |
| raise gr.Error("⚠️ No results generated!\nPlease re-check the inputs provided.") | |
| return | |
| # ---------------- Callback for Stopping Processing ---------------- | |
| def stop_processing(): | |
| global cancellation_event | |
| if cancellation_event: | |
| cancellation_event.set() # Signal cancellation | |
| # Immediately update UI to show Generate button and hide Stop button | |
| return { | |
| generate_btn: gr.update(visible=True), | |
| stop_btn: gr.update(visible=False), | |
| processing: gr.update(visible=False), | |
| download_file: gr.update(visible=False), | |
| status: '<span class="blinking">⏹️ Process Stopped</span>' | |
| } | |
| # ================== APP LAYOUT ================== | |
| with gr.Blocks(css=app_css) as demo: | |
| # Session state | |
| auth_state = gr.State(False) | |
| temp_report_type = gr.State("Cleaned Revenue Report") # Temporary state for report type | |
| current_interface_type = gr.State(None) # Track current interface type | |
| # ----- Authentication Overlay ----- | |
| with gr.Column(visible=True) as auth_overlay: | |
| gr.HTML("<div class='auth-backdrop'></div>") | |
| with gr.Column(elem_classes=["auth-dialog"]): | |
| gr.Markdown("## 🔒 Authentication Required") | |
| username = gr.Textbox(label="Username") | |
| password = gr.Textbox(label="Password", type="password") | |
| login_btn = gr.Button("Validate", variant="primary") | |
| error_msg = gr.Markdown("", visible=False) | |
| # ----- Main Content (blurred initially) ----- | |
| with gr.Column(elem_classes=["main-content","app-blur"]) as main_content: | |
| # Header Row with Logo & Title | |
| with gr.Row(elem_classes=["header-row"]): | |
| gr.HTML(""" | |
| <div style="display: flex; align-items: center;"> | |
| <img src="https://www.mediversal.in/logo.png" alt="Hospital Logo" style="height:60px; margin-right:210px;"> | |
| <h1 style="margin:0; color:#FF4500; font-size:2rem;">Revenue Report & Payouts Generator</h1> | |
| </div> | |
| """) | |
| # Controls: SBU Selection and Report Type Selection (one above the other, centered) | |
| with gr.Column(elem_classes=["controls-container"]): | |
| with gr.Column(elem_classes=["controls-column"]): | |
| sbu_dropdown = gr.Dropdown( | |
| label="Select SBU", | |
| choices=SBU_OPTIONS, | |
| value="MSSH Kankarbagh", | |
| elem_classes=["sbu-box"] | |
| ) | |
| report_type_dropdown = gr.Dropdown( | |
| label="Report Type", | |
| choices=REPORT_TYPE_OPTIONS["MSSH Kankarbagh"], | |
| value="Cleaned Revenue Report", | |
| elem_classes=["sbu-box"] | |
| ) | |
| # Month and Year Selection (initially hidden) | |
| with gr.Row(elem_classes=["month-year-row"]): | |
| month_dropdown = gr.Dropdown( | |
| label="Current Month", | |
| choices=MONTH_OPTIONS, | |
| value="July", | |
| visible=True | |
| ) | |
| year_dropdown = gr.Dropdown( | |
| label="Current Year", | |
| choices=YEAR_OPTIONS, | |
| value="2025", | |
| visible=True | |
| ) | |
| # File Upload Section | |
| with gr.Column(elem_classes=["file-upload-section"]): | |
| with gr.Column(elem_classes=["file-upload-container"]): | |
| gr.Markdown("### Upload Required Files:") | |
| # Single file upload (for Cleaned Revenue Report) | |
| single_file_upload = gr.File( | |
| label="Upload Revenue Report", | |
| file_types=[".xls"], | |
| file_count="single", | |
| elem_classes=["upload-box"], | |
| visible=True | |
| ) | |
| # Multiple file uploads (for other report types) | |
| with gr.Column(elem_classes=["file-upload-grid"]): | |
| file1 = gr.File(label="File 1", file_types=[".xls"], file_count="single", visible=False) | |
| file2 = gr.File(label="File 2", file_types=[".xls"], file_count="single", visible=False) | |
| file3 = gr.File(label="File 3", file_types=[".xls"], file_count="single", visible=False) | |
| file4 = gr.File(label="File 4", file_types=[".xls"], file_count="single", visible=False) | |
| file5 = gr.File(label="File 5", file_types=[".xls"], file_count="single", visible=False) | |
| file6 = gr.File(label="File 6", file_types=[".xls"], file_count="single", visible=False) | |
| file7 = gr.File(label="File 7", file_types=[".xls"], file_count="single", visible=False) | |
| file8 = gr.File(label="File 8", file_types=[".xls"], file_count="single", visible=False) | |
| file9 = gr.File(label="File 9", file_types=[".xls"], file_count="single", visible=False) | |
| # Grouping the Generate button and Processing box together | |
| with gr.Column(elem_classes=["generate-section"]): | |
| generate_btn = gr.Button("Generate Report", variant="primary", elem_classes=["generate-btn"]) | |
| # Use the new 'stop-btn' class to color this button red | |
| stop_btn = gr.Button("Stop", variant="secondary", elem_classes=["stop-btn"], visible=False) | |
| processing = gr.Column(visible=False, elem_classes=["processing-status"]) | |
| with processing: | |
| time_elapsed = gr.Markdown("**Elapsed Time:** 00:00") | |
| status = gr.Markdown("<span class='blinking'>Processing...</span>") | |
| # Download File component | |
| download_file = gr.File( | |
| label="Download Report", | |
| visible=False, | |
| elem_classes=["download-btn"] | |
| ) | |
| # Note Section | |
| with gr.Column(elem_classes=["note-section"]): | |
| gr.Markdown("**NOTE:** Upload the report downloaded from HIMS ONLY (without any modification).") | |
| gr.Markdown("In case of any queries, contact us at [ayush.raj@mediversal.in](mailto:ayush.raj@mediversal.in)") | |
| # Footer | |
| gr.Markdown(""" | |
| <div class='copyright-footer'> | |
| Copyright © 2025 Mediversal Healthcare Private Limited. All Rights Reserved. | |
| </div> | |
| """) | |
| # ================== INTERACTIVITY ================== | |
| # Update report type dropdown when SBU changes | |
| sbu_dropdown.change( | |
| update_report_type_dropdown, | |
| inputs=[sbu_dropdown], | |
| outputs=[report_type_dropdown, temp_report_type] | |
| ).then( | |
| lambda sbu, rtype, curr_type: update_file_upload_interface(sbu, rtype, curr_type), | |
| inputs=[sbu_dropdown, temp_report_type, current_interface_type], | |
| outputs=[single_file_upload, file1, file2, file3, file4, file5, file6, file7, file8, file9, | |
| month_dropdown, year_dropdown, current_interface_type] | |
| ) | |
| # Update file upload interface when report type changes | |
| report_type_dropdown.change( | |
| lambda sbu, rtype, curr_type: update_file_upload_interface(sbu, rtype, curr_type), | |
| inputs=[sbu_dropdown, report_type_dropdown, current_interface_type], | |
| outputs=[single_file_upload, file1, file2, file3, file4, file5, file6, file7, file8, file9, | |
| month_dropdown, year_dropdown, current_interface_type] | |
| ) | |
| # Report generation callback | |
| generate_btn.click( | |
| generate_report, | |
| inputs=[single_file_upload, file1, file2, file3, file4, file5, file6, file7, file8, file9, | |
| sbu_dropdown, report_type_dropdown, month_dropdown, year_dropdown], | |
| outputs=[processing, time_elapsed, status, download_file, generate_btn, stop_btn] | |
| ) | |
| # Stop button callback: sets the cancellation flag | |
| stop_btn.click( | |
| stop_processing, | |
| None, | |
| outputs=[generate_btn, stop_btn, processing, download_file, status]) | |
| # Authentication callback | |
| def handle_login(username, password, _): | |
| if authenticate(username, password): | |
| yield [ | |
| gr.update(visible=True), | |
| {"authenticated": False}, | |
| gr.update(elem_classes=["app-blur"]), | |
| gr.update(value="✅ Access Granted", visible=True) | |
| ] | |
| time.sleep(0.5) | |
| yield [ | |
| gr.update(visible=False), | |
| {"authenticated": True}, | |
| gr.update(elem_classes=[]), | |
| gr.update(value="", visible=False) | |
| ] | |
| else: | |
| yield [ | |
| gr.update(visible=True), | |
| {"authenticated": False}, | |
| gr.update(elem_classes=["app-blur"]), | |
| gr.update(value="⛔ Invalid Credentials", visible=True) | |
| ] | |
| login_btn.click( | |
| handle_login, | |
| [username, password, auth_state], | |
| [auth_overlay, auth_state, main_content, error_msg] | |
| ) | |
| username.submit( | |
| handle_login, | |
| [username, password, auth_state], | |
| [auth_overlay, auth_state, main_content, error_msg] | |
| ) | |
| password.submit( | |
| handle_login, | |
| [username, password, auth_state], | |
| [auth_overlay, auth_state, main_content, error_msg] | |
| ) | |
| # Updated Working Fully with fixed Upload Interfaces with working stop button | |
| # ================== LAUNCH APP ================== | |
| if __name__ == "__main__": | |
| demo.launch() |