import logging logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) import gradio as gr from gradio_calendar import Calendar from gematria import calculate_gematria, strip_diacritics from datetime import datetime, timedelta import json import inflect # --- Helper Functions --- def calculate_gematria_sum(text): """Calculates the gematria sum of a given text.""" if text: text_gematria = calculate_gematria(strip_diacritics(text)) return text_gematria return None def number_to_ordinal_word(number): ordinal_dict = { 1: "first", 2: "second", 3: "third", 4: "fourth", 5: "fifth", 6: "sixth", 7: "seventh", 8: "eighth", 9: "ninth", 10: "tenth", 11: "eleventh", 12: "twelfth", 13: "thirteenth", 14: "fourteenth", 15: "fifteenth", 16: "sixteenth", 17: "seventeenth", 18: "eighteenth", 19: "nineteenth", 20: "twentieth", 21: "twentyfirst", 22: "twentysecond", 23: "twentythird", 24: "twentyfourth", 25: "twentyfifth", 26: "twentysixth", 27: "twentyseventh", 28: "twentyeighth", 29: "twentyninth", 30: "thirtieth", 31: "thirtyfirst" } return ordinal_dict.get(number, "") def date_to_words(date_string): """Converts a date in YYYY-MM-DD format to English words.""" inf_engine = inflect.engine() date_obj = datetime.strptime(date_string, "%Y-%m-%d") return format_date(date_obj, inf_engine) def month_year_to_words(date_string): """Converts a month and year in YYYY-MM format to English words.""" inf_engine = inflect.engine() date_obj = datetime.strptime(date_string, "%Y-%m") return format_date(date_obj, inf_engine) def format_date(date_obj, inf_engine): year = date_obj.year if 1100 <= year <= 1999: year_words = f"{inf_engine.number_to_words(year // 100, andword='') } hundred" if year % 100 != 0: year_words += f" {inf_engine.number_to_words(year % 100, andword='')}" else: year_words = inf_engine.number_to_words(year, andword='') year_formatted = year_words.replace(',', '') month = date_obj.strftime("%B") try: # Try getting the day, if available day = date_obj.day day_ordinal = number_to_ordinal_word(day) return f"{day_ordinal} {month} {year_formatted}" # Return with day except: return f"{month} {year_formatted}" # Return without day def format_year_to_words(year): """Formats a year as English words.""" inf_engine = inflect.engine() if 1100 <= year <= 1999: year_words = f"{inf_engine.number_to_words(year // 100, andword='')} hundred" if year % 100 != 0: year_words += f" {inf_engine.number_to_words(year % 100, andword='')}" else: year_words = inf_engine.number_to_words(year, andword='') return year_words.replace(',', '') def perform_gematria_calculation_for_date_range(start_date, end_date): """Performs gematria calculation for a date range and groups by sum.""" logger.debug(f"Start Date: {start_date}, End Date: {end_date}") results = {} delta = timedelta(days=1) current_date = start_date processed_months = set() # To track processed month-year combinations processed_years = set() # To track processed years while current_date <= end_date: # Full date calculation date_string = current_date.strftime("%Y-%m-%d") date_words = date_to_words(date_string) gematria_sum = calculate_gematria_sum(date_words) if gematria_sum not in results: results[gematria_sum] = [] results[gematria_sum].append({"date": date_string, "date_words": date_words}) # Month+Year calculation (processed once per month) month_year_key = current_date.strftime("%Y-%m") if month_year_key not in processed_months: processed_months.add(month_year_key) month_year_words = f"{current_date.strftime('%B')} {format_year_to_words(current_date.year)}" month_year_gematria_sum = calculate_gematria_sum(month_year_words) if month_year_gematria_sum not in results: results[month_year_gematria_sum] = [] results[month_year_gematria_sum].append({ "date": month_year_key, "date_words": month_year_words }) # Year-only calculation (processed once per year) year_key = str(current_date.year) if year_key not in processed_years: processed_years.add(year_key) year_words = format_year_to_words(current_date.year) year_gematria_sum = calculate_gematria_sum(year_words) if year_gematria_sum not in results: results[year_gematria_sum] = [] results[year_gematria_sum].append({ "date": year_key, "date_words": year_words }) current_date += delta return results # --- Event Handlers --- def perform_calculation(start_date, end_date, include_words): """Performs the calculation and generates the JSON output.""" logger.debug(f"perform_calculation called with: start_date={start_date}, end_date={end_date}, include_words={include_words}") results = perform_gematria_calculation_for_date_range(start_date, end_date) json_result = generate_json_output(results, start_date, end_date, include_words) return json_result def generate_json_output(results, start_date, end_date, include_words): """Generates the JSON output with the date range and results.""" result = { "DateRange": { "StartDate": start_date.strftime("%Y-%m-%d"), "EndDate": end_date.strftime("%Y-%m-%d") }, "Results": [] } for gematria_sum, entries in results.items(): group = { "GematriaSum": gematria_sum, "Entries": [] } for entry in entries: entry_data = {"date": entry["date"]} if include_words: # Conditionally include `date_words` entry_data["date_words"] = entry["date_words"] group["Entries"].append(entry_data) result["Results"].append(group) # Ensure JSON validity return json.dumps(result, indent=4, ensure_ascii=False) # --- Main Gradio App --- with gr.Blocks() as app: with gr.Row(): start_date = Calendar(type="datetime", label="Start Date") end_date = Calendar(type="datetime", label="End Date") include_date_words = gr.Checkbox(value=True, label="Include Date-Words in JSON") calculate_btn = gr.Button("Calculate Gematria for Date Range") json_output = gr.Textbox(label="JSON Output") download_btn = gr.Button("Download JSON") json_file = gr.File(label="Downloaded JSON") # --- Event Triggers --- calculate_btn.click( perform_calculation, inputs=[start_date, end_date, include_date_words], outputs=[json_output], api_name="calculate_gematria" ) download_btn.click( lambda json_data, start_date, end_date: download_json(json_data, start_date, end_date), inputs=[json_output, start_date, end_date], outputs=[json_file], ) if __name__ == "__main__": app.launch(share=False)