|
|
import gradio as gr |
|
|
import pandas as pd |
|
|
import csv |
|
|
import os |
|
|
|
|
|
|
|
|
appointments_file_path = "appointments.csv" |
|
|
patients_file_path = "patients.csv" |
|
|
|
|
|
|
|
|
def load_csv(file_path): |
|
|
"""Load data from a CSV file, return an empty DataFrame if file is not found or empty.""" |
|
|
try: |
|
|
return pd.read_csv(file_path) |
|
|
except (pd.errors.EmptyDataError, FileNotFoundError): |
|
|
return pd.DataFrame() |
|
|
|
|
|
|
|
|
def get_patient_details(patient_id): |
|
|
"""Get patient details by Patient ID.""" |
|
|
with open(patients_file_path, mode='r') as file: |
|
|
reader = csv.DictReader(file) |
|
|
for row in reader: |
|
|
if row["Patient ID"] == str(patient_id): |
|
|
return row["Patient Name"] |
|
|
return "" |
|
|
|
|
|
|
|
|
def autofill_patient_name(patient_id): |
|
|
return get_patient_details(patient_id) |
|
|
|
|
|
|
|
|
def book_appointment_ui(patient_id, doctor_name, speciality, appointment_date, appointment_time): |
|
|
patient_name = get_patient_details(patient_id) |
|
|
if patient_name: |
|
|
with open(appointments_file_path, mode='a', newline='') as file: |
|
|
writer = csv.writer(file) |
|
|
writer.writerow([patient_id, patient_name, speciality, doctor_name, appointment_date, appointment_time]) |
|
|
return f"Appointment for {patient_name} with {doctor_name} in {speciality} on {appointment_date} at {appointment_time} booked successfully!" |
|
|
else: |
|
|
return "Patient ID not found." |
|
|
|
|
|
|
|
|
def get_appointment_details(patient_id): |
|
|
df = load_csv(appointments_file_path) |
|
|
df['Patient ID'] = df['Patient ID'].astype(str) |
|
|
appointments = df[df["Patient ID"] == patient_id] |
|
|
|
|
|
if appointments.empty: |
|
|
return f"No appointment records found for Patient ID: {patient_id}." |
|
|
|
|
|
details = "\n\n".join( |
|
|
f"Patient Name: {row['Patient Name']}\n" |
|
|
f"Doctor Name: {row['Doctor Name']}\n" |
|
|
f"Speciality: {row['Speciality']}\n" |
|
|
f"Appointment Date: {row['Appointment Date']}\n" |
|
|
f"Appointment Time: {row['Appointment Time']}\n" |
|
|
for _, row in appointments.iterrows() |
|
|
) |
|
|
return details |
|
|
|
|
|
|
|
|
def authenticate(hospital_name, username, password): |
|
|
"""Accept any credentials.""" |
|
|
print(f"Authentication bypassed for hospital: {hospital_name}, username: {username}, password: {password}") |
|
|
return True |
|
|
|
|
|
|
|
|
def gradio_interface(): |
|
|
with gr.Blocks() as demo: |
|
|
|
|
|
hospital_name = gr.Dropdown(choices=["Hospital A", "Hospital B"], label="Select Hospital") |
|
|
username = gr.Textbox(label="Username") |
|
|
password = gr.Textbox(label="Password", type="password") |
|
|
login_button = gr.Button("Login") |
|
|
login_output = gr.Textbox(label="Login Status", interactive=False) |
|
|
|
|
|
|
|
|
app_section = gr.Group(visible=False) |
|
|
|
|
|
|
|
|
def login_action(hospital_name, username, password): |
|
|
if authenticate(hospital_name, username, password): |
|
|
return gr.update(visible=True), "Login successful!" |
|
|
else: |
|
|
return gr.update(visible=False), "Invalid credentials or hospital name. Please try again." |
|
|
|
|
|
login_button.click( |
|
|
login_action, |
|
|
inputs=[hospital_name, username, password], |
|
|
outputs=[app_section, login_output] |
|
|
) |
|
|
|
|
|
|
|
|
with app_section: |
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
gr.Markdown("## Book an Appointment") |
|
|
patient_id_input = gr.Number(label="Patient ID", precision=0) |
|
|
patient_name_output = gr.Textbox(label="Patient Name", interactive=False) |
|
|
speciality_input = gr.Dropdown(choices=["ORTHOPEDICS", "CARDIOLOGY", "GENERAL MEDICINES", "NEPHROLOGY", "NEUROLOGY"], label="Speciality") |
|
|
doctor_name_input = gr.Textbox(label="Doctor Name") |
|
|
appointment_date_input = gr.Textbox(label="Appointment Date (YYYY-MM-DD)") |
|
|
appointment_time_input = gr.Textbox(label="Appointment Time (HH:MM)") |
|
|
book_appointment_button = gr.Button("Book Appointment") |
|
|
booking_status = gr.Textbox(label="Booking Status", interactive=False) |
|
|
|
|
|
|
|
|
patient_id_input.change( |
|
|
fn=lambda patient_id: autofill_patient_name(str(int(patient_id))), |
|
|
inputs=patient_id_input, |
|
|
outputs=patient_name_output |
|
|
) |
|
|
|
|
|
|
|
|
book_appointment_button.click( |
|
|
fn=book_appointment_ui, |
|
|
inputs=[patient_id_input, doctor_name_input, speciality_input, appointment_date_input, appointment_time_input], |
|
|
outputs=booking_status |
|
|
) |
|
|
|
|
|
with gr.Column(): |
|
|
gr.Markdown("## View Appointment Details") |
|
|
view_patient_id_input = gr.Number(label="Patient ID", precision=0) |
|
|
view_patient_name_output = gr.Textbox(label="Patient Name", interactive=False) |
|
|
appointment_output = gr.Textbox(label="Appointment Details", interactive=False, lines=10) |
|
|
|
|
|
|
|
|
view_patient_id_input.change( |
|
|
fn=lambda patient_id: ( |
|
|
get_patient_details(str(int(patient_id))), |
|
|
get_appointment_details(str(int(patient_id))) |
|
|
), |
|
|
inputs=view_patient_id_input, |
|
|
outputs=[view_patient_name_output, appointment_output] |
|
|
) |
|
|
|
|
|
return demo |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
print("Launching the Gradio app...") |
|
|
gradio_interface().launch(share=True) |
|
|
|