Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from datetime import datetime | |
| import pytz | |
| # In-session reminder list | |
| reminders = [] # Format: {'time': datetime, 'message': str, 'tz': str} | |
| # Add a reminder | |
| def add_reminder(time_str, message, timezone): | |
| try: | |
| user_tz = pytz.timezone(timezone) | |
| naive_dt = datetime.strptime(time_str, "%Y-%m-%d %H:%M") | |
| aware_dt = user_tz.localize(naive_dt) | |
| now_utc = datetime.now(pytz.utc) | |
| if aware_dt.astimezone(pytz.utc) < now_utc: | |
| return "β Time is in the past. Please enter a future time." | |
| reminders.append({'time': aware_dt, 'message': message, 'tz': timezone}) | |
| reminders.sort(key=lambda x: x['time'].astimezone(pytz.utc)) | |
| return f"β Reminder added for {aware_dt.strftime('%Y-%m-%d %H:%M %Z')}" | |
| except Exception as e: | |
| return f"β Error: {str(e)}" | |
| # List reminders | |
| def list_reminders(): | |
| if not reminders: | |
| return "π No reminders yet." | |
| now = datetime.now(pytz.utc) | |
| result = [] | |
| for r in reminders: | |
| local_time = r['time'].astimezone(pytz.timezone(r['tz'])) | |
| time_left = r['time'].astimezone(pytz.utc) - now | |
| mins = int(time_left.total_seconds() / 60) | |
| result.append(f"π {local_time.strftime('%Y-%m-%d %H:%M %Z')} β {r['message']} ({mins} min left)") | |
| return "\n".join(result) | |
| # Check next due reminder | |
| def check_next(): | |
| now = datetime.now(pytz.utc) | |
| for r in reminders: | |
| if (r['time'].astimezone(pytz.utc) - now).total_seconds() < 60: | |
| local_time = r['time'].astimezone(pytz.timezone(r['tz'])) | |
| return f"π Due now: {r['message']} ({local_time.strftime('%H:%M %Z')})" | |
| return "β Nothing due right now." | |
| # Delete reminder | |
| def delete_reminder(time_str, timezone): | |
| try: | |
| user_tz = pytz.timezone(timezone) | |
| target_time = user_tz.localize(datetime.strptime(time_str, "%Y-%m-%d %H:%M")) | |
| global reminders | |
| original_len = len(reminders) | |
| reminders = [r for r in reminders if not ( | |
| r['time'].astimezone(pytz.utc) == target_time.astimezone(pytz.utc) | |
| )] | |
| if len(reminders) < original_len: | |
| return f"ποΈ Reminder at {time_str} ({timezone}) deleted." | |
| return "β οΈ No matching reminder found." | |
| except Exception as e: | |
| return f"β Error: {str(e)}" | |
| # Common timezone choices | |
| timezone_choices = ["UTC", "Asia/Kolkata", "America/New_York", "Europe/London", "Asia/Tokyo"] | |
| # Gradio UI | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## β° Stateful Reminder Agent with Timezone Support") | |
| with gr.Tab("β Add Reminder"): | |
| time_in = gr.Textbox(label="Time (YYYY-MM-DD HH:MM)", placeholder="2025-06-07 09:00") | |
| msg_in = gr.Textbox(label="Reminder Message", placeholder="Doctor appointment") | |
| tz_in = gr.Dropdown(label="Timezone", choices=timezone_choices, value="UTC") | |
| out_add = gr.Textbox(label="Status") | |
| gr.Button("Add Reminder").click(fn=add_reminder, inputs=[time_in, msg_in, tz_in], outputs=out_add) | |
| with gr.Tab("π View All Reminders"): | |
| out_list = gr.Textbox(label="Reminders", lines=10) | |
| gr.Button("List Reminders").click(fn=list_reminders, outputs=out_list) | |
| with gr.Tab("π Check Whatβs Due"): | |
| out_next = gr.Textbox(label="Next Reminder") | |
| gr.Button("What's Next?").click(fn=check_next, outputs=out_next) | |
| with gr.Tab("ποΈ Delete Reminder"): | |
| del_time = gr.Textbox(label="Time to Delete (YYYY-MM-DD HH:MM)") | |
| del_tz = gr.Dropdown(label="Timezone of Reminder", choices=timezone_choices, value="UTC") | |
| out_del = gr.Textbox(label="Status") | |
| gr.Button("Delete").click(fn=delete_reminder, inputs=[del_time, del_tz], outputs=out_del) | |
| demo.launch() | |