Spaces:
Sleeping
Sleeping
File size: 9,296 Bytes
ca4bd13 9758397 ca4bd13 9758397 ca4bd13 9758397 ca4bd13 9758397 ca4bd13 |
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
import gradio as gr
import datetime
import json
import os
from classification_model import classify_medicine
from ocr_model import perform_ocr
MEDS_FILE = "medications.json"
def load_medications():
if os.path.exists(MEDS_FILE):
try:
with open(MEDS_FILE, "r") as f:
return json.load(f)
except Exception:
return []
return []
def save_medications(meds):
with open(MEDS_FILE, "w") as f:
json.dump(meds, f)
def classify_image(image):
with open(image, "rb") as f:
image_bytes = f.read()
result = classify_medicine(image_bytes)
return f"Class: {result['pill_class']}, Confidence: {result['confidence']:.2f}"
def ocr_image(image):
with open(image, "rb") as f:
image_bytes = f.read()
return perform_ocr(image_bytes)
def add_medication(name, dosage, frequency, start_date, end_date, notes):
# Convert dates to ISO strings for consistency
if isinstance(start_date, (float, int)):
start_date = datetime.datetime.fromtimestamp(start_date).date().isoformat()
elif isinstance(start_date, datetime.datetime):
start_date = start_date.date().isoformat()
elif isinstance(start_date, datetime.date):
start_date = start_date.isoformat()
if isinstance(end_date, (float, int)):
end_date = datetime.datetime.fromtimestamp(end_date).date().isoformat()
elif isinstance(end_date, datetime.datetime):
end_date = end_date.date().isoformat()
elif isinstance(end_date, datetime.date):
end_date = end_date.isoformat()
meds = load_medications()
meds.append({
"name": name,
"dosage": dosage,
"frequency": frequency,
"start_date": start_date,
"end_date": end_date,
"notes": notes
})
save_medications(meds)
return "β
Medication added successfully!"
def view_medications():
meds = load_medications()
if not meds:
return "No medications added."
return "\n".join([
f"{med['name']} - {med['dosage']} - {med['frequency']} ({med['start_date']} to {med['end_date']})"
for med in meds
])
def get_today_medications():
meds = load_medications()
today = datetime.date.today()
today_name = today.strftime("%A")
today_meds = []
for med in meds:
try:
start = datetime.date.fromisoformat(med["start_date"])
end = datetime.date.fromisoformat(med["end_date"])
frequency = med["frequency"]
# Skip if today is not within the medication date range
if not (start <= today <= end):
continue
# Handle weekly medications - only show on the appropriate day
if frequency == "Weekly" and today_name != "Monday": # Assuming weekly meds are taken on Monday
continue
today_meds.append(med)
except Exception:
continue
if not today_meds:
return "No medications scheduled for today."
return "\n".join([
f"{med['name']} - {med['dosage']} - {med['frequency']}"
+ (f" ({_get_time_of_day(med['frequency'])})" if _get_time_of_day(med['frequency']) else "")
for med in today_meds
])
def _get_time_of_day(frequency):
"""Helper function to return time of day based on frequency"""
if frequency == "Every morning":
return "Morning"
elif frequency == "Every evening":
return "Evening"
elif frequency == "Once daily":
return "Morning"
elif frequency == "Twice daily":
return "Morning and Evening"
elif frequency == "Three times daily":
return "Morning, Afternoon, and Evening"
elif frequency == "Four times daily":
return "Morning, Noon, Afternoon, and Night"
elif frequency == "Every 12 hours":
return "Morning and Evening"
return ""
def get_weekly_schedule():
meds = load_medications()
today = datetime.date.today()
week_meds = {(today + datetime.timedelta(days=i)).isoformat(): [] for i in range(7)}
days_of_week = [(today + datetime.timedelta(days=i)).strftime("%A") for i in range(7)]
for med in meds:
try:
start = datetime.date.fromisoformat(med["start_date"])
end = datetime.date.fromisoformat(med["end_date"])
frequency = med["frequency"]
except Exception:
continue
for i, day_str in enumerate(week_meds.keys()):
day_date = datetime.date.fromisoformat(day_str)
day_name = days_of_week[i]
# Skip if day is not within the medication date range
if not (start <= day_date <= end):
continue
# For weekly medications, only include on Mondays
if frequency == "Weekly" and day_name != "Monday":
continue
med_info = f"{med['name']} - {med['dosage']} - {med['frequency']}"
timing = _get_time_of_day(frequency)
if timing:
med_info += f" ({timing})"
week_meds[day_str].append(med_info)
# Format the output with day names for better readability
formatted_output = {}
for i, (day_str, meds_list) in enumerate(week_meds.items()):
day_name = days_of_week[i]
formatted_output[f"{day_name} ({day_str})"] = meds_list
return json.dumps(formatted_output, indent=2)
with gr.Blocks() as app:
gr.Markdown("# π Medication Tracker")
with gr.Tabs():
with gr.Tab("πΈ Identify Medicine"):
gr.Markdown("Upload an image to identify the medicine.")
with gr.Row():
image_input = gr.Image(type="filepath")
classify_output = gr.Textbox()
classify_button = gr.Button("Classify Medicine")
classify_button.click(classify_image, inputs=image_input, outputs=classify_output)
gr.Markdown("### Add this medication to your tracker")
identified_name = gr.Textbox(label="Medication Name")
identified_dosage = gr.Textbox(label="Dosage (e.g., 500mg)")
identified_frequency = gr.Dropdown(
label="Frequency",
choices=["Once daily", "Twice daily", "Three times daily", "Four times daily",
"Every morning", "Every evening", "Every 12 hours", "Weekly", "As needed"],
value="Once daily"
)
identified_start_date = gr.DateTime(label="Start Date")
identified_end_date = gr.DateTime(label="End Date")
identified_notes = gr.Textbox(label="Additional Notes")
identified_add_button = gr.Button("Add to My Medications")
identified_add_output = gr.Textbox()
identified_add_button.click(
add_medication,
inputs=[identified_name, identified_dosage, identified_frequency,
identified_start_date, identified_end_date, identified_notes],
outputs=identified_add_output
)
with gr.Tab("π Extract Medical Label"):
gr.Markdown("Upload an image of a prescription or label to extract text.")
image_input_ocr = gr.Image(type="filepath")
ocr_button = gr.Button("Extract Text")
ocr_output = gr.Textbox()
ocr_button.click(ocr_image, inputs=image_input_ocr, outputs=ocr_output)
with gr.Tab("π Add Medication"):
gr.Markdown("Add your medications manually.")
name = gr.Textbox(label="Medication Name")
dosage = gr.Textbox(label="Dosage (e.g., 500mg)")
frequency = gr.Dropdown(
label="Frequency",
choices=["Once daily", "Twice daily", "Three times daily", "Four times daily",
"Every morning", "Every evening", "Every 12 hours", "Weekly", "As needed"],
value="Once daily"
)
start_date = gr.DateTime(label="Start Date")
end_date = gr.DateTime(label="End Date")
notes = gr.Textbox(label="Additional Notes")
add_button = gr.Button("Add Medication")
add_output = gr.Textbox()
add_button.click(add_medication, inputs=[name, dosage, frequency, start_date, end_date, notes], outputs=add_output)
with gr.Tab("π
Medication Schedule"):
gr.Markdown("View your scheduled medications.")
today_button = gr.Button("View Today's Medications")
today_output = gr.Textbox(label="Today's Medications")
today_button.click(get_today_medications, outputs=today_output)
weekly_button = gr.Button("View Weekly Schedule")
weekly_output = gr.Textbox(label="Weekly Schedule")
weekly_button.click(get_weekly_schedule, outputs=weekly_output)
all_meds_button = gr.Button("View All Medications")
all_meds_output = gr.Textbox(label="All Medications")
all_meds_button.click(view_medications, outputs=all_meds_output)
app.launch()
|