Compare-RAG-CHAT / app-backup.py
openfree's picture
Create app-backup.py
72c2546 verified
raw
history blame
14.4 kB
import base64
import json
import os
import time
import zipfile
from pathlib import Path
import re
import uuid
import pymupdf
os.system('pip uninstall -y magic-pdf')
os.system('pip install git+https://github.com/opendatalab/MinerU.git@dev')
os.system('wget https://github.com/opendatalab/MinerU/raw/dev/scripts/download_models_hf.py -O download_models_hf.py')
os.system('python download_models_hf.py')
with open('/home/user/magic-pdf.json', 'r') as file:
data = json.load(file)
data['device-mode'] = "cuda"
if os.getenv('apikey'):
data['llm-aided-config']['title_aided']['api_key'] = os.getenv('apikey')
data['llm-aided-config']['title_aided']['enable'] = True
with open('/home/user/magic-pdf.json', 'w') as file:
json.dump(data, file, indent=4)
os.system('cp -r paddleocr /home/user/.paddleocr')
from gradio_pdf import PDF
import gradio as gr
from loguru import logger
from magic_pdf.data.data_reader_writer import FileBasedDataReader
from magic_pdf.libs.hash_utils import compute_sha256
from magic_pdf.tools.common import do_parse, prepare_env
def create_css():
return """
/* ์ „์ฒด ์Šคํƒ€์ผ */
.gradio-container {
background: linear-gradient(135deg, #EFF6FF 0%, #F5F3FF 100%);
max-width: 1200px !important;
margin: 0 auto !important;
padding: 2rem !important;
}
/* ์ œ๋ชฉ ์Šคํƒ€์ผ */
.title-area {
text-align: center;
margin-bottom: 2rem;
padding: 1rem;
background: white;
border-radius: 1rem;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}
.title-area h1 {
background: linear-gradient(90deg, #2563EB 0%, #7C3AED 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
font-size: 2.5rem;
font-weight: bold;
margin-bottom: 0.5rem;
}
.title-area p {
color: #6B7280;
font-size: 1.1rem;
}
/* ์ปดํฌ๋„ŒํŠธ ์Šคํƒ€์ผ๋ง */
.gr-box, .gr-panel {
border: 2px solid #E0E7FF !important;
border-radius: 12px !important;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;
background: white !important;
}
/* ํŒŒ์ผ ์—…๋กœ๋“œ ์˜์—ญ */
.file-upload {
border: 2px dashed #93C5FD !important;
border-radius: 8px !important;
padding: 2rem !important;
background: #F0F9FF !important;
transition: all 0.3s ease;
}
.file-upload:hover {
background: #E0F2FE !important;
border-color: #60A5FA !important;
}
/* ๋ฒ„ํŠผ ์Šคํƒ€์ผ๋ง */
.gr-button.primary-button {
background: linear-gradient(90deg, #2563EB 0%, #7C3AED 100%) !important;
color: white !important;
border: none !important;
border-radius: 8px !important;
padding: 0.75rem 1.5rem !important;
font-weight: bold !important;
transition: opacity 0.2s !important;
}
.gr-button.primary-button:hover {
opacity: 0.9 !important;
}
.gr-button.secondary-button {
background: white !important;
color: #4B5563 !important;
border: 1px solid #D1D5DB !important;
border-radius: 8px !important;
padding: 0.75rem 1.5rem !important;
}
.gr-button.secondary-button:hover {
background: #F9FAFB !important;
}
/* ์Šฌ๋ผ์ด๋” ์Šคํƒ€์ผ๋ง */
.gr-slider {
background: #E0E7FF !important;
}
.gr-slider .gr-slider-handle {
background: #4F46E5 !important;
}
/* ์ฒดํฌ๋ฐ•์Šค ์Šคํƒ€์ผ๋ง */
.gr-checkbox {
border-color: #6366F1 !important;
}
.gr-checkbox:checked {
background-color: #4F46E5 !important;
}
/* ํƒญ ์Šคํƒ€์ผ๋ง */
.gr-tabs {
border-bottom: 2px solid #E0E7FF !important;
}
.gr-tab-button {
color: #6B7280 !important;
padding: 0.75rem 1rem !important;
font-weight: 500 !important;
}
.gr-tab-button.selected {
color: #4F46E5 !important;
border-bottom: 2px solid #4F46E5 !important;
}
/* ๋งˆํฌ๋‹ค์šด ์ถœ๋ ฅ ์˜์—ญ */
.markdown-output {
background: white !important;
border-radius: 8px !important;
padding: 1rem !important;
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.05) !important;
}
"""
def read_fn(path):
disk_rw = FileBasedDataReader(os.path.dirname(path))
return disk_rw.read(os.path.basename(path))
def parse_pdf(doc_path, output_dir, end_page_id, is_ocr, layout_mode, formula_enable, table_enable, language):
os.makedirs(output_dir, exist_ok=True)
try:
file_name = f"{str(Path(doc_path).stem)}_{time.time()}"
pdf_data = read_fn(doc_path)
if is_ocr:
parse_method = "ocr"
else:
parse_method = "auto"
local_image_dir, local_md_dir = prepare_env(output_dir, file_name, parse_method)
do_parse(
output_dir,
file_name,
pdf_data,
[],
parse_method,
False,
end_page_id=end_page_id,
layout_model=layout_mode,
formula_enable=formula_enable,
table_enable=table_enable,
lang=language,
f_dump_orig_pdf=False,
)
return local_md_dir, file_name
except Exception as e:
logger.exception(e)
def compress_directory_to_zip(directory_path, output_zip_path):
try:
with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(directory_path):
for file in files:
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, directory_path)
zipf.write(file_path, arcname)
return 0
except Exception as e:
logger.exception(e)
return -1
def image_to_base64(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def replace_image_with_base64(markdown_text, image_dir_path):
pattern = r'\!\[(?:[^\]]*)\]\(([^)]+)\)'
def replace(match):
relative_path = match.group(1)
full_path = os.path.join(image_dir_path, relative_path)
base64_image = image_to_base64(full_path)
return f"![{relative_path}](data:image/jpeg;base64,{base64_image})"
return re.sub(pattern, replace, markdown_text)
def to_markdown(file_path, end_pages, is_ocr, layout_mode, formula_enable, table_enable, language):
file_path = to_pdf(file_path)
if end_pages > 20:
end_pages = 20
local_md_dir, file_name = parse_pdf(file_path, './output', end_pages - 1, is_ocr,
layout_mode, formula_enable, table_enable, language)
archive_zip_path = os.path.join("./output", compute_sha256(local_md_dir) + ".zip")
zip_archive_success = compress_directory_to_zip(local_md_dir, archive_zip_path)
if zip_archive_success == 0:
logger.info("์••์ถ• ์„ฑ๊ณต")
else:
logger.error("์••์ถ• ์‹คํŒจ")
md_path = os.path.join(local_md_dir, file_name + ".md")
with open(md_path, 'r', encoding='utf-8') as f:
txt_content = f.read()
md_content = replace_image_with_base64(txt_content, local_md_dir)
new_pdf_path = os.path.join(local_md_dir, file_name + "_layout.pdf")
return md_content, txt_content, archive_zip_path, new_pdf_path
def to_pdf(file_path):
with pymupdf.open(file_path) as f:
if f.is_pdf:
return file_path
else:
pdf_bytes = f.convert_to_pdf()
unique_filename = f"{uuid.uuid4()}.pdf"
tmp_file_path = os.path.join(os.path.dirname(file_path), unique_filename)
with open(tmp_file_path, 'wb') as tmp_pdf_file:
tmp_pdf_file.write(pdf_bytes)
return tmp_file_path
latex_delimiters = [{"left": "$$", "right": "$$", "display": True},
{"left": '$', "right": '$', "display": False}]
def init_model():
from magic_pdf.model.doc_analyze_by_custom_model import ModelSingleton
try:
model_manager = ModelSingleton()
txt_model = model_manager.get_model(False, False)
logger.info(f"txt_model init final")
ocr_model = model_manager.get_model(True, False)
logger.info(f"ocr_model init final")
return 0
except Exception as e:
logger.exception(e)
return -1
model_init = init_model()
logger.info(f"model_init: {model_init}")
latin_lang = [
'af', 'az', 'bs', 'cs', 'cy', 'da', 'de', 'es', 'et', 'fr', 'ga', 'hr',
'hu', 'id', 'is', 'it', 'ku', 'la', 'lt', 'lv', 'mi', 'ms', 'mt', 'nl',
'no', 'oc', 'pi', 'pl', 'pt', 'ro', 'rs_latin', 'sk', 'sl', 'sq', 'sv',
'sw', 'tl', 'tr', 'uz', 'vi', 'french', 'german'
]
arabic_lang = ['ar', 'fa', 'ug', 'ur']
cyrillic_lang = [
'ru', 'rs_cyrillic', 'be', 'bg', 'uk', 'mn', 'abq', 'ady', 'kbd', 'ava',
'dar', 'inh', 'che', 'lbe', 'lez', 'tab'
]
devanagari_lang = [
'hi', 'mr', 'ne', 'bh', 'mai', 'ang', 'bho', 'mah', 'sck', 'new', 'gom',
'sa', 'bgc'
]
other_lang = ['ch', 'en', 'korean', 'japan', 'chinese_cht', 'ta', 'te', 'ka']
all_lang = ['', 'auto']
all_lang.extend([*other_lang, *latin_lang, *arabic_lang, *cyrillic_lang, *devanagari_lang])
if __name__ == "__main__":
with gr.Blocks(title="OCR FLEX", css=create_css()) as demo:
# ํƒ€์ดํ‹€ ์˜์—ญ
with gr.Row(elem_classes="title-area"):
gr.HTML("""
<h1>OCR FLEX</h1>
<p>PDF์™€ ์ด๋ฏธ์ง€์—์„œ ํ…์ŠคํŠธ๋ฅผ ๋น ๋ฅด๊ณ  ์ •ํ™•ํ•˜๊ฒŒ ์ถ”์ถœํ•˜์„ธ์š”</p>
""")
with gr.Row():
# ์™ผ์ชฝ ํŒจ๋„
with gr.Column(variant='panel', scale=5):
file = gr.File(
label="PDF ๋˜๋Š” ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•˜์„ธ์š”",
file_types=[".pdf", ".png", ".jpeg", ".jpg"],
elem_classes="file-upload"
)
max_pages = gr.Slider(
1, 20, 10,
step=1,
label='์ตœ๋Œ€ ๋ณ€ํ™˜ ํŽ˜์ด์ง€ ์ˆ˜',
elem_classes="custom-slider"
)
with gr.Row():
layout_mode = gr.Dropdown(
["layoutlmv3", "doclayout_yolo"],
label="๋ ˆ์ด์•„์›ƒ ๋ชจ๋ธ",
value="doclayout_yolo",
elem_classes="custom-dropdown"
)
language = gr.Dropdown(
all_lang,
label="์–ธ์–ด",
value='auto',
elem_classes="custom-dropdown"
)
with gr.Row():
formula_enable = gr.Checkbox(
label="์ˆ˜์‹ ์ธ์‹ ํ™œ์„ฑํ™”",
value=True,
elem_classes="custom-checkbox"
)
is_ocr = gr.Checkbox(
label="OCR ๊ฐ•์ œ ํ™œ์„ฑํ™”",
value=False,
elem_classes="custom-checkbox"
)
table_enable = gr.Checkbox(
label="ํ‘œ ์ธ์‹ ํ™œ์„ฑํ™”(ํ…Œ์ŠคํŠธ)",
value=True,
elem_classes="custom-checkbox"
)
with gr.Row():
change_bu = gr.Button(
"๋ณ€ํ™˜",
elem_classes="primary-button"
)
clear_bu = gr.ClearButton(
value="์ดˆ๊ธฐํ™”",
elem_classes="secondary-button"
)
pdf_show = PDF(
label='PDF ๋ฏธ๋ฆฌ๋ณด๊ธฐ',
interactive=False,
visible=True,
height=800,
elem_classes="pdf-preview"
)
with gr.Accordion("์˜ˆ์ œ:", open=False):
example_root = os.path.join(os.path.dirname(__file__), "examples")
gr.Examples(
examples=[os.path.join(example_root, _) for _ in os.listdir(example_root) if
_.endswith("pdf")],
inputs=file
)
# ์˜ค๋ฅธ์ชฝ ํŒจ๋„
with gr.Column(variant='panel', scale=5):
output_file = gr.File(
label="๋ณ€ํ™˜ ๊ฒฐ๊ณผ",
interactive=False,
elem_classes="output-file"
)
with gr.Tabs() as tabs:
with gr.Tab("๋งˆํฌ๋‹ค์šด ๋ Œ๋”๋ง"):
md = gr.Markdown(
label="๋งˆํฌ๋‹ค์šด ๋ Œ๋”๋ง",
height=1100,
show_copy_button=True,
latex_delimiters=latex_delimiters,
line_breaks=True,
elem_classes="markdown-output"
)
with gr.Tab("๋งˆํฌ๋‹ค์šด ํ…์ŠคํŠธ"):
md_text = gr.TextArea(
lines=45,
show_copy_button=True,
elem_classes="markdown-text"
)
# ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ
file.change(
fn=to_pdf,
inputs=file,
outputs=pdf_show
)
change_bu.click(
fn=to_markdown,
inputs=[
file,
max_pages,
is_ocr,
layout_mode,
formula_enable,
table_enable,
language
],
outputs=[
md,
md_text,
output_file,
pdf_show
],
api_name=False
)
clear_bu.add([file, md, pdf_show, md_text, output_file, is_ocr])
# ์•ฑ ์‹คํ–‰
demo.launch(ssr_mode=True)