Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
@@ -145,7 +145,11 @@ def replace_image_with_base64(markdown_text, image_dir_path):
|
|
145 |
def to_pdf(file_path):
|
146 |
"""
|
147 |
μ΄λ―Έμ§(JPG/PNG λ±)λ₯Ό PDFλ‘ μ»¨λ²ν
.
|
|
|
148 |
"""
|
|
|
|
|
|
|
149 |
with pymupdf.open(file_path) as f:
|
150 |
if f.is_pdf:
|
151 |
return file_path
|
@@ -159,50 +163,54 @@ def to_pdf(file_path):
|
|
159 |
|
160 |
def to_markdown(file_path, end_pages, is_ocr, layout_mode, formula_enable, table_enable, language, progress=gr.Progress(track_tqdm=False)):
|
161 |
"""
|
162 |
-
μ
λ‘λλ PDF/μ΄λ―Έμ§
|
163 |
(νλ‘κ·Έλ μ€ λ° νμμ©)
|
164 |
"""
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
layout_mode, formula_enable, table_enable, language)
|
175 |
-
time.sleep(0.5)
|
176 |
-
|
177 |
-
progress(50, "μμΆ(zip) μμ± μ€...")
|
178 |
-
archive_zip_path = os.path.join("./output", compute_sha256(local_md_dir) + ".zip")
|
179 |
-
zip_archive_success = compress_directory_to_zip(local_md_dir, archive_zip_path)
|
180 |
-
if zip_archive_success == 0:
|
181 |
-
logger.info("μμΆ μ±κ³΅")
|
182 |
-
status_message = "\n\n**λ³ν μλ£ (μμΆ μ±κ³΅)**"
|
183 |
else:
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
|
201 |
def to_markdown_comparison(file_a, file_b, end_pages, is_ocr, layout_mode, formula_enable, table_enable, language, progress=gr.Progress(track_tqdm=False)):
|
202 |
"""
|
203 |
-
λ κ°μ νμΌμ λ³ννμ¬ A/B
|
204 |
-
κ°
|
205 |
-
λ
|
206 |
"""
|
207 |
combined_md = ""
|
208 |
if file_a is not None:
|
@@ -392,7 +400,7 @@ if __name__ == "__main__":
|
|
392 |
gr.HTML("""
|
393 |
<div class="title-area">
|
394 |
<h1>VisionOCR</h1>
|
395 |
-
<p>λ κ°μ PDF
|
396 |
ν νμΌλ§ μ
λ‘λνλ©΄ ν΄λΉ νμΌλ‘ λΆμν©λλ€.</p>
|
397 |
</div>
|
398 |
""")
|
@@ -404,8 +412,8 @@ if __name__ == "__main__":
|
|
404 |
hidden_chatbot = gr.Chatbot(visible=False)
|
405 |
|
406 |
with gr.Row():
|
407 |
-
file_a = gr.File(label="λ¬Έμ A μ
λ‘λ", file_types=[".pdf", ".png", ".jpeg", ".jpg"], interactive=True)
|
408 |
-
file_b = gr.File(label="λ¬Έμ B μ
λ‘λ", file_types=[".pdf", ".png", ".jpeg", ".jpg"], interactive=True)
|
409 |
convert_btn = gr.Button("λΉκ΅μ© λ³ννκΈ°")
|
410 |
|
411 |
# νμΌ μ
λ‘λ μ μν μ΄κΈ°ν (Chatbotμ μ¨κΉ μ²λ¦¬)
|
@@ -447,7 +455,7 @@ if __name__ == "__main__":
|
|
447 |
chat_input = gr.Textbox(lines=1, placeholder="μ§λ¬Έμ μ
λ ₯νμΈμ...")
|
448 |
clear_btn = gr.Button("λν μ΄κΈ°ν")
|
449 |
|
450 |
-
# μ±ν
μ
λ ₯
|
451 |
chat_input.submit(
|
452 |
fn=user_message,
|
453 |
inputs=[chat_input, chat_history, md_state],
|
|
|
145 |
def to_pdf(file_path):
|
146 |
"""
|
147 |
μ΄λ―Έμ§(JPG/PNG λ±)λ₯Ό PDFλ‘ μ»¨λ²ν
.
|
148 |
+
TXT, CSV νμΌμΈ κ²½μ° λ³ν μμ΄ μλ³Έ κ²½λ‘λ₯Ό λ°ννλ€.
|
149 |
"""
|
150 |
+
ext = Path(file_path).suffix.lower()
|
151 |
+
if ext in ['.txt', '.csv']:
|
152 |
+
return file_path
|
153 |
with pymupdf.open(file_path) as f:
|
154 |
if f.is_pdf:
|
155 |
return file_path
|
|
|
163 |
|
164 |
def to_markdown(file_path, end_pages, is_ocr, layout_mode, formula_enable, table_enable, language, progress=gr.Progress(track_tqdm=False)):
|
165 |
"""
|
166 |
+
μ
λ‘λλ PDF/μ΄λ―Έμ§ λλ TXT/CSV -> λ§ν¬λ€μ΄ λ³ν
|
167 |
(νλ‘κ·Έλ μ€ λ° νμμ©)
|
168 |
"""
|
169 |
+
ext = Path(file_path).suffix.lower()
|
170 |
+
if ext in ['.txt', '.csv']:
|
171 |
+
progress(0, "νμΌ μ½λ μ€...")
|
172 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
173 |
+
txt_content = f.read()
|
174 |
+
time.sleep(0.5)
|
175 |
+
progress(50, "νμΌ λ΄μ© μ²λ¦¬ μ€...")
|
176 |
+
progress(100, "λ³ν μλ£!")
|
177 |
+
return f"```{txt_content}```\n\n**λ³ν μλ£ (ν
μ€νΈ/CSV νμΌ)**"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
else:
|
179 |
+
progress(0, "PDFλ‘ λ³ν μ€...")
|
180 |
+
file_path = to_pdf(file_path)
|
181 |
+
time.sleep(0.5)
|
182 |
+
if end_pages > 20:
|
183 |
+
end_pages = 20
|
184 |
+
progress(20, "λ¬Έμ νμ± μ€...")
|
185 |
+
local_md_dir, file_name = parse_pdf(file_path, './output', end_pages - 1, is_ocr,
|
186 |
+
layout_mode, formula_enable, table_enable, language)
|
187 |
+
time.sleep(0.5)
|
188 |
+
progress(50, "μμΆ(zip) μμ± μ€...")
|
189 |
+
archive_zip_path = os.path.join("./output", compute_sha256(local_md_dir) + ".zip")
|
190 |
+
zip_archive_success = compress_directory_to_zip(local_md_dir, archive_zip_path)
|
191 |
+
if zip_archive_success == 0:
|
192 |
+
logger.info("μμΆ μ±κ³΅")
|
193 |
+
status_message = "\n\n**λ³ν μλ£ (μμΆ μ±κ³΅)**"
|
194 |
+
else:
|
195 |
+
logger.error("μμΆ μ€ν¨")
|
196 |
+
status_message = "\n\n**λ³ν μλ£ (μμΆ μ€ν¨)**"
|
197 |
+
time.sleep(0.5)
|
198 |
+
progress(70, "λ§ν¬λ€μ΄ μ½λ μ€...")
|
199 |
+
md_path = os.path.join(local_md_dir, file_name + ".md")
|
200 |
+
with open(md_path, 'r', encoding='utf-8') as f:
|
201 |
+
txt_content = f.read()
|
202 |
+
time.sleep(0.5)
|
203 |
+
progress(90, "μ΄λ―Έμ§ base64 λ³ν μ€...")
|
204 |
+
md_content = replace_image_with_base64(txt_content, local_md_dir)
|
205 |
+
time.sleep(0.5)
|
206 |
+
progress(100, "λ³ν μλ£!")
|
207 |
+
return md_content + status_message
|
208 |
|
209 |
def to_markdown_comparison(file_a, file_b, end_pages, is_ocr, layout_mode, formula_enable, table_enable, language, progress=gr.Progress(track_tqdm=False)):
|
210 |
"""
|
211 |
+
λ κ°μ νμΌμ λ³ννμ¬ A/B λΉκ΅μ© λ§ν¬λ€μ΄ μμ±.
|
212 |
+
κ° νμΌμ "λ¬Έμ A", "λ¬Έμ B" ν€λλ‘ κ΅¬λΆλλ©°,
|
213 |
+
λ νμΌ λͺ¨λ μ
λ‘λλ κ²½μ° μΆκ°λ‘ λΉκ΅ λΆμ μ§μμ¬νμ ν¬ν¨νλ€.
|
214 |
"""
|
215 |
combined_md = ""
|
216 |
if file_a is not None:
|
|
|
400 |
gr.HTML("""
|
401 |
<div class="title-area">
|
402 |
<h1>VisionOCR</h1>
|
403 |
+
<p>λ κ°μ PDF/μ΄λ―Έμ§/ν
μ€νΈ/CSV νμΌμ μ
λ‘λνμ¬ A/B λΉκ΅ ν, μΆλ‘ LLMκ³Ό λνν©λλ€.<br>
|
404 |
ν νμΌλ§ μ
λ‘λνλ©΄ ν΄λΉ νμΌλ‘ λΆμν©λλ€.</p>
|
405 |
</div>
|
406 |
""")
|
|
|
412 |
hidden_chatbot = gr.Chatbot(visible=False)
|
413 |
|
414 |
with gr.Row():
|
415 |
+
file_a = gr.File(label="λ¬Έμ A μ
λ‘λ", file_types=[".pdf", ".png", ".jpeg", ".jpg", ".txt", ".csv"], interactive=True)
|
416 |
+
file_b = gr.File(label="λ¬Έμ B μ
λ‘λ", file_types=[".pdf", ".png", ".jpeg", ".jpg", ".txt", ".csv"], interactive=True)
|
417 |
convert_btn = gr.Button("λΉκ΅μ© λ³ννκΈ°")
|
418 |
|
419 |
# νμΌ μ
λ‘λ μ μν μ΄κΈ°ν (Chatbotμ μ¨κΉ μ²λ¦¬)
|
|
|
455 |
chat_input = gr.Textbox(lines=1, placeholder="μ§λ¬Έμ μ
λ ₯νμΈμ...")
|
456 |
clear_btn = gr.Button("λν μ΄κΈ°ν")
|
457 |
|
458 |
+
# μ±ν
μ
λ ₯ ν LLM μλ΅μ hidden_chatbotμ μ μ₯(νλ©΄μ 보μ΄μ§ μμ)
|
459 |
chat_input.submit(
|
460 |
fn=user_message,
|
461 |
inputs=[chat_input, chat_history, md_state],
|