appoi / app.py
yougandar's picture
Update app.py
d95e4a2 verified
import gradio as gr
import pandas as pd
import csv
import os
# File paths
appointments_file_path = "appointments.csv"
patients_file_path = "patients.csv"
# Function to load CSV files
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()
# Function to get patient details by ID
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 ""
# Function to autofill patient name in the Book Appointment section
def autofill_patient_name(patient_id):
return get_patient_details(patient_id)
# Function to book an appointment
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."
# Function to retrieve appointment details
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
# Authentication Function for Multi-Tenant Login (accepts any username/password)
def authenticate(hospital_name, username, password):
"""Accept any credentials."""
print(f"Authentication bypassed for hospital: {hospital_name}, username: {username}, password: {password}")
return True # Always return True to accept any credentials
# Gradio Interface
def gradio_interface():
with gr.Blocks() as demo:
# Multi-Tenant Login Section
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)
# Placeholder for showing/hiding the booking and viewing sections
app_section = gr.Group(visible=False)
# Function to toggle authentication status
def login_action(hospital_name, username, password):
if authenticate(hospital_name, username, password):
return gr.update(visible=True), "Login successful!" # Show app section on successful login
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]
)
# Appointment Booking and Viewing Sections (conditionally visible)
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)
# Auto-fill patient name based on Patient ID
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 action
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)
# Update patient name and appointment details when Patient ID is entered
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
# Launch the Gradio interface
if __name__ == "__main__":
print("Launching the Gradio app...")
gradio_interface().launch(share=True)