File size: 7,707 Bytes
1896bc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c497b41
 
 
 
 
 
671ee52
0d08595
c497b41
 
 
 
 
 
1896bc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from typing import List, Optional, Tuple
from PIL import Image
from playwright.sync_api import sync_playwright
import os
import gradio as gr
from gradio_client.client import DEFAULT_TEMP_DIR
from transformers import AutoProcessor, AutoModelForCausalLM
API_TOKEN = os.getenv("HF_AUTH_TOKEN")
# PROCESSOR = AutoProcessor.from_pretrained(
#     "HuggingFaceM4/img2html",
#     token=API_TOKEN,
# )

IMAGE_GALLERY_PATHS = [
    f"example_images/{ex_image}"
    for ex_image in os.listdir(f"example_images")
]

import subprocess

def install_playwright():
    try:
        subprocess.run(["playwright", "install"], check=True)
        print("Playwright installation successful.")
        subprocess.run(["sudo", "playwright", "install-deps"], check=True)
        print("Playwright deps installation successful.")
    except subprocess.CalledProcessError as e:
        print(f"Error during Playwright installation: {e}")

# Call the function to install Playwright
install_playwright()

def add_file_gallery(selected_state: gr.SelectData, gallery_list: List[str]):
    # return (
    #     f"example_images/{gallery_list.root[selected_state.index].image.orig_name}",
    #     "",
    # )
    return f"example_images/{gallery_list.root[selected_state.index].image.orig_name}"

def expand_layout():
    return gr.Column(scale=2), gr.Textbox()

def render_webpage(
    html_css_code,
):
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)
        context = browser.new_context(
            user_agent=(
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0"
                " Safari/537.36"
            )
        )
        page = context.new_page()
        page.set_content(html_css_code)
        page.wait_for_load_state("networkidle")
        output_path_screenshot = f"{DEFAULT_TEMP_DIR}/{hash(html_css_code)}.png"
        page.screenshot(path=output_path_screenshot, full_page=True)

        context.close()
        browser.close()

    return Image.open(output_path_screenshot)


def model_inference(
    image,
):
    CAR_COMPNAY = """<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>XYZ Car Company</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        header {
            background-color: #333;
            color: #fff;
            padding: 1em;
            text-align: center;
        }

        nav {
            background-color: #555;
            color: #fff;
            padding: 0.5em;
            text-align: center;
        }

        nav a {
            color: #fff;
            text-decoration: none;
            padding: 0.5em 1em;
            margin: 0 1em;
        }

        section {
            padding: 2em;
        }

        h2 {
            color: #333;
        }

        .car-container {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
        }

        .car-card {
            width: 300px;
            margin: 1em;
            border: 1px solid #ddd;
            border-radius: 5px;
            overflow: hidden;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .car-image {
            width: 100%;
            height: 150px;
            object-fit: cover;
        }

        .car-details {
            padding: 1em;
        }

        footer {
            background-color: #333;
            color: #fff;
            text-align: center;
            padding: 1em;
            position: fixed;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>
<body>

    <header>
        <h1>XYZ Car Company</h1>
    </header>

    <nav>
        <a href="#">Home</a>
        <a href="#">Models</a>
        <a href="#">About Us</a>
        <a href="#">Contact</a>
    </nav>

    <section>
        <h2>Our Cars</h2>
        <div class="car-container">
            <div class="car-card">
                <img src="car1.jpg" alt="Car 1" class="car-image">
                <div class="car-details">
                    <h3>Model A</h3>
                    <p>Description of Model A.</p>
                </div>
            </div>

            <div class="car-card">
                <img src="car2.jpg" alt="Car 2" class="car-image">
                <div class="car-details">
                    <h3>Model B</h3>
                    <p>Description of Model B.</p>
                </div>
            </div>

            <!-- Add more car cards as needed -->
        </div>
    </section>

    <footer>
        &copy; 2024 XYZ Car Company. All rights reserved.
    </footer>

</body>
</html>"""
    rendered_page = render_webpage(CAR_COMPNAY)
    return CAR_COMPNAY, rendered_page

# textbox = gr.Textbox(
#     placeholder="Upload an image and ask the AI to create a meme!",
#     show_label=False,
#     value="Write a meme about this image.",
#     visible=True,
#     container=False,
#     label="Text input",
#     scale=8,
#     max_lines=5,
# )

generated_html = gr.Textbox(
    label="IDEFICS Generated HTML",
    elem_id="generated_html",
)
rendered_html = gr.Image(
)

css = """
.gradio-container{max-width: 1000px!important}
h1{display: flex;align-items: center;justify-content: center;gap: .25em}
*{transition: width 0.5s ease, flex-grow 0.5s ease}
"""

with gr.Blocks(title="Img2html", theme=gr.themes.Base(), css=css) as demo:
    with gr.Row(equal_height=True):
        # scale=2 when expanded
        with gr.Column(scale=4, min_width=250) as upload_area:
            imagebox = gr.Image(
                type="filepath", label="Image to HTML", height=272, visible=True, sources=["upload", "clipboard"],
            )
            with gr.Group():
                with gr.Row():
                    submit_btn = gr.Button(
                        value="▶️ Submit", visible=True, min_width=120
                    )
                    clear_btn = gr.ClearButton(
                        [imagebox, generated_html, rendered_html], value="🧹 Clear", min_width=120
                    )
                    regenerate_btn = gr.Button(
                        value="🔄 Regenerate", visible=True, min_width=120
                    )
        with gr.Column(scale=5) as result_area:
            rendered_html.render()
    with gr.Row():
        generated_html.render()
    with gr.Row(equal_height=True):
        template_gallery = gr.Gallery(
            value=IMAGE_GALLERY_PATHS,
            label="Templates Gallery",
            allow_preview=False,
            columns=4,
            elem_id="gallery",
            show_share_button=False,
            height=400,
        )

    gr.on(
        triggers=[
            imagebox.upload,
            submit_btn.click,
            template_gallery.select,
            regenerate_btn.click,
        ],
        fn=model_inference,
        inputs=[
            imagebox,
        ],
        outputs=[generated_html, rendered_html],
        queue=False,
    )
    regenerate_btn.click(
        fn=model_inference,
        inputs=[
            imagebox,
        ],
        outputs=[generated_html, rendered_html],
        queue=False,
    )
    template_gallery.select(
        fn=add_file_gallery,
        inputs=[template_gallery],
        outputs=[imagebox],
        queue=False,
    )
    demo.load(
        # fn=choose_gallery,
        # inputs=[gallery_type_choice],
        # outputs=[template_gallery],
        queue=False,
    )
demo.queue(max_size=40, api_open=False)
demo.launch(max_threads=400)