Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,122 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
)
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
"""
|
| 43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 44 |
-
"""
|
| 45 |
-
demo = gr.ChatInterface(
|
| 46 |
-
respond,
|
| 47 |
-
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
-
gr.Slider(
|
| 52 |
-
minimum=0.1,
|
| 53 |
-
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
-
step=0.05,
|
| 56 |
-
label="Top-p (nucleus sampling)",
|
| 57 |
-
),
|
| 58 |
-
],
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
if __name__ == "__main__":
|
| 63 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
from transformers import AutoModel, AutoTokenizer
|
| 4 |
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True)
|
| 6 |
+
model = AutoModel.from_pretrained('ucaslcl/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, device_map='cuda', use_safetensors=True)
|
| 7 |
+
model = model.eval().cuda()
|
|
|
|
| 8 |
|
| 9 |
+
@spaces.GPU
|
| 10 |
+
def process_image(image, task, ocr_type=None, ocr_box=None, ocr_color=None, render=True):
|
| 11 |
+
if task == "Plain Text OCR":
|
| 12 |
+
res = model.chat(tokenizer, image, ocr_type='ocr')
|
| 13 |
+
elif task == "Format Text OCR":
|
| 14 |
+
res = model.chat(tokenizer, image, ocr_type='format')
|
| 15 |
+
elif task == "Fine-grained OCR (Box)":
|
| 16 |
+
res = model.chat(tokenizer, image, ocr_type=ocr_type, ocr_box=ocr_box)
|
| 17 |
+
elif task == "Fine-grained OCR (Color)":
|
| 18 |
+
res = model.chat(tokenizer, image, ocr_type=ocr_type, ocr_color=ocr_color)
|
| 19 |
+
elif task == "Multi-crop OCR":
|
| 20 |
+
res = model.chat_crop(tokenizer, image_file=image)
|
| 21 |
+
elif task == "Render Formatted OCR":
|
| 22 |
+
res = model.chat(tokenizer, image, ocr_type='format', render=True, save_render_file='./demo.html')
|
| 23 |
+
with open('./demo.html', 'r') as f:
|
| 24 |
+
html_content = f.read()
|
| 25 |
+
return res, html_content
|
| 26 |
+
|
| 27 |
+
return res, None
|
| 28 |
|
| 29 |
+
def update_inputs(task):
|
| 30 |
+
if task == "Plain Text OCR" or task == "Format Text OCR" or task == "Multi-crop OCR":
|
| 31 |
+
return [gr.update(visible=False)] * 4
|
| 32 |
+
elif task == "Fine-grained OCR (Box)":
|
| 33 |
+
return [
|
| 34 |
+
gr.update(visible=True, choices=["ocr", "format"]),
|
| 35 |
+
gr.update(visible=True),
|
| 36 |
+
gr.update(visible=False),
|
| 37 |
+
gr.update(visible=False)
|
| 38 |
+
]
|
| 39 |
+
elif task == "Fine-grained OCR (Color)":
|
| 40 |
+
return [
|
| 41 |
+
gr.update(visible=True, choices=["ocr", "format"]),
|
| 42 |
+
gr.update(visible=False),
|
| 43 |
+
gr.update(visible=True, choices=["red", "green", "blue"]),
|
| 44 |
+
gr.update(visible=False)
|
| 45 |
+
]
|
| 46 |
+
elif task == "Render Formatted OCR":
|
| 47 |
+
return [gr.update(visible=False)] * 3 + [gr.update(visible=True)]
|
| 48 |
+
|
| 49 |
+
def ocr_demo(image, task, ocr_type, ocr_box, ocr_color, render):
|
| 50 |
+
res, html_content = process_image(image, task, ocr_type, ocr_box, ocr_color, render)
|
| 51 |
+
if html_content:
|
| 52 |
+
return res, html_content
|
| 53 |
+
return res, None
|
| 54 |
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown()
|
| 57 |
+
gr.Markdown()
|
| 58 |
+
gr.Markdown("""
|
| 59 |
+
# "General OCR Theory: Towards OCR-2.0 via a Unified End-to-end Model"
|
| 60 |
+
|
| 61 |
+
"🔥🔥🔥This is the official online demo of GOT-OCR-2.0 model!!!"
|
| 62 |
+
|
| 63 |
+
### Repo
|
| 64 |
+
- **Hugging Face**: [ucaslcl/GOT-OCR2_0](https://huggingface.co/ucaslcl/GOT-OCR2_0)
|
| 65 |
+
- **GitHub**: [ucaslcl/GOT-OCR2_0](https://github.com/Ucas-HaoranWei/GOT-OCR2.0/)
|
| 66 |
+
- **Paper**: [AriXiv](https://arxiv.org/abs/2409.01704)
|
| 67 |
+
""")
|
| 68 |
+
|
| 69 |
+
with gr.Row():
|
| 70 |
+
with gr.Column():
|
| 71 |
+
image_input = gr.Image(type="filepath", label="upload your image")
|
| 72 |
+
task_dropdown = gr.Dropdown(
|
| 73 |
+
choices=[
|
| 74 |
+
"plain text OCR",
|
| 75 |
+
"format text OCR",
|
| 76 |
+
"fine-grained OCR (Box)",
|
| 77 |
+
"fine-grained OCR (Color)",
|
| 78 |
+
"multi-crop OCR",
|
| 79 |
+
"render Formatted OCR"
|
| 80 |
+
],
|
| 81 |
+
label="Select Task",
|
| 82 |
+
value="Plain Text OCR"
|
| 83 |
+
)
|
| 84 |
+
ocr_type_dropdown = gr.Dropdown(
|
| 85 |
+
choices=["ocr", "format"],
|
| 86 |
+
label="OCR Type",
|
| 87 |
+
visible=False
|
| 88 |
+
)
|
| 89 |
+
ocr_box_input = gr.Textbox(
|
| 90 |
+
label="OCR Box (x1,y1,x2,y2)",
|
| 91 |
+
placeholder="e.g., 100,100,200,200",
|
| 92 |
+
visible=False
|
| 93 |
+
)
|
| 94 |
+
ocr_color_dropdown = gr.Dropdown(
|
| 95 |
+
choices=["red", "green", "blue"],
|
| 96 |
+
label="OCR Color",
|
| 97 |
+
visible=False
|
| 98 |
+
)
|
| 99 |
+
render_checkbox = gr.Checkbox(
|
| 100 |
+
label="Render Result",
|
| 101 |
+
visible=False
|
| 102 |
+
)
|
| 103 |
+
submit_button = gr.Button("Process")
|
| 104 |
+
|
| 105 |
+
with gr.Column():
|
| 106 |
+
output_text = gr.Textbox(label="OCR Result")
|
| 107 |
+
output_html = gr.HTML(label="Rendered HTML Output")
|
| 108 |
+
|
| 109 |
|
| 110 |
+
task_dropdown.change(
|
| 111 |
+
update_inputs,
|
| 112 |
+
inputs=[task_dropdown],
|
| 113 |
+
outputs=[ocr_type_dropdown, ocr_box_input, ocr_color_dropdown, render_checkbox]
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
submit_button.click(
|
| 117 |
+
ocr_demo,
|
| 118 |
+
inputs=[image_input, task_dropdown, ocr_type_dropdown, ocr_box_input, ocr_color_dropdown],
|
| 119 |
+
outputs=[output_text, output_html]
|
| 120 |
+
)
|
| 121 |
|
| 122 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|