Spaces:
Running
on
Zero
Running
on
Zero
VictorSanh
commited on
Commit
•
1896bc8
1
Parent(s):
f82900d
first commit
Browse files
README.md
CHANGED
@@ -2,11 +2,11 @@
|
|
2 |
title: Img2html
|
3 |
emoji: ⚡
|
4 |
colorFrom: red
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.13.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
11 |
|
12 |
-
|
|
|
2 |
title: Img2html
|
3 |
emoji: ⚡
|
4 |
colorFrom: red
|
5 |
+
colorTo: green
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.13.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
11 |
|
12 |
+
Demo
|
app.py
ADDED
@@ -0,0 +1,271 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List, Optional, Tuple
|
2 |
+
from PIL import Image
|
3 |
+
from playwright.sync_api import sync_playwright
|
4 |
+
import os
|
5 |
+
import gradio as gr
|
6 |
+
from gradio_client.client import DEFAULT_TEMP_DIR
|
7 |
+
from transformers import AutoProcessor, AutoModelForCausalLM
|
8 |
+
API_TOKEN = os.getenv("HF_AUTH_TOKEN")
|
9 |
+
# PROCESSOR = AutoProcessor.from_pretrained(
|
10 |
+
# "HuggingFaceM4/img2html",
|
11 |
+
# token=API_TOKEN,
|
12 |
+
# )
|
13 |
+
|
14 |
+
IMAGE_GALLERY_PATHS = [
|
15 |
+
f"example_images/{ex_image}"
|
16 |
+
for ex_image in os.listdir(f"example_images")
|
17 |
+
]
|
18 |
+
|
19 |
+
def add_file_gallery(selected_state: gr.SelectData, gallery_list: List[str]):
|
20 |
+
# return (
|
21 |
+
# f"example_images/{gallery_list.root[selected_state.index].image.orig_name}",
|
22 |
+
# "",
|
23 |
+
# )
|
24 |
+
return f"example_images/{gallery_list.root[selected_state.index].image.orig_name}"
|
25 |
+
|
26 |
+
def expand_layout():
|
27 |
+
return gr.Column(scale=2), gr.Textbox()
|
28 |
+
|
29 |
+
def render_webpage(
|
30 |
+
html_css_code,
|
31 |
+
):
|
32 |
+
with sync_playwright() as p:
|
33 |
+
browser = p.chromium.launch(headless=True)
|
34 |
+
context = browser.new_context(
|
35 |
+
user_agent=(
|
36 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0"
|
37 |
+
" Safari/537.36"
|
38 |
+
)
|
39 |
+
)
|
40 |
+
page = context.new_page()
|
41 |
+
page.set_content(html_css_code)
|
42 |
+
page.wait_for_load_state("networkidle")
|
43 |
+
output_path_screenshot = f"{DEFAULT_TEMP_DIR}/{hash(html_css_code)}.png"
|
44 |
+
page.screenshot(path=output_path_screenshot, full_page=True)
|
45 |
+
|
46 |
+
context.close()
|
47 |
+
browser.close()
|
48 |
+
|
49 |
+
return Image.open(output_path_screenshot)
|
50 |
+
|
51 |
+
|
52 |
+
def model_inference(
|
53 |
+
image,
|
54 |
+
):
|
55 |
+
CAR_COMPNAY = """<!DOCTYPE html>
|
56 |
+
<html lang="en">
|
57 |
+
<head>
|
58 |
+
<meta charset="UTF-8">
|
59 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
60 |
+
<title>XYZ Car Company</title>
|
61 |
+
<style>
|
62 |
+
body {
|
63 |
+
font-family: 'Arial', sans-serif;
|
64 |
+
margin: 0;
|
65 |
+
padding: 0;
|
66 |
+
background-color: #f4f4f4;
|
67 |
+
}
|
68 |
+
|
69 |
+
header {
|
70 |
+
background-color: #333;
|
71 |
+
color: #fff;
|
72 |
+
padding: 1em;
|
73 |
+
text-align: center;
|
74 |
+
}
|
75 |
+
|
76 |
+
nav {
|
77 |
+
background-color: #555;
|
78 |
+
color: #fff;
|
79 |
+
padding: 0.5em;
|
80 |
+
text-align: center;
|
81 |
+
}
|
82 |
+
|
83 |
+
nav a {
|
84 |
+
color: #fff;
|
85 |
+
text-decoration: none;
|
86 |
+
padding: 0.5em 1em;
|
87 |
+
margin: 0 1em;
|
88 |
+
}
|
89 |
+
|
90 |
+
section {
|
91 |
+
padding: 2em;
|
92 |
+
}
|
93 |
+
|
94 |
+
h2 {
|
95 |
+
color: #333;
|
96 |
+
}
|
97 |
+
|
98 |
+
.car-container {
|
99 |
+
display: flex;
|
100 |
+
flex-wrap: wrap;
|
101 |
+
justify-content: space-around;
|
102 |
+
}
|
103 |
+
|
104 |
+
.car-card {
|
105 |
+
width: 300px;
|
106 |
+
margin: 1em;
|
107 |
+
border: 1px solid #ddd;
|
108 |
+
border-radius: 5px;
|
109 |
+
overflow: hidden;
|
110 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
111 |
+
}
|
112 |
+
|
113 |
+
.car-image {
|
114 |
+
width: 100%;
|
115 |
+
height: 150px;
|
116 |
+
object-fit: cover;
|
117 |
+
}
|
118 |
+
|
119 |
+
.car-details {
|
120 |
+
padding: 1em;
|
121 |
+
}
|
122 |
+
|
123 |
+
footer {
|
124 |
+
background-color: #333;
|
125 |
+
color: #fff;
|
126 |
+
text-align: center;
|
127 |
+
padding: 1em;
|
128 |
+
position: fixed;
|
129 |
+
bottom: 0;
|
130 |
+
width: 100%;
|
131 |
+
}
|
132 |
+
</style>
|
133 |
+
</head>
|
134 |
+
<body>
|
135 |
+
|
136 |
+
<header>
|
137 |
+
<h1>XYZ Car Company</h1>
|
138 |
+
</header>
|
139 |
+
|
140 |
+
<nav>
|
141 |
+
<a href="#">Home</a>
|
142 |
+
<a href="#">Models</a>
|
143 |
+
<a href="#">About Us</a>
|
144 |
+
<a href="#">Contact</a>
|
145 |
+
</nav>
|
146 |
+
|
147 |
+
<section>
|
148 |
+
<h2>Our Cars</h2>
|
149 |
+
<div class="car-container">
|
150 |
+
<div class="car-card">
|
151 |
+
<img src="car1.jpg" alt="Car 1" class="car-image">
|
152 |
+
<div class="car-details">
|
153 |
+
<h3>Model A</h3>
|
154 |
+
<p>Description of Model A.</p>
|
155 |
+
</div>
|
156 |
+
</div>
|
157 |
+
|
158 |
+
<div class="car-card">
|
159 |
+
<img src="car2.jpg" alt="Car 2" class="car-image">
|
160 |
+
<div class="car-details">
|
161 |
+
<h3>Model B</h3>
|
162 |
+
<p>Description of Model B.</p>
|
163 |
+
</div>
|
164 |
+
</div>
|
165 |
+
|
166 |
+
<!-- Add more car cards as needed -->
|
167 |
+
</div>
|
168 |
+
</section>
|
169 |
+
|
170 |
+
<footer>
|
171 |
+
© 2024 XYZ Car Company. All rights reserved.
|
172 |
+
</footer>
|
173 |
+
|
174 |
+
</body>
|
175 |
+
</html>"""
|
176 |
+
rendered_page = render_webpage(CAR_COMPNAY)
|
177 |
+
return CAR_COMPNAY, rendered_page
|
178 |
+
|
179 |
+
# textbox = gr.Textbox(
|
180 |
+
# placeholder="Upload an image and ask the AI to create a meme!",
|
181 |
+
# show_label=False,
|
182 |
+
# value="Write a meme about this image.",
|
183 |
+
# visible=True,
|
184 |
+
# container=False,
|
185 |
+
# label="Text input",
|
186 |
+
# scale=8,
|
187 |
+
# max_lines=5,
|
188 |
+
# )
|
189 |
+
|
190 |
+
generated_html = gr.Textbox(
|
191 |
+
label="IDEFICS Generated HTML",
|
192 |
+
elem_id="generated_html",
|
193 |
+
)
|
194 |
+
rendered_html = gr.Image(
|
195 |
+
)
|
196 |
+
|
197 |
+
css = """
|
198 |
+
.gradio-container{max-width: 1000px!important}
|
199 |
+
h1{display: flex;align-items: center;justify-content: center;gap: .25em}
|
200 |
+
*{transition: width 0.5s ease, flex-grow 0.5s ease}
|
201 |
+
"""
|
202 |
+
|
203 |
+
with gr.Blocks(title="Img2html", theme=gr.themes.Base(), css=css) as demo:
|
204 |
+
with gr.Row(equal_height=True):
|
205 |
+
# scale=2 when expanded
|
206 |
+
with gr.Column(scale=4, min_width=250) as upload_area:
|
207 |
+
imagebox = gr.Image(
|
208 |
+
type="filepath", label="Image to HTML", height=272, visible=True, sources=["upload", "clipboard"],
|
209 |
+
)
|
210 |
+
with gr.Group():
|
211 |
+
with gr.Row():
|
212 |
+
submit_btn = gr.Button(
|
213 |
+
value="▶️ Submit", visible=True, min_width=120
|
214 |
+
)
|
215 |
+
clear_btn = gr.ClearButton(
|
216 |
+
[imagebox, generated_html, rendered_html], value="🧹 Clear", min_width=120
|
217 |
+
)
|
218 |
+
regenerate_btn = gr.Button(
|
219 |
+
value="🔄 Regenerate", visible=True, min_width=120
|
220 |
+
)
|
221 |
+
with gr.Column(scale=5) as result_area:
|
222 |
+
rendered_html.render()
|
223 |
+
with gr.Row():
|
224 |
+
generated_html.render()
|
225 |
+
with gr.Row(equal_height=True):
|
226 |
+
template_gallery = gr.Gallery(
|
227 |
+
value=IMAGE_GALLERY_PATHS,
|
228 |
+
label="Templates Gallery",
|
229 |
+
allow_preview=False,
|
230 |
+
columns=4,
|
231 |
+
elem_id="gallery",
|
232 |
+
show_share_button=False,
|
233 |
+
height=400,
|
234 |
+
)
|
235 |
+
|
236 |
+
gr.on(
|
237 |
+
triggers=[
|
238 |
+
imagebox.upload,
|
239 |
+
submit_btn.click,
|
240 |
+
template_gallery.select,
|
241 |
+
regenerate_btn.click,
|
242 |
+
],
|
243 |
+
fn=model_inference,
|
244 |
+
inputs=[
|
245 |
+
imagebox,
|
246 |
+
],
|
247 |
+
outputs=[generated_html, rendered_html],
|
248 |
+
queue=False,
|
249 |
+
)
|
250 |
+
regenerate_btn.click(
|
251 |
+
fn=model_inference,
|
252 |
+
inputs=[
|
253 |
+
imagebox,
|
254 |
+
],
|
255 |
+
outputs=[generated_html, rendered_html],
|
256 |
+
queue=False,
|
257 |
+
)
|
258 |
+
template_gallery.select(
|
259 |
+
fn=add_file_gallery,
|
260 |
+
inputs=[template_gallery],
|
261 |
+
outputs=[imagebox],
|
262 |
+
queue=False,
|
263 |
+
)
|
264 |
+
demo.load(
|
265 |
+
# fn=choose_gallery,
|
266 |
+
# inputs=[gallery_type_choice],
|
267 |
+
# outputs=[template_gallery],
|
268 |
+
queue=False,
|
269 |
+
)
|
270 |
+
demo.queue(max_size=40, api_open=False)
|
271 |
+
demo.launch(max_threads=400)
|