Spaces:
Runtime error
Runtime error
- __pycache__/utils.cpython-39.pyc +0 -0
- app.py +142 -23
- samples/problems/problem1.png +0 -0
- samples/problems/problem2.png +0 -0
- samples/problems/problem3.png +0 -0
- samples/sample1.png +0 -0
- samples/sample2.png +0 -0
- samples/sample3.png +0 -0
- samples/solutions/solution1.png +0 -0
- samples/solutions/solution2.png +0 -0
- samples/solutions/solution3.png +0 -0
__pycache__/utils.cpython-39.pyc
CHANGED
|
Binary files a/__pycache__/utils.cpython-39.pyc and b/__pycache__/utils.cpython-39.pyc differ
|
|
|
app.py
CHANGED
|
@@ -4,34 +4,153 @@ import argparse
|
|
| 4 |
import gradio as gr
|
| 5 |
import spaces
|
| 6 |
|
| 7 |
-
from utils import Image2Text
|
| 8 |
|
| 9 |
@spaces.GPU(duration=60)
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
examples_path = os.path.dirname(__file__)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
if __name__ == "__main__":
|
| 18 |
-
repo_id = os.getenv('MODEL_REPO_ID')
|
| 19 |
-
hf_token = os.environ.get("HF_TOKEN")
|
| 20 |
|
| 21 |
-
device = "cuda"
|
| 22 |
-
image_to_text = Image2Text(repo_id, hf_token=hf_token, device=device)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
demo.launch()
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import spaces
|
| 6 |
|
| 7 |
+
# from utils import Image2Text
|
| 8 |
|
| 9 |
@spaces.GPU(duration=60)
|
| 10 |
+
def predict(input_img):
|
| 11 |
+
return {'0': 'hellow', '1': 'world'}
|
| 12 |
+
# global image_to_text
|
| 13 |
+
# contents = image_to_text.get_text(input_img, num_beams=4)
|
| 14 |
+
# return contents
|
| 15 |
+
|
| 16 |
+
def get_image_path_list(folder_name):
|
| 17 |
+
image_basename_list = os.listdir(folder_name)
|
| 18 |
+
image_path_list = sorted([os.path.join(folder_name, basename) for basename in image_basename_list])
|
| 19 |
+
return image_path_list
|
| 20 |
+
|
| 21 |
+
def swap_to_gallery(images):
|
| 22 |
+
return gr.update(value=images, visible=True)
|
| 23 |
+
|
| 24 |
+
def swap_to_gallery(images):
|
| 25 |
+
return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
|
| 26 |
+
|
| 27 |
+
def remove_back_to_files():
|
| 28 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
| 29 |
+
|
| 30 |
+
def upload_example_to_gallery(images):
|
| 31 |
+
return gr.update(value=images, visible=True)
|
| 32 |
+
|
| 33 |
+
def get_example():
|
| 34 |
+
return ['./samples/sample1.jpg', './samples/sample2.jpg']
|
| 35 |
|
| 36 |
examples_path = os.path.dirname(__file__)
|
| 37 |
|
| 38 |
+
|
| 39 |
+
title = r"""
|
| 40 |
+
<h1 align="center">π© for Hwp math problems</h1>
|
| 41 |
+
<div align="center">
|
| 42 |
+
<h3>
|
| 43 |
+
I have launched a service that includes HWP automation.<br>
|
| 44 |
+
Automatically convert the processed images into HWP documents. <a href="https://hwpmath.duckdns.org/">[Try now! π€]</a>
|
| 45 |
+
</h3>
|
| 46 |
+
</div>
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
description = r"""
|
| 50 |
+
<b>Official π€ Gradio demo</b> for <a href='https://github.com/sooooner/DonutMathHWP' target='_blank'><b>DonutMathHWP</b></a>.<br>
|
| 51 |
+
<br>
|
| 52 |
+
βοΈ[<b>Important</b>] HWP Conversion Steps:<br>
|
| 53 |
+
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>
|
| 54 |
+
2οΈβ£ <b>Start Conversion</b>: Click the <b>Submit</b> button to initiate the conversion.<br>
|
| 55 |
+
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>
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
example = r"""
|
| 59 |
+
<div>
|
| 60 |
+
<h3>π‘ <b>Example Images:</b> Sample Images of Math Problems and Solutions</h3>
|
| 61 |
+
|
| 62 |
+
<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.
|
| 63 |
+
<b>Solution:</b> The image should show solution steps with formulas and calculations, occupying most of the space and in sequential order.
|
| 64 |
+
</div>
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
article = r"""<br>
|
| 68 |
+
π§ <b>Contact</b>
|
| 69 |
+
<br>
|
| 70 |
+
If you have any questions, please feel free to reach me out at <b>[email protected]</b>
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
css = """
|
| 74 |
+
.gradio-container {
|
| 75 |
+
width: 85% !important
|
| 76 |
+
}
|
| 77 |
+
.highlighted-examples {
|
| 78 |
+
border: 4px solid #FFD700;
|
| 79 |
+
padding: 20px;
|
| 80 |
+
margin: 30px 0;
|
| 81 |
+
background-color: #FFFACD;
|
| 82 |
+
border-radius: 15px;
|
| 83 |
+
font-size: 1.2em;
|
| 84 |
+
}
|
| 85 |
+
.highlighted-examples:hover {
|
| 86 |
+
box-shadow: 0 0 25px rgba(255, 215, 0, 0.9);
|
| 87 |
+
cursor: pointer;
|
| 88 |
+
transform: scale(1.02);
|
| 89 |
+
}
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
if __name__ == "__main__":
|
| 93 |
+
# repo_id = os.getenv('MODEL_REPO_ID')
|
| 94 |
+
# hf_token = os.environ.get("HF_TOKEN")
|
| 95 |
|
| 96 |
+
# device = "cuda"
|
| 97 |
+
# image_to_text = Image2Text(repo_id, hf_token=hf_token, device=device)
|
| 98 |
+
|
| 99 |
+
with gr.Blocks(css=css) as demo:
|
| 100 |
+
gr.Markdown(title)
|
| 101 |
+
gr.Markdown(description)
|
| 102 |
+
with gr.Row():
|
| 103 |
+
with gr.Column():
|
| 104 |
+
files = gr.File(
|
| 105 |
+
label="Drag (Select) 1 or more photos",
|
| 106 |
+
file_types=["image"],
|
| 107 |
+
file_count="multiple",
|
| 108 |
+
visible=True
|
| 109 |
+
)
|
| 110 |
+
uploaded_files = gr.Gallery(show_label=False, visible=False, columns=2)
|
| 111 |
+
with gr.Column(visible=False) as clear_button:
|
| 112 |
+
remove_and_reupload = gr.ClearButton(value="Remove and upload new ones", components=files, size="sm")
|
| 113 |
+
submit = gr.Button("Submit")
|
| 114 |
+
|
| 115 |
+
with gr.Column():
|
| 116 |
+
extracted_texts_output = gr.JSON(label="Extracted Texts")
|
| 117 |
+
|
| 118 |
+
files.upload(fn=swap_to_gallery, inputs=files, outputs=[uploaded_files, clear_button, files])
|
| 119 |
+
remove_and_reupload.click(fn=remove_back_to_files, outputs=[uploaded_files, clear_button, files])
|
| 120 |
+
|
| 121 |
+
submit.click(
|
| 122 |
+
fn=predict,
|
| 123 |
+
inputs=files,
|
| 124 |
+
outputs=extracted_texts_output
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# gr.Examples(
|
| 128 |
+
# examples=[[get_image_path_list('./samples/problems')], [get_image_path_list('./samples/solutions')]],
|
| 129 |
+
# # examples=[
|
| 130 |
+
# # (get_image_path_list('./samples/problems'), "Problems"),
|
| 131 |
+
# # (get_image_path_list('./samples/solutions'), "Solutions")
|
| 132 |
+
# # ],
|
| 133 |
+
# fn=swap_to_gallery,
|
| 134 |
+
# run_on_click=True,
|
| 135 |
+
# inputs=files,
|
| 136 |
+
# outputs=[uploaded_files, clear_button, files],
|
| 137 |
+
# cache_examples=True,
|
| 138 |
+
# label="π‘ Example Images",
|
| 139 |
+
# elem_id="highlighted-examples"
|
| 140 |
+
# )
|
| 141 |
+
|
| 142 |
+
gr.Markdown(example)
|
| 143 |
+
gr.Examples(
|
| 144 |
+
examples=[[get_image_path_list('./samples/problems')], [get_image_path_list('./samples/solutions')]],
|
| 145 |
+
inputs=files,
|
| 146 |
+
run_on_click=True,
|
| 147 |
+
outputs=[uploaded_files, clear_button, files],
|
| 148 |
+
fn=swap_to_gallery,
|
| 149 |
+
cache_examples=True,
|
| 150 |
+
label=r"Select a sample images of the problem or solution",
|
| 151 |
+
elem_id="highlighted-examples"
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
gr.Markdown(article)
|
| 155 |
+
|
| 156 |
demo.launch()
|
samples/problems/problem1.png
ADDED
|
samples/problems/problem2.png
ADDED
|
samples/problems/problem3.png
ADDED
|
samples/sample1.png
DELETED
|
Binary file (12.8 kB)
|
|
|
samples/sample2.png
DELETED
|
Binary file (65.4 kB)
|
|
|
samples/sample3.png
DELETED
|
Binary file (97.9 kB)
|
|
|
samples/solutions/solution1.png
ADDED
|
samples/solutions/solution2.png
ADDED
|
samples/solutions/solution3.png
ADDED
|