File size: 3,230 Bytes
fb5d899
9561eba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
832fb86
 
9561eba
 
 
 
 
832fb86
9561eba
 
 
 
 
832fb86
 
9561eba
 
 
 
 
832fb86
9561eba
 
832fb86
9561eba
 
 
 
 
832fb86
9561eba
 
 
832fb86
9561eba
 
 
832fb86
9561eba
832fb86
9561eba
 
832fb86
9561eba
 
 
832fb86
9561eba
832fb86
9561eba
832fb86
9561eba
832fb86
9561eba
832fb86
9561eba
 
 
 
 
 
 
832fb86
9561eba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
832fb86
9561eba
 
 
 
 
 
832fb86
9561eba
 
 
832fb86
9561eba
832fb86
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import gradio as gr
import sqlite3
from fpdf import FPDF
import os

# ---------------- DATABASE ----------------
conn = sqlite3.connect("hospital.db", check_same_thread=False)
c = conn.cursor()

c.execute("""
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    email TEXT,
    password TEXT,
    role TEXT
)
""")

c.execute("""
CREATE TABLE IF NOT EXISTS patients (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    age INTEGER,
    gender TEXT,
    phone TEXT
)
""")

c.execute("""
CREATE TABLE IF NOT EXISTS appointments (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    patient TEXT,
    doctor TEXT,
    date TEXT
)
""")

conn.commit()

# ---------------- FUNCTIONS ----------------

def register(name, email, password, role):
    c.execute("INSERT INTO users(name,email,password,role) VALUES (?,?,?,?)",
              (name, email, password, role))
    conn.commit()
    return "User registered successfully"

def login(email, password):
    c.execute("SELECT * FROM users WHERE email=? AND password=?", (email, password))
    user = c.fetchone()
    if user:
        return f"Welcome {user[1]} ({user[4]})"
    return "Invalid login"

def add_patient(name, age, gender, phone):
    c.execute("INSERT INTO patients(name,age,gender,phone) VALUES (?,?,?,?)",
              (name, age, gender, phone))
    conn.commit()
    return "Patient added"

def view_patients():
    c.execute("SELECT * FROM patients")
    return c.fetchall()

def book_appointment(patient, doctor, date):
    c.execute("INSERT INTO appointments(patient,doctor,date) VALUES (?,?,?)",
              (patient, doctor, date))
    conn.commit()
    return "Appointment booked"

def generate_pdf():
    c.execute("SELECT * FROM patients")
    data = c.fetchall()

    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=12)

    pdf.cell(200, 10, "Hospital Patients Report", ln=True)

    for row in data:
        pdf.cell(200, 10, str(row), ln=True)

    file_path = "reports/patients.pdf"
    os.makedirs("reports", exist_ok=True)
    pdf.output(file_path)

    return file_path

# ---------------- UI ----------------

with gr.Blocks(title="PRO Hospital System") as app:

    gr.Markdown("# 🏥 PRO Hospital Management System")

    with gr.Tab("Register"):
        register_btn = gr.Button("Register User")
        r = gr.Interface(
            fn=register,
            inputs=["text","text","text","text"],
            outputs="text"
        )

    with gr.Tab("Login"):
        login_box = gr.Interface(
            fn=login,
            inputs=["text","text"],
            outputs="text"
        )

    with gr.Tab("Patients"):
        patient_box = gr.Interface(
            fn=add_patient,
            inputs=["text","number","text","text"],
            outputs="text"
        )

        gr.Dataframe(view_patients())

    with gr.Tab("Appointments"):
        appointment_box = gr.Interface(
            fn=book_appointment,
            inputs=["text","text","text"],
            outputs="text"
        )

    with gr.Tab("Reports"):
        pdf_btn = gr.Button("Generate PDF Report")
        pdf_output = gr.File()

        pdf_btn.click(generate_pdf, outputs=pdf_output)

app.launch()