File size: 2,870 Bytes
248faf4
8fe565c
94a2175
a1cd594
8fe565c
3cac69c
 
 
94a2175
3cac69c
94a2175
3cac69c
 
 
 
94a2175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fe565c
a1cd594
 
 
 
 
3cac69c
94a2175
3cac69c
94a2175
3cac69c
 
 
 
94a2175
3cac69c
 
 
 
 
94a2175
 
 
 
 
 
 
 
 
 
0410be7
86f195c
3cac69c
94a2175
 
3cac69c
 
94a2175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cac69c
 
 
94a2175
3cac69c
0410be7
94a2175
3cac69c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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()