Spaces:
Sleeping
Sleeping
# app.py | |
import gradio as gr | |
import json | |
from datetime import datetime | |
from patient_registration import register_patient | |
from test_selection import select_tests, get_tests_by_category | |
from billing import fetch_billing | |
# Load data | |
def load_data(): | |
try: | |
with open("data.json", "r") as file: | |
data = json.load(file) | |
return data.get("patients", {}), data.get("last_sequence", {"year": None, "month": None, "number": 0}) | |
except FileNotFoundError: | |
return {}, {"year": None, "month": None, "number": 0} | |
# Save data | |
def save_data(patients, last_sequence): | |
with open("data.json", "w") as file: | |
json.dump({"patients": patients, "last_sequence": last_sequence}, file) | |
# Generate patient ID | |
def generate_patient_id(phone, last_sequence): | |
today = datetime.now() | |
current_year = today.year | |
current_month = today.month | |
# Check if year or month has changed | |
if last_sequence["year"] != current_year or last_sequence["month"] != current_month: | |
# Reset sequence for new year/month | |
last_sequence["year"] = current_year | |
last_sequence["month"] = current_month | |
last_sequence["number"] = 1 | |
else: | |
# Increment sequence number | |
last_sequence["number"] += 1 | |
# Format patient ID | |
patient_id = f"{current_year}{current_month:02d}{last_sequence['number']:05d}" | |
return patient_id | |
# Patient Registration Tab | |
def registration_interface(name, father_name, age, phone, address, pincode): | |
patients, last_sequence = load_data() | |
# Generate patient ID | |
patient_id = generate_patient_id(phone, last_sequence) | |
# Add patient details to data | |
patients[patient_id] = { | |
"name": name, | |
"father_name": father_name, | |
"age": age, | |
"phone": phone, | |
"address": address, | |
"pincode": pincode, | |
"tests": [], | |
"total_cost": 0 | |
} | |
# Save data and updated sequence | |
save_data(patients, last_sequence) | |
return f"Patient Registered. Patient ID: {patient_id}" | |
# Tests Selection Tab | |
def test_interface(categories): | |
available_tests = get_tests_by_category(categories) | |
return gr.update(choices=available_tests) # Update dropdown with available tests | |
def confirm_tests_interface(patient_id, selected_tests): | |
patients, last_sequence = load_data() # Load both patients and last_sequence | |
response = select_tests(patient_id, selected_tests, patients) # Update patients directly | |
save_data(patients, last_sequence) # Save both patients and last_sequence | |
return response | |
# Billing Tab | |
def billing_interface(patient_id): | |
patients, _ = load_data() # Load patients and ignore last_sequence | |
if patient_id in patients: | |
billing_info = fetch_billing(patient_id, patients) | |
return billing_info | |
else: | |
return "Invalid Patient ID. Please check the ID." | |
# Gradio Interface | |
with gr.Blocks() as app: | |
gr.Markdown("# Sathkrutha LIMS - Patient Management and Billing App") | |
with gr.Tab("Patient Registration"): | |
with gr.Row(): | |
name = gr.Textbox(label="Patient Name", placeholder="Enter patient name", elem_id="name_field", lines=1) | |
father_name = gr.Textbox(label="Father/Husband Name", placeholder="Enter father or husband name", elem_id="father_name_field", lines=1) | |
with gr.Row(): | |
age = gr.Number(label="Age", value=None, minimum=0, maximum=99, elem_id="age_field") | |
phone = gr.Number(label="Phone Number", value=None, elem_id="phone_field") | |
with gr.Row(): | |
address = gr.Textbox(label="Address", placeholder="Enter address", lines=2) | |
pincode = gr.Number(label="Pincode", value=None, elem_id="pincode_field") | |
register_button = gr.Button("Register Patient") | |
registration_output = gr.Textbox(label="Registration Output") | |
register_button.click(registration_interface, [name, father_name, age, phone, address, pincode], registration_output) | |
with gr.Tab("Tests"): | |
patient_id_test = gr.Textbox(label="Patient ID") | |
categories = gr.CheckboxGroup( | |
["Haematology", "Clinical Pathology", "Biochemistry", "Microbiology", "Specific Diseases", "Serology", "Radiology", "Other Diagnostic Tests"], | |
label="Select Test Categories" | |
) | |
available_tests = gr.CheckboxGroup(label="Available Tests") | |
confirm_button = gr.Button("Confirm Tests") | |
test_output = gr.Textbox(label="Test Selection Output") | |
categories.change(test_interface, inputs=categories, outputs=available_tests) # Update available tests based on selected categories | |
confirm_button.click(confirm_tests_interface, [patient_id_test, available_tests], test_output) | |
with gr.Tab("Billing"): | |
patient_id_bill = gr.Textbox(label="Patient ID") | |
fetch_button = gr.Button("Fetch Billing") | |
billing_output = gr.Textbox(label="Billing Information") | |
fetch_button.click(billing_interface, [patient_id_bill], billing_output) | |
# Custom CSS styling | |
app.css = """ | |
body { | |
font-family: Arial, sans-serif; | |
background: url('./background.png') no-repeat center center fixed; | |
background-size: cover; | |
color: #ffffff; | |
} | |
.gradio-container { | |
max-width: 800px; | |
margin: 0 auto; | |
padding: 20px; | |
background: rgba(34, 34, 34, 0.9); /* Slight transparency to see the background */ | |
border-radius: 10px; | |
box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.2); | |
} | |
#name_field, #father_name_field { | |
max-width: 750px; | |
border: 1px solid #ddd; | |
border-radius: 5px; | |
padding: 10px; | |
} | |
#age_field { | |
max-width: 300px; | |
border: 1px solid #ddd; | |
border-radius: 5px; | |
padding: 10px; | |
} | |
#phone_field { | |
max-width: 1100px; | |
border: 1px solid #ddd; | |
border-radius: 5px; | |
padding: 10px; | |
} | |
#pincode_field { | |
max-width: 400px; | |
border: 1px solid #ddd; | |
border-radius: 5px; | |
padding: 10px; | |
} | |
textarea { | |
font-size: 14px; | |
color: #333; | |
} | |
.gr-button { | |
color: #fff; | |
border: none; | |
border-radius: 5px; | |
padding: 10px 20px; | |
cursor: pointer; | |
font-weight: bold; | |
} | |
.gr-button:hover { | |
background-color: #e64a19; | |
} | |
.gr-tab { | |
background-color: rgba(34, 34, 34, 0.8); /* Semi-transparent to see the background */ | |
border-radius: 8px; | |
} | |
h1, h2, h3, h4, h5, h6 { | |
color: #ff9800; | |
} | |
#billing_output { | |
background-color: #444; | |
padding: 15px; | |
border-radius: 5px; | |
color: #fff; | |
} | |
""" | |
app.launch() | |