Spaces:
Runtime error
Runtime error
import gradio as gr | |
import dateparser | |
from datetime import datetime | |
def calculate_days_between(start_date_str, end_date_str): | |
def parse_date(date_str): | |
date = dateparser.parse(date_str) | |
if date is None: | |
raise ValueError("Could not parse the date. Ensure it is in natural language or YYYY-MM-DD format.") | |
return date | |
try: | |
start_date = parse_date(start_date_str) | |
end_date = parse_date(end_date_str) | |
num_days = (end_date - start_date).days | |
return f'The number of days between {start_date.strftime("%Y-%m-%d")} and {end_date.strftime("%Y-%m-%d")} is {num_days} days.' | |
except ValueError as e: | |
return str(e) | |
iface = gr.Interface( | |
fn=calculate_days_between, | |
inputs=[ | |
gr.Textbox(placeholder="2021-09-01 or September 1, 2021", label="Start Date"), | |
gr.Textbox(placeholder="2024-01-23 or January 23, 2024", label="End Date") | |
], | |
outputs="text", | |
title="Date Diffusion", | |
description="Enter two dates to calculate the number of days between them. You can use natural language (e.g., 'July 20, 2023', 'a week ago', 'yesterday') or a specific date format 'YYYY-MM-DD' (e.g., '2023-07-20').", | |
examples=[ | |
["2021-09-01", "2024-01-23"], | |
["September 1, 2021", "January 23, 2024"], | |
["today", "Febuary 18, 2026"] | |
], | |
theme="soft", | |
cache_examples=False | |
) | |
iface.launch() | |