Jacksonnavigator7 commited on
Commit
e14091f
·
verified ·
1 Parent(s): eea4d05

Create app

Browse files
Files changed (1) hide show
  1. app +117 -0
app ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hospital Queue Management System - Gradio App for Hugging Face
2
+ # Features: Multiple Doctors, Patient Queue, Phone Number Status Check
3
+
4
+ import sqlite3
5
+ import gradio as gr
6
+
7
+ # ---------------------------
8
+ # DATABASE SETUP
9
+ # ---------------------------
10
+ conn = sqlite3.connect("hospital.db", check_same_thread=False)
11
+ cursor = conn.cursor()
12
+ cursor.execute('''CREATE TABLE IF NOT EXISTS doctors (
13
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
14
+ name TEXT NOT NULL
15
+ )''')
16
+ cursor.execute('''CREATE TABLE IF NOT EXISTS patients (
17
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
18
+ name TEXT,
19
+ phone TEXT,
20
+ symptoms TEXT,
21
+ doctor_id INTEGER,
22
+ queue_number INTEGER,
23
+ status TEXT DEFAULT 'Waiting',
24
+ FOREIGN KEY (doctor_id) REFERENCES doctors(id)
25
+ )''')
26
+ conn.commit()
27
+
28
+ # Seed doctors if table is empty
29
+ cursor.execute("SELECT COUNT(*) FROM doctors")
30
+ if cursor.fetchone()[0] == 0:
31
+ doctors = [("Dr. Smith",), ("Dr. Lee",), ("Dr. Patel",)]
32
+ cursor.executemany("INSERT INTO doctors (name) VALUES (?)", doctors)
33
+ conn.commit()
34
+
35
+ # ---------------------------
36
+ # CORE FUNCTIONS
37
+ # ---------------------------
38
+ def register_patient(name, phone, symptoms, doctor_name):
39
+ cursor.execute("SELECT id FROM doctors WHERE name=?", (doctor_name,))
40
+ doctor_id = cursor.fetchone()[0]
41
+ cursor.execute("SELECT MAX(queue_number) FROM patients WHERE doctor_id=?", (doctor_id,))
42
+ last_queue = cursor.fetchone()[0]
43
+ next_queue = 1 if last_queue is None else last_queue + 1
44
+ cursor.execute("INSERT INTO patients (name, phone, symptoms, doctor_id, queue_number) VALUES (?, ?, ?, ?, ?)",
45
+ (name, phone, symptoms, doctor_id, next_queue))
46
+ conn.commit()
47
+ return f"Registered! Your queue number is {next_queue} with {doctor_name}."
48
+
49
+ def check_status(phone):
50
+ cursor.execute("SELECT d.name, p.queue_number, p.status FROM patients p JOIN doctors d ON p.doctor_id = d.id WHERE p.phone=? ORDER BY p.id DESC LIMIT 1", (phone,))
51
+ result = cursor.fetchone()
52
+ if result:
53
+ return f"Doctor: {result[0]}\nQueue #: {result[1]}\nStatus: {result[2]}"
54
+ return "Phone number not found."
55
+
56
+ def get_doctor_queue(doctor_name):
57
+ cursor.execute("SELECT id FROM doctors WHERE name=?", (doctor_name,))
58
+ doctor_id = cursor.fetchone()[0]
59
+ cursor.execute("SELECT id, name, queue_number, status FROM patients WHERE doctor_id=? ORDER BY queue_number", (doctor_id,))
60
+ rows = cursor.fetchall()
61
+ return rows
62
+
63
+ def call_next(doctor_name):
64
+ cursor.execute("SELECT id FROM doctors WHERE name=?", (doctor_name,))
65
+ doctor_id = cursor.fetchone()[0]
66
+ cursor.execute("SELECT id, name FROM patients WHERE doctor_id=? AND status='Waiting' ORDER BY queue_number ASC LIMIT 1", (doctor_id,))
67
+ patient = cursor.fetchone()
68
+ if patient:
69
+ cursor.execute("UPDATE patients SET status='In Consultation' WHERE id=?", (patient[0],))
70
+ conn.commit()
71
+ return f"Calling {patient[1]} for consultation."
72
+ return "No patients waiting."
73
+
74
+ def complete_patient(patient_id):
75
+ cursor.execute("UPDATE patients SET status='Completed' WHERE id=?", (patient_id,))
76
+ conn.commit()
77
+ return f"Patient ID {patient_id} marked as completed."
78
+
79
+ # ---------------------------
80
+ # GRADIO INTERFACE
81
+ # ---------------------------
82
+
83
+ doctor_names = [row[0] for row in cursor.execute("SELECT name FROM doctors").fetchall()]
84
+
85
+ with gr.Blocks() as demo:
86
+ gr.Markdown("# 🏥 Hospital Queue Management System")
87
+
88
+ with gr.Tab("➕ Register Patient"):
89
+ with gr.Row():
90
+ name = gr.Textbox(label="Patient Name")
91
+ phone = gr.Textbox(label="Phone Number")
92
+ symptoms = gr.Textbox(label="Symptoms")
93
+ doctor = gr.Dropdown(choices=doctor_names, label="Select Doctor")
94
+ register_btn = gr.Button("Register")
95
+ register_output = gr.Textbox(label="Response")
96
+ register_btn.click(fn=register_patient, inputs=[name, phone, symptoms, doctor], outputs=register_output)
97
+
98
+ with gr.Tab("🔍 Check My Status"):
99
+ phone_lookup = gr.Textbox(label="Enter Your Phone Number")
100
+ check_btn = gr.Button("Check Status")
101
+ status_output = gr.Textbox(label="Queue Info")
102
+ check_btn.click(fn=check_status, inputs=phone_lookup, outputs=status_output)
103
+
104
+ with gr.Tab("🩺 Doctor Panel"):
105
+ doc_select = gr.Dropdown(choices=doctor_names, label="Select Doctor")
106
+ call_btn = gr.Button("Call Next Patient")
107
+ call_output = gr.Textbox(label="Now Calling")
108
+ queue_table = gr.Dataframe(headers=["ID", "Name", "Queue #", "Status"], interactive=False)
109
+ refresh_btn = gr.Button("Refresh Queue")
110
+
111
+ call_btn.click(fn=call_next, inputs=doc_select, outputs=call_output)
112
+ refresh_btn.click(fn=lambda d: get_doctor_queue(d), inputs=doc_select, outputs=queue_table)
113
+
114
+ # ---------------------------
115
+ # LAUNCH APP
116
+ # ---------------------------
117
+ demo.launch()