Spaces:
Running on Zero
Running on Zero
Commit ·
b537793
1
Parent(s): 2096b5f
update
Browse files123456789101112131415161718192021222 unmodified lines25262728293031323334353637383940414243144 unmodified lines1881891901911921931941951961971981992002012022032042052062072082092101 unmodified line2122132142152162172182192202212222236 unmodified lines23023123223323423523623723823924012 unmodified lines25325425525625725825926026111 unmodified lines273274275276277278279280281282283from typing import List, Optional, Tuplefrom PIL import Imagefrom playwright.sync_api import sync_playwrightimport osimport gradio as grfrom gradio_client.client import DEFAULT_TEMP_DIRfrom transformers import AutoProcessor, AutoModelForCausalLMAPI_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 subprocessdef install_playwright():try:2 unmodified linesexcept subprocess.CalledProcessError as e:print(f"Error during Playwright installation: {e}")# Call the function to install Playwrightinstall_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,):144 unmodified linesrendered_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}1 unmodified line*{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 expandedwith 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():6 unmodified linesregenerate_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",12 unmodified linesregenerate_btn.click,],fn=model_inference,inputs=[imagebox,],outputs=[generated_html, rendered_html],queue=False,)11 unmodified linesoutputs=[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)123456789101112131415161718192021222324252627282930312 unmodified lines3435363738394041424344144 unmodified lines1891901911921931941951961971981992002012022032042052061 unmodified line2082092102112122132142152162172182192202212226 unmodified lines22923023123223323423523623723823924024112 unmodified lines25425525625725825926011 unmodified lines272273274275276277278import osimport subprocessfrom playwright.sync_api import sync_playwrightfrom typing import Listfrom PIL import Imageimport gradio as grfrom gradio_client.client import DEFAULT_TEMP_DIRfrom transformers import AutoProcessor, AutoModelForCausalLMAPI_TOKEN = os.getenv("HF_AUTH_TOKEN")# PROCESSOR = AutoProcessor.from_pretrained(# "HuggingFaceM4/img2html",# token=API_TOKEN,# )# MODEL = AutoModelForCausalLM.from_pretrained(# "HuggingFaceM4/img2html",# token=API_TOKEN,# )IMAGE_GALLERY_PATHS = [f"example_images/{ex_image}"for ex_image in os.listdir(f"example_images")]def install_playwright():try:2 unmodified linesexcept subprocess.CalledProcessError as e:print(f"Error during Playwright installation: {e}")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}"def render_webpage(html_css_code,):144 unmodified linesrendered_page = render_webpage(CAR_COMPNAY)return CAR_COMPNAY, rendered_pagegenerated_html = gr.Code(label="Extracted HTML",elem_id="generated_html",)rendered_html = gr.Image(label="Rendered HTML")# rendered_html = gr.HTML(# label="Rendered HTML"# )css = """.gradio-container{max-width: 1000px!important}1 unmodified line*{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):with gr.Column(scale=4, min_width=250) as upload_area:imagebox = gr.Image(type="filepath",label="Screenshot to extract",visible=True,sources=["upload", "clipboard"],)with gr.Group():with gr.Row():6 unmodified linesregenerate_btn = gr.Button(value="🔄 Regenerate", visible=True, min_width=120)with gr.Column(scale=4) as result_area:rendered_html.render()with gr.Row():generated_html.render()with gr.Row():template_gallery = gr.Gallery(value=IMAGE_GALLERY_PATHS,label="Templates Gallery",12 unmodified linesregenerate_btn.click,],fn=model_inference,inputs=[imagebox],outputs=[generated_html, rendered_html],queue=False,)11 unmodified linesoutputs=[imagebox],queue=False,)demo.load(queue=False)demo.queue(max_size=40, api_open=False)demo.launch(max_threads=400)
app.py+32-37