Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import threading | |
| import time | |
| from datetime import datetime, timedelta, timezone | |
| from updater import run_update_pipeline | |
| IST = timezone(timedelta(hours=5, minutes=30)) | |
| status_logs = ["System Initialized."] | |
| update_running = False | |
| def append_log(msg): | |
| global status_logs | |
| timestamp = datetime.now(IST).strftime("%Y-%m-%d %H:%M:%S") | |
| full_msg = f"[{timestamp}] {msg}" | |
| status_logs.append(full_msg) | |
| if len(status_logs) > 50: | |
| status_logs.pop(0) | |
| def force_update(): | |
| global update_running | |
| if update_running: | |
| return "Update is already running!" | |
| update_running = True | |
| append_log("Manual update triggered.") | |
| def worker(): | |
| global update_running | |
| try: | |
| run_update_pipeline(log_callback=append_log) | |
| except Exception as e: | |
| append_log(f"Error in pipeline: {e}") | |
| finally: | |
| update_running = False | |
| threading.Thread(target=worker, daemon=True).start() | |
| return "Update started. Check logs for progress." | |
| def get_logs(): | |
| return "\n".join(status_logs) | |
| def daemon_loop(): | |
| global update_running | |
| append_log("Daemon thread started. Running initial startup check...") | |
| # Run immediately on startup | |
| update_running = True | |
| try: | |
| run_update_pipeline(log_callback=append_log) | |
| except Exception as e: | |
| append_log(f"Startup update failed: {e}") | |
| finally: | |
| update_running = False | |
| last_run_date = datetime.now(IST).date() | |
| while True: | |
| now = datetime.now(IST) | |
| is_weekday = now.weekday() < 5 | |
| past_market_close = now.hour > 15 or (now.hour == 15 and now.minute >= 30) | |
| if is_weekday and past_market_close and not update_running: | |
| if last_run_date != now.date(): | |
| append_log("Scheduled daily update starting...") | |
| update_running = True | |
| try: | |
| run_update_pipeline(log_callback=append_log) | |
| last_run_date = now.date() | |
| except Exception as e: | |
| append_log(f"Scheduled update failed: {e}") | |
| finally: | |
| update_running = False | |
| time.sleep(60) # check every minute | |
| threading.Thread(target=daemon_loop, daemon=True).start() | |
| with gr.Blocks(title="Groww Data Updater") as demo: | |
| gr.Markdown("# ๐ Groww Data Updater") | |
| gr.Markdown("This space automatically runs every weekday after 15:30 IST to fetch new minute-level data and push it to Hugging Face.") | |
| with gr.Row(): | |
| btn = gr.Button("Force Update Now", variant="primary") | |
| logs = gr.Textbox(label="Status Logs", lines=15, value=get_logs, every=5) | |
| btn.click(force_update, outputs=gr.Textbox(visible=False)) | |
| if __name__ == "__main__": | |
| demo.queue().launch(server_name="0.0.0.0", server_port=7860) | |