Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from simple_salesforce import Salesforce | |
| from datetime import datetime | |
| import requests | |
| from apscheduler.schedulers.background import BackgroundScheduler | |
| import os | |
| from dotenv import load_dotenv | |
| import hashlib | |
| load_dotenv() | |
| # Salesforce Connection | |
| sf = Salesforce( | |
| username=os.getenv('SF_USERNAME'), | |
| password=os.getenv('SF_PASSWORD'), | |
| security_token=os.getenv('SF_SECURITY_TOKEN'), | |
| version=os.getenv('SF_API_VERSION') | |
| ) | |
| # Fast2SMS Configuration | |
| FAST2SMS_API_KEY = os.getenv('FAST2SMS_API_KEY') | |
| FAST2SMS_URL = 'https://www.fast2sms.com/dev/bulkV2' | |
| def send_sms(phone, message): | |
| payload = { | |
| 'authorization': FAST2SMS_API_KEY, | |
| 'message': message, | |
| 'numbers': phone, | |
| 'route': 'p', | |
| 'flash': 0 | |
| } | |
| response = requests.post(FAST2SMS_URL, params=payload) | |
| return response.json() | |
| def send_followup_messages(): | |
| interactions = sf.query("SELECT Id, Patient__r.Phone__c, Patient__r.Consent__c, Response__c FROM Interaction__c WHERE CreatedDate = TODAY")['records'] | |
| for interaction in interactions: | |
| if interaction['Patient__r']['Consent__c']: | |
| send_sms(interaction['Patient__r']['Phone__c'], 'Follow-up required based on your response.') | |
| appointments = sf.query("SELECT Id, Patient__r.Phone__c, Patient__r.Consent__c, Appointment_Date__c FROM Appointment__c WHERE Appointment_Date__c = TODAY")['records'] | |
| for appt in appointments: | |
| if appt['Patient__r']['Consent__c']: | |
| send_sms(appt['Patient__r']['Phone__c'], f'Reminder: Your appointment is today at {appt["Appointment_Date__c"]}.') | |
| scheduler = BackgroundScheduler() | |
| scheduler.add_job(send_followup_messages, 'interval', days=1) | |
| scheduler.start() | |
| # Gradio Interface Functions | |
| def get_dashboard(patient_id): | |
| if not patient_id: | |
| return "Please log in first." | |
| patient = sf.query(f"SELECT Name FROM Patient_Details__c WHERE Id = '{patient_id}'")['records'][0] | |
| appointments = sf.query(f"SELECT Id, Appointment_Date__c, Appointment_Time__c, Doctor__r.Name, Status__c FROM Appointment__c WHERE Patient__c = '{patient_id}'")['records'] | |
| output = f"Welcome, {patient['Name']}\n\nAppointments:\n" | |
| for appt in appointments: | |
| output += f"- Date: {appt['Appointment_Date__c']}, Time: {appt['Appointment_Time__c']}, Doctor: {appt['Doctor__r']['Name']}, Status: {appt['Status__c']}\n" | |
| return output | |
| def get_patient_name(patient_id): | |
| if not patient_id: | |
| return "" | |
| try: | |
| patient = sf.query(f"SELECT Name FROM Patient_Details__c WHERE Id = '{patient_id}'")['records'][0] | |
| return patient['Name'] | |
| except IndexError: | |
| return "Patient not found" | |
| def update_patient_details(patient_id, name, address, phone, dob, gender, emergency_contact_name, emergency_contact_number, emergency_contact_relationship, marital_status, medication, language, consent): | |
| if not patient_id: | |
| return "Please log in first." | |
| sf.Patient_Details__c.update(patient_id, { | |
| 'Name': name, | |
| 'Address__c': address, | |
| 'Phone__c': phone, | |
| 'DateOfBirth__c': dob.strftime('%Y-%m-%d'), | |
| 'Gender__c': gender, | |
| 'Emergency_Contact_Name__c': emergency_contact_name, | |
| 'Emergency_Contact_Number__c': emergency_contact_number, | |
| 'Emergency_Contact_Relationship__c': emergency_contact_relationship, | |
| 'Marital_Status__c': marital_status, | |
| 'Medication__c': medication, | |
| 'Language__c': language, | |
| 'Consent__c': bool(consent) | |
| }) | |
| return "Patient details updated successfully!" | |
| def appointment_scheduling(patient_id, doctor, appointment_date, appointment_time, purpose): | |
| if not patient_id: | |
| return "Please log in first." | |
| sf.Appointment__c.create({ | |
| 'Patient__c': patient_id, | |
| 'Doctor__c': doctor, | |
| 'Appointment_Date__c': appointment_date.strftime('%Y-%m-%d'), | |
| 'Appointment_Time__c': appointment_time, | |
| 'Purpose__c': purpose, | |
| 'Status__c': 'Scheduled' | |
| }) | |
| return "Appointment scheduled successfully!" | |
| def login(email, password): | |
| credential = sf.query(f"SELECT Id, Patient_Details__c FROM Patient_Credentials__c WHERE Email__c = '{email}' AND Password__c = '{hashlib.sha256(password.encode()).hexdigest()}'")['records'] | |
| if credential: | |
| return credential[0]['Patient_Details__c'], gr.update(value="Logged in successfully!"), gr.update(visible=True), gr.update(visible=False) | |
| return None, "Invalid credentials", gr.update(visible=False), gr.update(visible=False) | |
| def logout(): | |
| return None, gr.update(value="Logged out."), gr.update(visible=False), gr.update(visible=True) | |
| # Gradio Interface Setup | |
| interface = gr.Blocks(title="Clinic Portal") | |
| with interface: | |
| gr.Markdown("# Clinic Portal") | |
| patient_id = gr.State(value=None) | |
| with gr.Tab("Login"): | |
| email = gr.Textbox(label="Email") | |
| password = gr.Textbox(label="Password", type="password") | |
| login_btn = gr.Button("Login") | |
| logout_btn = gr.Button("Logout", visible=False) | |
| output = gr.Textbox(label="Status") | |
| login_btn.click(fn=login, inputs=[email, password], outputs=[patient_id, output, logout_btn, login_btn]) | |
| logout_btn.click(fn=logout, inputs=[], outputs=[patient_id, output, logout_btn, login_btn]) | |
| with gr.Tab("Dashboard"): | |
| dashboard_output = gr.Textbox(label="Dashboard", interactive=False) | |
| dashboard_btn = gr.Button("Refresh Dashboard") | |
| dashboard_btn.click(fn=get_dashboard, inputs=patient_id, outputs=dashboard_output) | |
| with gr.Tab("Patient Details"): | |
| name = gr.Textbox(label="Name", value="", interactive=True) | |
| address = gr.Textbox(label="Address") | |
| phone = gr.Textbox(label="Phone") | |
| dob = gr.DatePicker(label="Date of Birth") | |
| gender = gr.Dropdown(choices=["Male", "Female", "Other"], label="Gender") | |
| emergency_contact_name = gr.Textbox(label="Emergency Contact Name") | |
| emergency_contact_number = gr.Textbox(label="Emergency Contact Number") | |
| emergency_contact_relationship = gr.Dropdown(choices=["Parent", "Spouse", "Other"], label="Emergency Contact Relationship") | |
| marital_status = gr.Dropdown(choices=["Single", "Married", "Other"], label="Marital Status") | |
| medication = gr.Textbox(label="Medication") | |
| language = gr.Dropdown(choices=["English", "Spanish", "French"], label="Language") | |
| consent = gr.Checkbox(label="Consent for SMS/WhatsApp Follow-ups") | |
| save_btn = gr.Button("Save") | |
| status = gr.Textbox(label="Status", interactive=False) | |
| patient_id.change(fn=get_patient_name, inputs=patient_id, outputs=name) | |
| save_btn.click(fn=update_patient_details, inputs=[patient_id, name, address, phone, dob, gender, emergency_contact_name, emergency_contact_number, emergency_contact_relationship, marital_status, medication, language, consent], outputs=status) | |
| with gr.Tab("Appointment Scheduling"): | |
| doctor = gr.Dropdown(choices=[doc['Id'] for doc in sf.query("SELECT Id, Name FROM Doctor__c WHERE Is_Available__c = true")['records']], label="Doctor") | |
| appointment_date = gr.DatePicker(label="Appointment Date") | |
| appointment_time = gr.Textbox(label="Appointment Time", type="text") | |
| purpose = gr.Textbox(label="Purpose") | |
| schedule_btn = gr.Button("Schedule") | |
| status = gr.Textbox(label="Status", interactive=False) | |
| schedule_btn.click(fn=appointment_scheduling, inputs=[patient_id, doctor, appointment_date, appointment_time, purpose], outputs=status) | |
| interface.launch(server_name="0.0.0.0", server_port=7860) # Gradio default port | |