Spaces:
Running
on
Zero
Running
on
Zero
Commit
·
b537793
1
Parent(s):
2096b5f
update
Browse files
app.py
CHANGED
|
@@ -1,22 +1,31 @@
|
|
| 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 |
-
import subprocess
|
| 20 |
|
| 21 |
def install_playwright():
|
| 22 |
try:
|
|
@@ -25,19 +34,11 @@ def install_playwright():
|
|
| 25 |
except subprocess.CalledProcessError as e:
|
| 26 |
print(f"Error during Playwright installation: {e}")
|
| 27 |
|
| 28 |
-
# Call the function to install Playwright
|
| 29 |
install_playwright()
|
| 30 |
|
| 31 |
def add_file_gallery(selected_state: gr.SelectData, gallery_list: List[str]):
|
| 32 |
-
# return (
|
| 33 |
-
# f"example_images/{gallery_list.root[selected_state.index].image.orig_name}",
|
| 34 |
-
# "",
|
| 35 |
-
# )
|
| 36 |
return f"example_images/{gallery_list.root[selected_state.index].image.orig_name}"
|
| 37 |
|
| 38 |
-
def expand_layout():
|
| 39 |
-
return gr.Column(scale=2), gr.Textbox()
|
| 40 |
-
|
| 41 |
def render_webpage(
|
| 42 |
html_css_code,
|
| 43 |
):
|
|
@@ -188,23 +189,18 @@ def model_inference(
|
|
| 188 |
rendered_page = render_webpage(CAR_COMPNAY)
|
| 189 |
return CAR_COMPNAY, rendered_page
|
| 190 |
|
| 191 |
-
# textbox = gr.Textbox(
|
| 192 |
-
# placeholder="Upload an image and ask the AI to create a meme!",
|
| 193 |
-
# show_label=False,
|
| 194 |
-
# value="Write a meme about this image.",
|
| 195 |
-
# visible=True,
|
| 196 |
-
# container=False,
|
| 197 |
-
# label="Text input",
|
| 198 |
-
# scale=8,
|
| 199 |
-
# max_lines=5,
|
| 200 |
-
# )
|
| 201 |
|
| 202 |
-
generated_html = gr.
|
| 203 |
-
label="
|
| 204 |
elem_id="generated_html",
|
| 205 |
)
|
| 206 |
rendered_html = gr.Image(
|
|
|
|
| 207 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
css = """
|
| 210 |
.gradio-container{max-width: 1000px!important}
|
|
@@ -212,12 +208,15 @@ h1{display: flex;align-items: center;justify-content: center;gap: .25em}
|
|
| 212 |
*{transition: width 0.5s ease, flex-grow 0.5s ease}
|
| 213 |
"""
|
| 214 |
|
|
|
|
| 215 |
with gr.Blocks(title="Img2html", theme=gr.themes.Base(), css=css) as demo:
|
| 216 |
with gr.Row(equal_height=True):
|
| 217 |
-
# scale=2 when expanded
|
| 218 |
with gr.Column(scale=4, min_width=250) as upload_area:
|
| 219 |
imagebox = gr.Image(
|
| 220 |
-
type="filepath",
|
|
|
|
|
|
|
|
|
|
| 221 |
)
|
| 222 |
with gr.Group():
|
| 223 |
with gr.Row():
|
|
@@ -230,11 +229,13 @@ with gr.Blocks(title="Img2html", theme=gr.themes.Base(), css=css) as demo:
|
|
| 230 |
regenerate_btn = gr.Button(
|
| 231 |
value="🔄 Regenerate", visible=True, min_width=120
|
| 232 |
)
|
| 233 |
-
with gr.Column(scale=
|
| 234 |
rendered_html.render()
|
|
|
|
| 235 |
with gr.Row():
|
| 236 |
generated_html.render()
|
| 237 |
-
|
|
|
|
| 238 |
template_gallery = gr.Gallery(
|
| 239 |
value=IMAGE_GALLERY_PATHS,
|
| 240 |
label="Templates Gallery",
|
|
@@ -253,9 +254,7 @@ with gr.Blocks(title="Img2html", theme=gr.themes.Base(), css=css) as demo:
|
|
| 253 |
regenerate_btn.click,
|
| 254 |
],
|
| 255 |
fn=model_inference,
|
| 256 |
-
inputs=[
|
| 257 |
-
imagebox,
|
| 258 |
-
],
|
| 259 |
outputs=[generated_html, rendered_html],
|
| 260 |
queue=False,
|
| 261 |
)
|
|
@@ -273,11 +272,7 @@ with gr.Blocks(title="Img2html", theme=gr.themes.Base(), css=css) as demo:
|
|
| 273 |
outputs=[imagebox],
|
| 274 |
queue=False,
|
| 275 |
)
|
| 276 |
-
demo.load(
|
| 277 |
-
|
| 278 |
-
# inputs=[gallery_type_choice],
|
| 279 |
-
# outputs=[template_gallery],
|
| 280 |
-
queue=False,
|
| 281 |
-
)
|
| 282 |
demo.queue(max_size=40, api_open=False)
|
| 283 |
demo.launch(max_threads=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
|
| 4 |
+
from playwright.sync_api import sync_playwright
|
| 5 |
+
from typing import List
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
import gradio as gr
|
| 9 |
from gradio_client.client import DEFAULT_TEMP_DIR
|
| 10 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 11 |
+
|
| 12 |
+
|
| 13 |
API_TOKEN = os.getenv("HF_AUTH_TOKEN")
|
| 14 |
# PROCESSOR = AutoProcessor.from_pretrained(
|
| 15 |
# "HuggingFaceM4/img2html",
|
| 16 |
# token=API_TOKEN,
|
| 17 |
# )
|
| 18 |
+
# MODEL = AutoModelForCausalLM.from_pretrained(
|
| 19 |
+
# "HuggingFaceM4/img2html",
|
| 20 |
+
# token=API_TOKEN,
|
| 21 |
+
# )
|
| 22 |
+
|
| 23 |
|
| 24 |
IMAGE_GALLERY_PATHS = [
|
| 25 |
f"example_images/{ex_image}"
|
| 26 |
for ex_image in os.listdir(f"example_images")
|
| 27 |
]
|
| 28 |
|
|
|
|
| 29 |
|
| 30 |
def install_playwright():
|
| 31 |
try:
|
|
|
|
| 34 |
except subprocess.CalledProcessError as e:
|
| 35 |
print(f"Error during Playwright installation: {e}")
|
| 36 |
|
|
|
|
| 37 |
install_playwright()
|
| 38 |
|
| 39 |
def add_file_gallery(selected_state: gr.SelectData, gallery_list: List[str]):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return f"example_images/{gallery_list.root[selected_state.index].image.orig_name}"
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
def render_webpage(
|
| 43 |
html_css_code,
|
| 44 |
):
|
|
|
|
| 189 |
rendered_page = render_webpage(CAR_COMPNAY)
|
| 190 |
return CAR_COMPNAY, rendered_page
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
+
generated_html = gr.Code(
|
| 194 |
+
label="Extracted HTML",
|
| 195 |
elem_id="generated_html",
|
| 196 |
)
|
| 197 |
rendered_html = gr.Image(
|
| 198 |
+
label="Rendered HTML"
|
| 199 |
)
|
| 200 |
+
# rendered_html = gr.HTML(
|
| 201 |
+
# label="Rendered HTML"
|
| 202 |
+
# )
|
| 203 |
+
|
| 204 |
|
| 205 |
css = """
|
| 206 |
.gradio-container{max-width: 1000px!important}
|
|
|
|
| 208 |
*{transition: width 0.5s ease, flex-grow 0.5s ease}
|
| 209 |
"""
|
| 210 |
|
| 211 |
+
|
| 212 |
with gr.Blocks(title="Img2html", theme=gr.themes.Base(), css=css) as demo:
|
| 213 |
with gr.Row(equal_height=True):
|
|
|
|
| 214 |
with gr.Column(scale=4, min_width=250) as upload_area:
|
| 215 |
imagebox = gr.Image(
|
| 216 |
+
type="filepath",
|
| 217 |
+
label="Screenshot to extract",
|
| 218 |
+
visible=True,
|
| 219 |
+
sources=["upload", "clipboard"],
|
| 220 |
)
|
| 221 |
with gr.Group():
|
| 222 |
with gr.Row():
|
|
|
|
| 229 |
regenerate_btn = gr.Button(
|
| 230 |
value="🔄 Regenerate", visible=True, min_width=120
|
| 231 |
)
|
| 232 |
+
with gr.Column(scale=4) as result_area:
|
| 233 |
rendered_html.render()
|
| 234 |
+
|
| 235 |
with gr.Row():
|
| 236 |
generated_html.render()
|
| 237 |
+
|
| 238 |
+
with gr.Row():
|
| 239 |
template_gallery = gr.Gallery(
|
| 240 |
value=IMAGE_GALLERY_PATHS,
|
| 241 |
label="Templates Gallery",
|
|
|
|
| 254 |
regenerate_btn.click,
|
| 255 |
],
|
| 256 |
fn=model_inference,
|
| 257 |
+
inputs=[imagebox],
|
|
|
|
|
|
|
| 258 |
outputs=[generated_html, rendered_html],
|
| 259 |
queue=False,
|
| 260 |
)
|
|
|
|
| 272 |
outputs=[imagebox],
|
| 273 |
queue=False,
|
| 274 |
)
|
| 275 |
+
demo.load(queue=False)
|
| 276 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
demo.queue(max_size=40, api_open=False)
|
| 278 |
demo.launch(max_threads=400)
|