Spaces:
Sleeping
Sleeping
File size: 3,767 Bytes
b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e bcc7224 d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb d0c627e b02a7cb |
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 |
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()
|