Update app.py
Browse files
app.py
CHANGED
|
@@ -6,29 +6,30 @@ import textwrap
|
|
| 6 |
from datetime import datetime, timedelta
|
| 7 |
import streamlit as st
|
| 8 |
|
| 9 |
-
# Set your API key
|
| 10 |
OPENROUTER_API_KEY = "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"
|
| 11 |
|
| 12 |
-
# Load
|
| 13 |
dataset_url = "https://huggingface.co/spaces/chrisaldikaraharja/AutomatedFollowUpSystem/resolve/main/PatientData%20-%20Sheet1.csv"
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def generate_follow_up_message(
|
| 23 |
try:
|
| 24 |
-
#
|
| 25 |
-
patient_data = data[data['Patient
|
|
|
|
| 26 |
if patient_data.empty:
|
| 27 |
-
return f"Patient with
|
| 28 |
|
| 29 |
patient_data = patient_data.iloc[0]
|
| 30 |
|
| 31 |
-
|
| 32 |
health_condition = patient_data['Health Condition']
|
| 33 |
last_visit_date = patient_data['Last Visit Date']
|
| 34 |
patient_email = patient_data['Email']
|
|
@@ -51,6 +52,8 @@ def generate_follow_up_message(patient_id):
|
|
| 51 |
headers={
|
| 52 |
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 53 |
"Content-Type": "application/json",
|
|
|
|
|
|
|
| 54 |
},
|
| 55 |
data=json.dumps({
|
| 56 |
"model": "deepseek/deepseek-chat:free",
|
|
@@ -67,32 +70,31 @@ def generate_follow_up_message(patient_id):
|
|
| 67 |
if response.status_code == 200:
|
| 68 |
follow_up_message = response.json()['choices'][0]['message']['content']
|
| 69 |
formatted_message = "\n".join(textwrap.wrap(follow_up_message, width=80))
|
| 70 |
-
return formatted_message, patient_email
|
| 71 |
else:
|
| 72 |
-
return f"Failed to generate message. Error: {response.status_code}", None
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
-
return f"An error occurred: {e}", None
|
| 76 |
|
| 77 |
# Streamlit UI for patient search and message sending
|
| 78 |
st.title("Patient Follow-Up Reminder System")
|
| 79 |
|
| 80 |
-
# Input
|
| 81 |
patient_name_input = st.text_input("Enter Patient Name")
|
| 82 |
-
patient_id_input = st.number_input("Enter Patient ID", min_value=1)
|
| 83 |
|
| 84 |
-
if patient_name_input
|
| 85 |
# Generate the follow-up message based on the input
|
| 86 |
-
follow_up_message, patient_email = generate_follow_up_message(
|
| 87 |
|
| 88 |
if "Patient not found" not in follow_up_message:
|
| 89 |
st.write("**Generated Message:**")
|
| 90 |
st.write(follow_up_message)
|
|
|
|
| 91 |
|
| 92 |
if st.button("Send Reminder"):
|
| 93 |
-
#
|
| 94 |
try:
|
| 95 |
-
# Replace with your email sending functionality (e.g., using SMTP or any email service)
|
| 96 |
send_email(patient_email, follow_up_message)
|
| 97 |
st.success("Message sent successfully!")
|
| 98 |
except Exception as e:
|
|
@@ -100,9 +102,39 @@ if patient_name_input and patient_id_input:
|
|
| 100 |
else:
|
| 101 |
st.warning(follow_up_message)
|
| 102 |
|
| 103 |
-
#
|
| 104 |
def send_email(recipient_email, message):
|
| 105 |
-
#
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from datetime import datetime, timedelta
|
| 7 |
import streamlit as st
|
| 8 |
|
| 9 |
+
# Set your API key
|
| 10 |
OPENROUTER_API_KEY = "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"
|
| 11 |
|
| 12 |
+
# Load your patient data
|
| 13 |
dataset_url = "https://huggingface.co/spaces/chrisaldikaraharja/AutomatedFollowUpSystem/resolve/main/PatientData%20-%20Sheet1.csv"
|
| 14 |
+
data = pd.read_csv(dataset_url) # Loading the patient data from the URL
|
| 15 |
+
st.write("Patient data loaded successfully.")
|
| 16 |
+
st.write(data.head()) # Displaying the first few rows of the dataset
|
| 17 |
+
|
| 18 |
+
# Mailjet API credentials
|
| 19 |
+
MAILJET_API_KEY = 'f746efdd7af5c96a033ddb90a78a5704'
|
| 20 |
+
MAILJET_SECRET_KEY = '5ee6955f792909206669fb10e9910b57'
|
| 21 |
+
|
| 22 |
+
def generate_follow_up_message(patient_name):
|
| 23 |
try:
|
| 24 |
+
# Filter data by patient name
|
| 25 |
+
patient_data = data[data['Patient Name'].str.contains(patient_name, case=False, na=False)]
|
| 26 |
+
|
| 27 |
if patient_data.empty:
|
| 28 |
+
return f"Patient with name {patient_name} not found.", None, None
|
| 29 |
|
| 30 |
patient_data = patient_data.iloc[0]
|
| 31 |
|
| 32 |
+
patient_id = patient_data['Patient ID']
|
| 33 |
health_condition = patient_data['Health Condition']
|
| 34 |
last_visit_date = patient_data['Last Visit Date']
|
| 35 |
patient_email = patient_data['Email']
|
|
|
|
| 52 |
headers={
|
| 53 |
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 54 |
"Content-Type": "application/json",
|
| 55 |
+
"HTTP-Referer": "<YOUR_SITE_URL>", # Optional. Site URL for rankings
|
| 56 |
+
"X-Title": "<YOUR_SITE_NAME>", # Optional. Site title for rankings
|
| 57 |
},
|
| 58 |
data=json.dumps({
|
| 59 |
"model": "deepseek/deepseek-chat:free",
|
|
|
|
| 70 |
if response.status_code == 200:
|
| 71 |
follow_up_message = response.json()['choices'][0]['message']['content']
|
| 72 |
formatted_message = "\n".join(textwrap.wrap(follow_up_message, width=80))
|
| 73 |
+
return formatted_message, patient_email, patient_id
|
| 74 |
else:
|
| 75 |
+
return f"Failed to generate message. Error: {response.status_code}", None, None
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
+
return f"An error occurred: {e}", None, None
|
| 79 |
|
| 80 |
# Streamlit UI for patient search and message sending
|
| 81 |
st.title("Patient Follow-Up Reminder System")
|
| 82 |
|
| 83 |
+
# Input box for patient name
|
| 84 |
patient_name_input = st.text_input("Enter Patient Name")
|
|
|
|
| 85 |
|
| 86 |
+
if patient_name_input:
|
| 87 |
# Generate the follow-up message based on the input
|
| 88 |
+
follow_up_message, patient_email, patient_id = generate_follow_up_message(patient_name_input)
|
| 89 |
|
| 90 |
if "Patient not found" not in follow_up_message:
|
| 91 |
st.write("**Generated Message:**")
|
| 92 |
st.write(follow_up_message)
|
| 93 |
+
st.write(f"**Patient ID:** {patient_id}")
|
| 94 |
|
| 95 |
if st.button("Send Reminder"):
|
| 96 |
+
# Send the email using Mailjet
|
| 97 |
try:
|
|
|
|
| 98 |
send_email(patient_email, follow_up_message)
|
| 99 |
st.success("Message sent successfully!")
|
| 100 |
except Exception as e:
|
|
|
|
| 102 |
else:
|
| 103 |
st.warning(follow_up_message)
|
| 104 |
|
| 105 |
+
# Function to send email using Mailjet
|
| 106 |
def send_email(recipient_email, message):
|
| 107 |
+
# Mailjet API endpoint
|
| 108 |
+
url = 'https://api.mailjet.com/v3.1/send'
|
| 109 |
+
|
| 110 |
+
# Email details
|
| 111 |
+
from_email = 'christ10aldika@gmail.com' # Sender email
|
| 112 |
+
subject = 'Appointment Reminder'
|
| 113 |
+
|
| 114 |
+
# Prepare the message body
|
| 115 |
+
message_body = f"""
|
| 116 |
+
Dear {recipient_email.split('@')[0]},\n\n
|
| 117 |
+
{message}\n\n
|
| 118 |
+
If you have any questions or concerns, please reach out to us at sentramedika@gmail.com.
|
| 119 |
+
"""
|
| 120 |
+
|
| 121 |
+
# Email payload
|
| 122 |
+
payload = {
|
| 123 |
+
"Messages": [
|
| 124 |
+
{
|
| 125 |
+
"From": {"Email": from_email, "Name": "Christ Aldika"}, # Sender name and email
|
| 126 |
+
"To": [{"Email": recipient_email, "Name": recipient_email}], # Recipient email
|
| 127 |
+
"Subject": subject,
|
| 128 |
+
"TextPart": message_body,
|
| 129 |
+
}
|
| 130 |
+
]
|
| 131 |
+
}
|
| 132 |
+
# Send the email request
|
| 133 |
+
response = requests.post(url, json=payload, auth=(MAILJET_API_KEY, MAILJET_SECRET_KEY))
|
| 134 |
+
|
| 135 |
+
if response.status_code == 200:
|
| 136 |
+
print("Email sent successfully via Mailjet!")
|
| 137 |
+
print("Response:", response.json())
|
| 138 |
+
else:
|
| 139 |
+
print(f"Failed to send email. Status Code: {response.status_code}")
|
| 140 |
+
print("Response:", response.text)
|