File size: 2,719 Bytes
8648055
 
 
fe62fa6
 
 
f98fda7
8648055
f98fda7
 
 
 
 
 
8648055
 
 
 
fe62fa6
 
 
 
 
8648055
 
 
e8d469e
f98fda7
 
e8d469e
 
f98fda7
e8d469e
 
 
 
 
1f08ee1
 
 
 
 
 
f98fda7
 
e8d469e
f98fda7
e8d469e
763c9f2
1f08ee1
f98fda7
f56fd7f
f98fda7
763c9f2
fe62fa6
f98fda7
763c9f2
 
 
 
 
7ee48ee
4c78eaf
7ee48ee
763c9f2
 
f98fda7
 
 
 
 
 
 
763c9f2
 
 
1f08ee1
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
import gradio as gr
import pandas as pd
import os
import logging

# Set up logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# Ensure the "files" directory exists
FILES_DIR = "files"
os.makedirs(FILES_DIR, exist_ok=True)

# Path to CSV file in the "files" directory of your project
CSV_FILE = os.path.join(FILES_DIR, "patients.csv")

# Initialize the CSV file if it does not exist
if not os.path.exists(CSV_FILE):
    df = pd.DataFrame(columns=["Patient ID", "Patient Name", "Age", "Phone Number"])
    try:
        df.to_csv(CSV_FILE, index=False)
        logging.info(f"Created new CSV file at {CSV_FILE}")
    except Exception as e:
        logging.error(f"Failed to create CSV file: {e}")

# Function to handle registration
def register_patient(name, age, phone):
    try:
        logging.info(f"Starting registration for {name}, Age: {age}, Phone: {phone}")
        
        # Load existing data
        df = pd.read_csv(CSV_FILE)
        logging.debug(f"Loaded existing data: {df}")
        
        # Generate new patient ID
        new_id = len(df) + 1
        
        # Append new patient data
        new_data = pd.DataFrame({
            "Patient ID": [new_id],
            "Patient Name": [name],
            "Age": [age],
            "Phone Number": [phone]
        })
        logging.info(f"New patient data: {new_data}")

        df = pd.concat([df, new_data], ignore_index=True)
        logging.debug(f"Updated DataFrame: {df}")
        
        # Save updated data
        df.to_csv(CSV_FILE, index=False)
        logging.info(f"Data saved to CSV file successfully at {CSV_FILE}.")
        
        return f"Patient {name} with ID {new_id} registered successfully!", CSV_FILE
    except Exception as e:
        logging.error(f"An error occurred while registering patient: {e}")
        return f"An error occurred: {e}", None

# Define Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("## Patient Registration")
    with gr.Row():
        patient_name = gr.Textbox(label="Patient Name", placeholder="Enter patient name")
        age = gr.Number(label="Age", precision=0)
        phone_number = gr.Textbox(label="Phone Number", placeholder="Enter phone number")
    register_button = gr.Button("Register")
    status = gr.Textbox(label="Status", interactive=False)
    download_button = gr.File(label="Download CSV")

    def update_status_and_file(*inputs):
        message, file_path = register_patient(*inputs)
        return message, file_path

    register_button.click(fn=update_status_and_file, inputs=[patient_name, age, phone_number], outputs=[status, download_button])

# Launch the app
if __name__ == "__main__":
    demo.launch()