Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,201 +1,126 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import spaces
|
| 3 |
-
from transformers import AutoModel, AutoTokenizer
|
| 4 |
import os
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
import
|
| 9 |
-
import
|
| 10 |
from pathlib import Path
|
|
|
|
| 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 |
-
def
|
| 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 |
-
def
|
| 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 |
-
"format texts OCR",
|
| 130 |
-
"plain multi-crop OCR",
|
| 131 |
-
"format multi-crop OCR",
|
| 132 |
-
"plain fine-grained OCR",
|
| 133 |
-
"format fine-grained OCR",
|
| 134 |
-
],
|
| 135 |
-
label="Choose one mode of GOT",
|
| 136 |
-
value="plain texts OCR"
|
| 137 |
-
)
|
| 138 |
-
fine_grained_dropdown = gr.Dropdown(
|
| 139 |
-
choices=["box", "color"],
|
| 140 |
-
label="fine-grained type",
|
| 141 |
-
visible=False
|
| 142 |
-
)
|
| 143 |
-
color_dropdown = gr.Dropdown(
|
| 144 |
-
choices=["red", "green", "blue"],
|
| 145 |
-
label="color list",
|
| 146 |
-
visible=False
|
| 147 |
-
)
|
| 148 |
-
box_input = gr.Textbox(
|
| 149 |
-
label="input box: [x1,y1,x2,y2]",
|
| 150 |
-
placeholder="e.g., [0,0,100,100]",
|
| 151 |
-
visible=False
|
| 152 |
-
)
|
| 153 |
-
submit_button = gr.Button("Submit")
|
| 154 |
-
|
| 155 |
-
with gr.Column():
|
| 156 |
-
ocr_result = gr.Textbox(label="GOT output")
|
| 157 |
-
|
| 158 |
-
with gr.Column():
|
| 159 |
-
gr.Markdown("**If you choose the mode with format, the mathpix result will be automatically rendered as follows:**")
|
| 160 |
-
html_result = gr.HTML(label="rendered html", show_label=True)
|
| 161 |
-
|
| 162 |
-
# Removed examples section
|
| 163 |
-
"""
|
| 164 |
-
gr.Examples(
|
| 165 |
-
examples=[
|
| 166 |
-
["assets/coco.jpg", "plain texts OCR", "", "", ""],
|
| 167 |
-
["assets/en_30.png", "plain texts OCR", "", "", ""],
|
| 168 |
-
["assets/table.jpg", "format texts OCR", "", "", ""],
|
| 169 |
-
["assets/eq.jpg", "format texts OCR", "", "", ""],
|
| 170 |
-
["assets/exam.jpg", "format texts OCR", "", "", ""],
|
| 171 |
-
["assets/giga.jpg", "format multi-crop OCR", "", "", ""],
|
| 172 |
-
["assets/aff2.png", "plain fine-grained OCR", "box", "", "[409,763,756,891]"],
|
| 173 |
-
["assets/color.png", "plain fine-grained OCR", "color", "red", ""],
|
| 174 |
-
],
|
| 175 |
-
inputs=[image_input, task_dropdown, fine_grained_dropdown, color_dropdown, box_input],
|
| 176 |
-
outputs=[ocr_result, html_result],
|
| 177 |
-
fn=run_GOT,
|
| 178 |
-
label="examples",
|
| 179 |
-
)
|
| 180 |
-
"""
|
| 181 |
-
|
| 182 |
-
task_dropdown.change(
|
| 183 |
-
task_update,
|
| 184 |
-
inputs=[task_dropdown],
|
| 185 |
-
outputs=[fine_grained_dropdown, color_dropdown, box_input]
|
| 186 |
-
)
|
| 187 |
-
fine_grained_dropdown.change(
|
| 188 |
-
fine_grained_update,
|
| 189 |
-
inputs=[fine_grained_dropdown],
|
| 190 |
-
outputs=[color_dropdown, box_input]
|
| 191 |
-
)
|
| 192 |
-
|
| 193 |
-
submit_button.click(
|
| 194 |
-
run_GOT,
|
| 195 |
-
inputs=[image_input, task_dropdown, fine_grained_dropdown, color_dropdown, box_input],
|
| 196 |
-
outputs=[ocr_result, html_result]
|
| 197 |
-
)
|
| 198 |
-
|
| 199 |
-
if __name__ == "__main__":
|
| 200 |
-
cleanup_old_files()
|
| 201 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import copy
|
| 3 |
+
import tempfile
|
| 4 |
+
import requests
|
| 5 |
+
import re
|
| 6 |
+
from argparse import ArgumentParser
|
| 7 |
from pathlib import Path
|
| 8 |
+
from byaldi import RAGMultiModalModel
|
| 9 |
|
| 10 |
+
API_KEY = os.environ['API_KEY']
|
| 11 |
+
RAG = RAGMultiModalModel.from_pretrained("vidore/colpali-v1.2")
|
| 12 |
+
|
| 13 |
+
def _get_args():
|
| 14 |
+
parser = ArgumentParser()
|
| 15 |
+
parser.add_argument("--share", action="store_true", default=False)
|
| 16 |
+
args = parser.parse_args()
|
| 17 |
+
return args
|
| 18 |
+
|
| 19 |
+
def _parse_text(text):
|
| 20 |
+
lines = text.split("\n")
|
| 21 |
+
lines = [line for line in lines if line]
|
| 22 |
+
count = 0
|
| 23 |
+
for i, line in enumerate(lines):
|
| 24 |
+
if "```" in line:
|
| 25 |
+
count += 1
|
| 26 |
+
items = line.split("`")
|
| 27 |
+
lines[i] = f'<pre><code class="language-{items[-1]}">' if count % 2 == 1 else "<br></code></pre>"
|
| 28 |
+
elif count % 2 == 1:
|
| 29 |
+
lines[i] = "<br>" + re.sub(r'[<>\*_\-.\!\(\)\$]', lambda x: f'&{x.group(0)};', line.replace(" ", " "))
|
| 30 |
+
return "".join(lines)
|
| 31 |
+
|
| 32 |
+
def _remove_image_special(text):
|
| 33 |
+
text = text.replace('<ref>', '').replace('</ref>', '')
|
| 34 |
+
return re.sub(r'<box>.*?(</box>|$)', '', text)
|
| 35 |
+
|
| 36 |
+
def _launch_demo(args):
|
| 37 |
+
uploaded_file_dir = os.environ.get("GRADIO_TEMP_DIR") or str(Path(tempfile.gettempdir()) / "gradio")
|
| 38 |
+
|
| 39 |
+
def predict(_chatbot, task_history):
|
| 40 |
+
chat_query = _chatbot[-1][0]
|
| 41 |
+
query = task_history[-1][0]
|
| 42 |
+
if not chat_query:
|
| 43 |
+
_chatbot.pop()
|
| 44 |
+
task_history.pop()
|
| 45 |
+
return _chatbot
|
| 46 |
+
|
| 47 |
+
history_cp = copy.deepcopy(task_history)
|
| 48 |
+
messages = []
|
| 49 |
+
content = []
|
| 50 |
+
|
| 51 |
+
for q, a in history_cp:
|
| 52 |
+
content.append({'image': f'file://{q[0]}'})
|
| 53 |
+
messages.append({'role': 'user', 'content': content})
|
| 54 |
+
messages.append({'role': 'assistant', 'content': [{'text': a}]})
|
| 55 |
+
content = []
|
| 56 |
+
messages.pop()
|
| 57 |
+
|
| 58 |
+
responses = RAG.call(model='qwen-vl-max-0809', messages=messages, stream=True)
|
| 59 |
+
for response in responses:
|
| 60 |
+
response_content = response['output']['choices'][0]['message']['content']
|
| 61 |
+
response_text = ''.join(ele.get('text', ele.get('box', '')) for ele in response_content)
|
| 62 |
+
_chatbot[-1] = (_parse_text(chat_query), _remove_image_special(response_text))
|
| 63 |
+
yield _chatbot
|
| 64 |
+
|
| 65 |
+
response_text = response_content[0]['text']
|
| 66 |
+
_chatbot[-1] = (_parse_text(chat_query), response_text)
|
| 67 |
+
task_history[-1] = (query, _parse_text(response_text))
|
| 68 |
+
|
| 69 |
+
def regenerate(_chatbot, task_history):
|
| 70 |
+
if not task_history:
|
| 71 |
+
return _chatbot
|
| 72 |
+
item = task_history[-1]
|
| 73 |
+
if item[1] is None:
|
| 74 |
+
return _chatbot
|
| 75 |
+
task_history[-1] = (item[0], None)
|
| 76 |
+
chatbot_item = _chatbot.pop(-1)
|
| 77 |
+
_chatbot.append((chatbot_item[0], None) if chatbot_item[0] is not None else (_chatbot[-1][0], None))
|
| 78 |
+
return predict(_chatbot, task_history)
|
| 79 |
+
|
| 80 |
+
def add_text(history, task_history, text):
|
| 81 |
+
task_text = text
|
| 82 |
+
history = history if history is not None else []
|
| 83 |
+
task_history = task_history if task_history is not None else []
|
| 84 |
+
history.append((_parse_text(text), None))
|
| 85 |
+
task_history.append((task_text, None))
|
| 86 |
+
return history, task_history, ""
|
| 87 |
+
|
| 88 |
+
def add_file(history, task_history, file):
|
| 89 |
+
history = history if history is not None else []
|
| 90 |
+
task_history = task_history if task_history is not None else []
|
| 91 |
+
history.append(((file.name,), None))
|
| 92 |
+
task_history.append(((file.name,), None))
|
| 93 |
+
return history, task_history
|
| 94 |
+
|
| 95 |
+
def reset_state(task_history):
|
| 96 |
+
task_history.clear()
|
| 97 |
+
return []
|
| 98 |
+
|
| 99 |
+
with gr.Blocks() as demo:
|
| 100 |
+
gr.Markdown("""<p align="center"><img src="https://modelscope.oss-cn-beijing.aliyuncs.com/resource/qwen.png" style="height: 80px"/><p>""")
|
| 101 |
+
gr.Markdown("<center><font size=8>Qwen2-VL-Max</center>")
|
| 102 |
+
|
| 103 |
+
chatbot = gr.Chatbot(label='Qwen2-VL-Max', height=500)
|
| 104 |
+
query = gr.Textbox(lines=2, label='Input')
|
| 105 |
+
task_history = gr.State([])
|
| 106 |
+
|
| 107 |
+
with gr.Row():
|
| 108 |
+
addfile_btn = gr.UploadButton("๐ Upload", file_types=["image"])
|
| 109 |
+
submit_btn = gr.Button("๐ Submit")
|
| 110 |
+
regen_btn = gr.Button("๐ค๏ธ Regenerate")
|
| 111 |
+
empty_bin = gr.Button("๐งน Clear History")
|
| 112 |
+
|
| 113 |
+
submit_btn.click(add_text, [chatbot, task_history, query], [chatbot, task_history]).then(predict, [chatbot, task_history], [chatbot], show_progress=True)
|
| 114 |
+
submit_btn.click(lambda: gr.update(value=""), [], [query])
|
| 115 |
+
empty_bin.click(reset_state, [task_history], [chatbot], show_progress=True)
|
| 116 |
+
regen_btn.click(regenerate, [chatbot, task_history], [chatbot], show_progress=True)
|
| 117 |
+
addfile_btn.upload(add_file, [chatbot, task_history, addfile_btn], [chatbot, task_history], show_progress=True)
|
| 118 |
+
|
| 119 |
+
demo.queue().launch(share=args.share)
|
| 120 |
+
|
| 121 |
+
def main():
|
| 122 |
+
args = _get_args()
|
| 123 |
+
_launch_demo(args)
|
| 124 |
+
|
| 125 |
+
if __name__ == '__main__':
|
| 126 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|