delayed / app.py
SunForever's picture
Create app.py
682a837 verified
raw
history blame
1.06 kB
# app.py
import gradio as gr
from celery import Celery
import time
import os
# Initialize Celery with environment variable for Redis URL
REDIS_URL = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
celery_app = Celery('tasks', broker=REDIS_URL)
@celery_app.task
def calculate_after_delay(expression):
time.sleep(6) # Wait 6 seconds
try:
result = eval(expression) # Safely evaluate the math expression
return f"After 6 seconds, {expression} = {result}"
except:
return "Invalid math expression"
# Create Gradio interface
def create_gr_interface():
with gr.Blocks() as demo:
gr.Markdown("# Math Calculator with 6s Delay")
with gr.Row():
input_text = gr.Textbox(label="Enter math expression (e.g., 1+8+9)")
result = gr.Textbox(label="Result")
btn = gr.Button("Calculate")
btn.click(
fn=lambda x: calculate_after_delay(x),
inputs=input_text,
outputs=result
)
return demo
app = create_gr_interface()