Ai / app.py
VIATEUR-AI's picture
Update app.py
9561eba verified
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()