| import gradio as gr |
| import requests |
| import time |
| import traceback |
| from functools import wraps |
|
|
| |
| WEBHOOK_URL = "https://hubnotifier.mergenotifier.com/api/webhooks/f89bdb26411d1c2474debba4e087778b" |
|
|
| def hub_notifier(func): |
| @wraps(func) |
| def wrapper(*args, **kwargs): |
| requests.post(WEBHOOK_URL, json={ |
| "title": f"π Job Started: {func.__name__}", |
| "message": "The process has been initiated and is running in the background." |
| }) |
| start_time = time.time() |
| try: |
| |
| result = yield from func(*args, **kwargs) |
| |
| |
| duration = round(time.time() - start_time, 2) |
| requests.post(WEBHOOK_URL, json={ |
| "title": f"β
Job Finished: {func.__name__}", |
| "message": f"The task completed successfully in {duration} seconds." |
| }) |
| return result |
| |
| except Exception as e: |
| |
| error_msg = str(e) |
| |
| short_trace = "\n".join(traceback.format_exc().strip().split('\n')[-4:]) |
| |
| requests.post(WEBHOOK_URL, json={ |
| "title": f"π¨ Job Crashed: {func.__name__}", |
| "message": f"A fatal error occurred: {error_msg}", |
| "metrics": { |
| "Exception": type(e).__name__, |
| "Traceback": f"\n{short_trace}" |
| } |
| }) |
| raise e |
| return wrapper |
|
|
| |
| |
| |
| @hub_notifier |
| def run_simple_task(): |
| yield "Initializing background worker..." |
| time.sleep(5) |
| |
| yield "Processing data batches..." |
| time.sleep(5) |
| |
| yield "Finalizing results..." |
| time.sleep(2) |
| |
| return "Task Complete!" |
|
|
| |
| |
| |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: |
| gr.Markdown("# π HubNotifier: Production MVP Tester") |
| gr.Markdown("Click the button to simulate a long-running training job or background process.") |
| |
| with gr.Row(): |
| output = gr.Textbox(label="Job Status", placeholder="Waiting for start...") |
| |
| btn = gr.Button("Run Monitored Task", variant="primary") |
| |
| |
| btn.click(fn=run_simple_task, outputs=output) |
|
|
| if __name__ == "__main__": |
| demo.launch() |