Spaces:
Runtime error
Runtime error
File size: 5,722 Bytes
3a67079 b51fb19 492d9c6 b51fb19 1d2e98e b51fb19 5a73c06 66f0272 1d2e98e 66f0272 3a67079 ee63313 3a67079 66f0272 3a67079 1d2e98e 492d9c6 1d2e98e 66f0272 3a67079 |
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 |
import os
import torch
import argparse
import gradio as gr
import spaces
from utils import Image2Text
@spaces.GPU(duration=60)
def predict(input_img):
global image_to_text
contents = image_to_text.get_text(input_img, num_beams=4)
return contents
def get_image_path_list(folder_name):
image_basename_list = os.listdir(folder_name)
image_path_list = sorted([os.path.join(folder_name, basename) for basename in image_basename_list])
return image_path_list
def swap_to_gallery(images):
return gr.update(value=images, visible=True)
def swap_to_gallery(images):
return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
def remove_back_to_files():
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
def upload_example_to_gallery(images):
return gr.update(value=images, visible=True)
def get_example():
return ['./samples/sample1.jpg', './samples/sample2.jpg']
examples_path = os.path.dirname(__file__)
title = r"""
<h1 align="center">π© for Hwp math problems</h1>
<div align="center">
<h3>
I have launched a service that includes HWP automation.<br>
Automatically convert the processed images into HWP documents. <a href="https://hwpmath.duckdns.org/">[Try now! π€]</a>
</h3>
</div>
"""
description = r"""
<b>Official π€ Gradio demo</b> for <a href='https://github.com/sooooner/DonutMathHWP' target='_blank'><b>DonutMathHWP</b></a>.<br>
<br>
βοΈ[<b>Important</b>] HWP Conversion Steps:<br>
1οΈβ£ <b>Image Upload</b>: Upload an image containing a math problem or solution, including any mathematical formulas. Note that text recognition for problems is not included, so please ensure the math content <b>occupies the majority of the image</b>.<br>
2οΈβ£ <b>Start Conversion</b>: Click the <b>Submit</b> button to initiate the conversion.<br>
3οΈβ£ <b>Conversion Output</b>: In the converted output, <b>text enclosed within $ symbols</b> represents mathematical formulas. When using the output in an HWP document, treat all segments surrounded by $ symbols as formulas for accurate formatting.<br>
"""
example = r"""
<div>
<h3>π‘ <b>Example Images:</b> Sample Images of Math Problems and Solutions</h3>
<b>Problem:</b> The image should include a math problem with relevant formulas or symbols, taking up most of the image area for clear recognition.
<b>Solution:</b> The image should show solution steps with formulas and calculations, occupying most of the space and in sequential order.
</div>
"""
article = r"""<br>
π§ <b>Contact</b>
<br>
If you have any questions, please feel free to reach me out at <b>tnsgh0101@gmail.com</b>
"""
css = """
.gradio-container {
width: 85% !important
}
.highlighted-examples {
border: 4px solid #FFD700;
padding: 20px;
margin: 30px 0;
background-color: #FFFACD;
border-radius: 15px;
font-size: 1.2em;
}
.highlighted-examples:hover {
box-shadow: 0 0 25px rgba(255, 215, 0, 0.9);
cursor: pointer;
transform: scale(1.02);
}
"""
if __name__ == "__main__":
repo_id = os.getenv('MODEL_REPO_ID')
hf_token = os.environ.get("HF_TOKEN")
device = "cuda"
image_to_text = Image2Text(repo_id, hf_token=hf_token, device=device)
with gr.Blocks(css=css) as demo:
gr.Markdown(title)
gr.Markdown(description)
with gr.Row():
with gr.Column():
files = gr.File(
label="Drag (Select) 1 or more photos",
file_types=["image"],
file_count="multiple",
visible=True
)
uploaded_files = gr.Gallery(show_label=False, visible=False, columns=2)
with gr.Column(visible=False) as clear_button:
remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
submit = gr.Button("Submit")
with gr.Column():
extracted_texts_output = gr.JSON(label="Extracted Texts")
files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
submit.click(
fn=predict,
inputs=files,
outputs=extracted_texts_output
)
# gr.Examples(
# examples=[[get_image_path_list('./samples/problems')], [get_image_path_list('./samples/solutions')]],
# # examples=[
# # (get_image_path_list('./samples/problems'), "Problems"),
# # (get_image_path_list('./samples/solutions'), "Solutions")
# # ],
# fn=swap_to_gallery,
# run_on_click=True,
# inputs=files,
# outputs=[uploaded_files, clear_button, files],
# cache_examples=True,
# label="π‘ Example Images",
# elem_id="highlighted-examples"
# )
gr.Markdown(example)
gr.Examples(
examples=[[get_image_path_list('./samples/problems')], [get_image_path_list('./samples/solutions')]],
inputs=files,
run_on_click=True,
outputs=[uploaded_files, clear_button, files],
fn=swap_to_gallery,
cache_examples=True,
label=r"Select a sample images of the problem or solution",
elem_id="highlighted-examples"
)
gr.Markdown(article)
demo.launch() |