| import gradio as gr |
| import csv |
| import os |
|
|
| |
| patient_file_path = "patients.csv" |
|
|
| def initialize_csv(): |
| """Initialize the CSV file if it doesn't exist.""" |
| if not os.path.exists(patient_file_path): |
| with open(patient_file_path, mode='w', newline='') as file: |
| writer = csv.writer(file) |
| writer.writerow(["Patient ID", "Name", "Age", "Age Unit", "Gender", "Address", "Contact Number"]) |
|
|
| def register_patient(name, age, age_unit, gender, contact): |
| """Register a new patient and return the registration status.""" |
| address = "Hyderabad" |
| initialize_csv() |
| |
| |
| with open(patient_file_path, mode='r') as file: |
| reader = csv.reader(file) |
| rows = list(reader) |
| last_id = int(rows[-1][0]) if len(rows) > 1 else 0 |
| |
| new_id = last_id + 1 |
| |
| |
| with open(patient_file_path, mode='a', newline='') as file: |
| writer = csv.writer(file) |
| writer.writerow([new_id, name, age, age_unit, gender, address, contact]) |
| |
| return f"Patient ID: {new_id}, Name: {name} registered successfully." |
|
|
| def create_gradio_interface(): |
| """Create and return the Gradio interface for patient registration.""" |
| initialize_csv() |
| |
| with gr.Blocks() as demo: |
| gr.Markdown("## Register Patient") |
| |
| with gr.Row(): |
| with gr.Column(): |
| |
| name = gr.Textbox(label="Patient Name", placeholder="Enter patient name") |
| |
| |
| age = gr.Number(label="Age", value=0) |
| age_unit = gr.Radio(["Years", "Months", "Days"], label="Age Unit", value="Years") |
| |
| contact_number = gr.Textbox(label="Contact Number", placeholder="Enter contact number") |
| |
| with gr.Column(): |
| |
| gender = gr.Radio(["Male", "Female", "Transgender"], label="Gender") |
|
|
| |
| result = gr.Textbox(label="Result", interactive=False) |
| |
| |
| register_button = gr.Button("Register") |
| |
| |
| register_button.click( |
| fn=register_patient, |
| inputs=[name, age, age_unit, gender, contact_number], |
| outputs=result |
| ) |
| |
| return demo |
|
|
| if __name__ == "__main__": |
| create_gradio_interface().launch() |
|
|