Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,3 @@
|
|
| 1 |
-
# Enhanced Hospital Queue Management System - Gradio App for Hugging Face
|
| 2 |
-
# Features: Multiple Doctors, Patient Queue, Phone Number Status Check, Appointment Scheduling, Estimated Wait Times
|
| 3 |
-
|
| 4 |
import sqlite3
|
| 5 |
import gradio as gr
|
| 6 |
import pandas as pd
|
|
@@ -31,7 +28,7 @@ cursor.execute('''CREATE TABLE IF NOT EXISTS doctors (
|
|
| 31 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 32 |
name TEXT NOT NULL,
|
| 33 |
specialty TEXT,
|
| 34 |
-
avg_consultation_time INTEGER DEFAULT 15,
|
| 35 |
available BOOLEAN DEFAULT 1
|
| 36 |
)''')
|
| 37 |
|
|
@@ -41,7 +38,7 @@ cursor.execute('''CREATE TABLE IF NOT EXISTS patients (
|
|
| 41 |
phone TEXT NOT NULL,
|
| 42 |
email TEXT,
|
| 43 |
symptoms TEXT,
|
| 44 |
-
priority INTEGER DEFAULT 3,
|
| 45 |
doctor_id INTEGER,
|
| 46 |
queue_number INTEGER,
|
| 47 |
token TEXT UNIQUE,
|
|
@@ -826,141 +823,67 @@ with gr.Blocks(theme=theme, title="Hospital Queue Management System") as demo:
|
|
| 826 |
|
| 827 |
# Admin Panel Tab
|
| 828 |
with gr.Tab("⚙️ Admin Panel"):
|
| 829 |
-
gr.Markdown("###
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 842 |
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
# Set up event handlers
|
| 846 |
-
toggle_btn.click(
|
| 847 |
-
fn=toggle_doctor_availability,
|
| 848 |
-
inputs=toggle_doctor_id,
|
| 849 |
-
outputs=toggle_output
|
| 850 |
-
)
|
| 851 |
-
|
| 852 |
-
refresh_doctors_btn.click(
|
| 853 |
-
fn=get_doctor_availability,
|
| 854 |
-
inputs=[],
|
| 855 |
-
outputs=doctors_table
|
| 856 |
-
)
|
| 857 |
-
|
| 858 |
-
# Initialize doctors table
|
| 859 |
-
demo.load(fn=get_doctor_availability, inputs=[], outputs=doctors_table)
|
| 860 |
-
|
| 861 |
-
# System monitoring section
|
| 862 |
-
gr.Markdown("### System Monitoring")
|
| 863 |
-
|
| 864 |
-
with gr.Row():
|
| 865 |
-
db_size = gr.Textbox(label="Database Size")
|
| 866 |
-
system_status = gr.Textbox(label="System Status")
|
| 867 |
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
size_str = "Database file not found"
|
| 882 |
-
|
| 883 |
-
# Check database connection
|
| 884 |
-
if conn:
|
| 885 |
-
try:
|
| 886 |
-
cursor.execute("SELECT COUNT(*) FROM doctors")
|
| 887 |
-
cursor.fetchone()
|
| 888 |
-
status = "System operational - Database connection OK"
|
| 889 |
-
except:
|
| 890 |
-
status = "Database connection error"
|
| 891 |
-
else:
|
| 892 |
-
status = "Database connection not established"
|
| 893 |
-
|
| 894 |
-
return size_str, status
|
| 895 |
-
|
| 896 |
-
except Exception as e:
|
| 897 |
-
return "Error getting size", f"Error: {str(e)}"
|
| 898 |
-
|
| 899 |
-
check_system_btn = gr.Button("Check System Status")
|
| 900 |
-
check_system_btn.click(
|
| 901 |
-
fn=check_system_status,
|
| 902 |
-
inputs=[],
|
| 903 |
-
outputs=[db_size, system_status]
|
| 904 |
-
)
|
| 905 |
-
|
| 906 |
-
# Backup & Maintenance
|
| 907 |
-
gr.Markdown("### Backup & Maintenance")
|
| 908 |
-
|
| 909 |
-
def backup_database():
|
| 910 |
-
"""Create a backup of the database"""
|
| 911 |
-
try:
|
| 912 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 913 |
-
backup_file = f"hospital_backup_{timestamp}.db"
|
| 914 |
-
|
| 915 |
-
# Create connection to source and destination databases
|
| 916 |
-
source_conn = sqlite3.connect("hospital.db")
|
| 917 |
-
backup_conn = sqlite3.connect(backup_file)
|
| 918 |
-
|
| 919 |
-
# Backup
|
| 920 |
-
source_conn.backup(backup_conn)
|
| 921 |
-
|
| 922 |
-
# Close connections
|
| 923 |
-
source_conn.close()
|
| 924 |
-
backup_conn.close()
|
| 925 |
-
|
| 926 |
-
return f"Backup created successfully: {backup_file}"
|
| 927 |
-
|
| 928 |
-
except Exception as e:
|
| 929 |
-
return f"Backup failed: {str(e)}"
|
| 930 |
-
|
| 931 |
-
backup_btn = gr.Button("Create Database Backup")
|
| 932 |
-
backup_output = gr.Textbox(label="Backup Result")
|
| 933 |
-
|
| 934 |
-
backup_btn.click(fn=backup_database, inputs=[], outputs=backup_output)
|
| 935 |
-
|
| 936 |
-
# Emergency queue reset
|
| 937 |
-
def reset_queue():
|
| 938 |
-
"""Reset the entire patient queue for emergency situations"""
|
| 939 |
-
try:
|
| 940 |
-
cursor.execute("UPDATE patients SET status='Completed' WHERE status IN ('Waiting', 'In Consultation')")
|
| 941 |
-
conn.commit()
|
| 942 |
-
affected = cursor.rowcount
|
| 943 |
-
return f"Queue reset successfully. {affected} patients marked as completed."
|
| 944 |
-
except Exception as e:
|
| 945 |
-
conn.rollback()
|
| 946 |
-
return f"Queue reset failed: {str(e)}"
|
| 947 |
-
|
| 948 |
-
with gr.Row():
|
| 949 |
-
gr.Markdown("### ⚠️ Emergency Queue Reset ⚠️")
|
| 950 |
-
with gr.Row():
|
| 951 |
-
reset_confirm = gr.Checkbox(label="I confirm I want to reset all queues")
|
| 952 |
-
reset_btn = gr.Button("Reset All Queues", variant="stop")
|
| 953 |
-
reset_output = gr.Textbox(label="Reset Result")
|
| 954 |
-
|
| 955 |
-
def safe_reset_queue(confirmed):
|
| 956 |
-
if confirmed:
|
| 957 |
-
return reset_queue()
|
| 958 |
-
else:
|
| 959 |
-
return "Please check the confirmation box to reset queues"
|
| 960 |
-
|
| 961 |
-
reset_btn.click(fn=safe_reset_queue, inputs=reset_confirm, outputs=reset_output)
|
| 962 |
-
|
| 963 |
-
# ---------------------------
|
| 964 |
-
# LAUNCH APP
|
| 965 |
-
# ---------------------------
|
| 966 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
|
|
|
| 28 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 29 |
name TEXT NOT NULL,
|
| 30 |
specialty TEXT,
|
| 31 |
+
avg_consultation_time INTEGER DEFAULT 15, -- in minutes
|
| 32 |
available BOOLEAN DEFAULT 1
|
| 33 |
)''')
|
| 34 |
|
|
|
|
| 38 |
phone TEXT NOT NULL,
|
| 39 |
email TEXT,
|
| 40 |
symptoms TEXT,
|
| 41 |
+
priority INTEGER DEFAULT 3, -- 1=emergency, 2=urgent, 3=normal
|
| 42 |
doctor_id INTEGER,
|
| 43 |
queue_number INTEGER,
|
| 44 |
token TEXT UNIQUE,
|
|
|
|
| 823 |
|
| 824 |
# Admin Panel Tab
|
| 825 |
with gr.Tab("⚙️ Admin Panel"):
|
| 826 |
+
gr.Markdown("### Doctor Availability Management")
|
| 827 |
+
|
| 828 |
+
doctors_table = gr.Dataframe(
|
| 829 |
+
headers=["ID", "Name", "Specialty", "Status"],
|
| 830 |
+
interactive=False,
|
| 831 |
+
label="Doctors"
|
| 832 |
+
)
|
| 833 |
+
|
| 834 |
+
with gr.Row():
|
| 835 |
+
doctor_id = gr.Number(label="Doctor ID", precision=0)
|
| 836 |
+
toggle_btn = gr.Button("Toggle Availability")
|
| 837 |
+
toggle_output = gr.Textbox(label="Result")
|
| 838 |
+
|
| 839 |
+
refresh_doctors_btn = gr.Button("Refresh Doctor List")
|
| 840 |
+
|
| 841 |
+
# Set up event handlers
|
| 842 |
+
toggle_btn.click(
|
| 843 |
+
fn=toggle_doctor_availability,
|
| 844 |
+
inputs=doctor_id,
|
| 845 |
+
outputs=toggle_output
|
| 846 |
+
)
|
| 847 |
+
|
| 848 |
+
refresh_doctors_btn.click(
|
| 849 |
+
fn=get_doctor_availability,
|
| 850 |
+
inputs=[],
|
| 851 |
+
outputs=doctors_table
|
| 852 |
+
)
|
| 853 |
+
|
| 854 |
+
# Initialize doctors table
|
| 855 |
+
demo.load(
|
| 856 |
+
fn=get_doctor_availability,
|
| 857 |
+
inputs=[],
|
| 858 |
+
outputs=doctors_table
|
| 859 |
+
)
|
| 860 |
+
|
| 861 |
+
# System Maintenance Section
|
| 862 |
+
gr.Markdown("### System Maintenance")
|
| 863 |
+
|
| 864 |
+
with gr.Row():
|
| 865 |
+
backup_btn = gr.Button("Backup Database")
|
| 866 |
+
backup_output = gr.Textbox(label="Backup Status")
|
| 867 |
+
|
| 868 |
+
def backup_database():
|
| 869 |
+
"""Create a backup of the database"""
|
| 870 |
+
try:
|
| 871 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 872 |
+
backup_file = f"hospital_backup_{timestamp}.db"
|
| 873 |
|
| 874 |
+
# Create connection to new file
|
| 875 |
+
backup_conn = sqlite3.connect(backup_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 876 |
|
| 877 |
+
# Copy data
|
| 878 |
+
conn.backup(backup_conn)
|
| 879 |
+
backup_conn.close()
|
| 880 |
+
|
| 881 |
+
return f"Backup created successfully: {backup_file}"
|
| 882 |
+
except Exception as e:
|
| 883 |
+
return f"Backup failed: {str(e)}"
|
| 884 |
+
|
| 885 |
+
backup_btn.click(fn=backup_database, inputs=[], outputs=backup_output)
|
| 886 |
+
|
| 887 |
+
# Launch the app
|
| 888 |
+
if __name__ == "__main__":
|
| 889 |
+
demo.launch(share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|