Booking / app.py
cnph001's picture
Update app.py
a1cd594 verified
Raw
History Blame Contribute Delete
2.87 kB
import gradio as gr
import spaces
from PIL import Image, ImageDraw
import time
IMAGE_PATH = "floor_layout.png"
TABLES_LAYOUT = {
"Table 1": {"x": (50, 180), "y": (50, 180)},
"Table 2": {"x": (220, 350), "y": (50, 180)},
"Table 3": {"x": (50, 180), "y": (250, 380)},
"Table 4": {"x": (220, 350), "y": (250, 380)},
"VIP Booth": {"x": (400, 600), "y": (100, 400)},
}
def get_centers():
centers = {}
for name, bounds in TABLES_LAYOUT.items():
x_min, x_max = bounds["x"]
y_min, y_max = bounds["y"]
centers[name] = (
(x_min + x_max) // 2,
(y_min + y_max) // 2
)
return centers
def draw_tables(selected=None):
img = Image.open(IMAGE_PATH).convert("RGBA")
draw = ImageDraw.Draw(img, "RGBA")
for name, (x, y) in get_centers().items():
if name == selected:
fill = (0, 255, 0, 120) # green selected
outline = "lime"
else:
fill = (255, 0, 0, 100) # red available
outline = "red"
radius = 35
draw.ellipse(
(
x - radius,
y - radius,
x + radius,
y + radius
),
fill=fill,
outline=outline,
width=4
)
# Add table name
text_position = (x - 35, y - 8)
draw.text(
text_position,
name,
fill="white"
)
return img
@spaces.GPU
def gpu_warmup():
#Small GPU-compatible placeholder
time.sleep(0.01)
return True
def handle_image_click(evt: gr.SelectData):
if not evt or not evt.index:
return draw_tables(), "No click detected."
click_x, click_y = evt.index
for table_name, bounds in TABLES_LAYOUT.items():
x_min, x_max = bounds["x"]
y_min, y_max = bounds["y"]
if x_min <= click_x <= x_max and y_min <= click_y <= y_max:
return (
draw_tables(table_name),
f"🎯 Selected: **{table_name}**"
)
return (
draw_tables(),
f"Clicked at ({click_x}, {click_y}) - no table found."
)
with gr.Blocks() as demo:
gr.Markdown(
"# 🍽️ Restaurant Table Reservation Layout"
)
gr.Markdown(
"Click a highlighted table to select it."
)
with gr.Row():
floor_plan = gr.Image(
value=draw_tables(),
label="Floor Plan",
type="pil",
interactive=True
)
status = gr.Markdown(
"No table selected."
)
floor_plan.select(
fn=handle_image_click,
outputs=[floor_plan, status]
)
demo.launch()