Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,7 @@
|
|
|
|
|
|
|
|
|
|
1 |
import base64
|
2 |
import json
|
3 |
import os
|
@@ -8,9 +12,9 @@ import re
|
|
8 |
import uuid
|
9 |
import pymupdf
|
10 |
|
|
|
11 |
os.system('pip uninstall -y magic-pdf')
|
12 |
os.system('pip install git+https://github.com/opendatalab/MinerU.git@dev')
|
13 |
-
|
14 |
os.system('wget https://github.com/opendatalab/MinerU/raw/dev/scripts/download_models_hf.py -O download_models_hf.py')
|
15 |
os.system('python download_models_hf.py')
|
16 |
|
@@ -26,7 +30,7 @@ with open('/home/user/magic-pdf.json', 'w') as file:
|
|
26 |
json.dump(data, file, indent=4)
|
27 |
|
28 |
os.system('cp -r paddleocr /home/user/.paddleocr')
|
29 |
-
from gradio_pdf import PDF
|
30 |
|
31 |
import gradio as gr
|
32 |
from loguru import logger
|
@@ -65,81 +69,9 @@ def create_css():
|
|
65 |
color: #6B7280;
|
66 |
font-size: 1.1rem;
|
67 |
}
|
68 |
-
/* ์ปดํฌ๋ํธ
|
69 |
-
.
|
70 |
-
|
71 |
-
border-radius: 12px !important;
|
72 |
-
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;
|
73 |
-
background: white !important;
|
74 |
-
}
|
75 |
-
/* ํ์ผ ์
๋ก๋ ์์ญ */
|
76 |
-
.file-upload {
|
77 |
-
border: 2px dashed #93C5FD !important;
|
78 |
-
border-radius: 8px !important;
|
79 |
-
padding: 2rem !important;
|
80 |
-
background: #F0F9FF !important;
|
81 |
-
transition: all 0.3s ease;
|
82 |
-
}
|
83 |
-
.file-upload:hover {
|
84 |
-
background: #E0F2FE !important;
|
85 |
-
border-color: #60A5FA !important;
|
86 |
-
}
|
87 |
-
/* ๋ฒํผ ์คํ์ผ๋ง */
|
88 |
-
.gr-button.primary-button {
|
89 |
-
background: linear-gradient(90deg, #2563EB 0%, #7C3AED 100%) !important;
|
90 |
-
color: white !important;
|
91 |
-
border: none !important;
|
92 |
-
border-radius: 8px !important;
|
93 |
-
padding: 0.75rem 1.5rem !important;
|
94 |
-
font-weight: bold !important;
|
95 |
-
transition: opacity 0.2s !important;
|
96 |
-
}
|
97 |
-
.gr-button.primary-button:hover {
|
98 |
-
opacity: 0.9 !important;
|
99 |
-
}
|
100 |
-
.gr-button.secondary-button {
|
101 |
-
background: white !important;
|
102 |
-
color: #4B5563 !important;
|
103 |
-
border: 1px solid #D1D5DB !important;
|
104 |
-
border-radius: 8px !important;
|
105 |
-
padding: 0.75rem 1.5rem !important;
|
106 |
-
}
|
107 |
-
.gr-button.secondary-button:hover {
|
108 |
-
background: #F9FAFB !important;
|
109 |
-
}
|
110 |
-
/* ์ฌ๋ผ์ด๋ ์คํ์ผ๋ง */
|
111 |
-
.gr-slider {
|
112 |
-
background: #E0E7FF !important;
|
113 |
-
}
|
114 |
-
.gr-slider .gr-slider-handle {
|
115 |
-
background: #4F46E5 !important;
|
116 |
-
}
|
117 |
-
/* ์ฒดํฌ๋ฐ์ค ์คํ์ผ๋ง */
|
118 |
-
.gr-checkbox {
|
119 |
-
border-color: #6366F1 !important;
|
120 |
-
}
|
121 |
-
.gr-checkbox:checked {
|
122 |
-
background-color: #4F46E5 !important;
|
123 |
-
}
|
124 |
-
/* ํญ ์คํ์ผ๋ง */
|
125 |
-
.gr-tabs {
|
126 |
-
border-bottom: 2px solid #E0E7FF !important;
|
127 |
-
}
|
128 |
-
.gr-tab-button {
|
129 |
-
color: #6B7280 !important;
|
130 |
-
padding: 0.75rem 1rem !important;
|
131 |
-
font-weight: 500 !important;
|
132 |
-
}
|
133 |
-
.gr-tab-button.selected {
|
134 |
-
color: #4F46E5 !important;
|
135 |
-
border-bottom: 2px solid #4F46E5 !important;
|
136 |
-
}
|
137 |
-
/* ๋งํฌ๋ค์ด ์ถ๋ ฅ ์์ญ */
|
138 |
-
.markdown-output {
|
139 |
-
background: white !important;
|
140 |
-
border-radius: 8px !important;
|
141 |
-
padding: 1rem !important;
|
142 |
-
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.05) !important;
|
143 |
}
|
144 |
"""
|
145 |
|
@@ -202,41 +134,56 @@ def replace_image_with_base64(markdown_text, image_dir_path):
|
|
202 |
return f""
|
203 |
return re.sub(pattern, replace, markdown_text)
|
204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
def to_markdown(file_path, end_pages, is_ocr, layout_mode, formula_enable, table_enable, language):
|
|
|
|
|
|
|
|
|
206 |
file_path = to_pdf(file_path)
|
207 |
if end_pages > 20:
|
208 |
end_pages = 20
|
209 |
local_md_dir, file_name = parse_pdf(file_path, './output', end_pages - 1, is_ocr,
|
210 |
-
|
211 |
archive_zip_path = os.path.join("./output", compute_sha256(local_md_dir) + ".zip")
|
212 |
zip_archive_success = compress_directory_to_zip(local_md_dir, archive_zip_path)
|
213 |
if zip_archive_success == 0:
|
214 |
logger.info("์์ถ ์ฑ๊ณต")
|
215 |
else:
|
216 |
logger.error("์์ถ ์คํจ")
|
|
|
217 |
md_path = os.path.join(local_md_dir, file_name + ".md")
|
218 |
with open(md_path, 'r', encoding='utf-8') as f:
|
219 |
txt_content = f.read()
|
|
|
220 |
md_content = replace_image_with_base64(txt_content, local_md_dir)
|
221 |
-
new_pdf_path = os.path.join(local_md_dir, file_name + "_layout.pdf")
|
222 |
-
return md_content, txt_content, archive_zip_path, new_pdf_path
|
223 |
|
224 |
-
|
225 |
-
with pymupdf.open(file_path) as f:
|
226 |
-
if f.is_pdf:
|
227 |
-
return file_path
|
228 |
-
else:
|
229 |
-
pdf_bytes = f.convert_to_pdf()
|
230 |
-
unique_filename = f"{uuid.uuid4()}.pdf"
|
231 |
-
tmp_file_path = os.path.join(os.path.dirname(file_path), unique_filename)
|
232 |
-
with open(tmp_file_path, 'wb') as tmp_pdf_file:
|
233 |
-
tmp_pdf_file.write(pdf_bytes)
|
234 |
-
return tmp_file_path
|
235 |
|
236 |
-
latex_delimiters = [
|
237 |
-
|
|
|
|
|
238 |
|
239 |
def init_model():
|
|
|
|
|
|
|
240 |
from magic_pdf.model.doc_analyze_by_custom_model import ModelSingleton
|
241 |
try:
|
242 |
model_manager = ModelSingleton()
|
@@ -272,142 +219,310 @@ other_lang = ['ch', 'en', 'korean', 'japan', 'chinese_cht', 'ta', 'te', 'ka']
|
|
272 |
all_lang = ['', 'auto']
|
273 |
all_lang.extend([*other_lang, *latin_lang, *arabic_lang, *cyrillic_lang, *devanagari_lang])
|
274 |
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
298 |
)
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
label="์ธ์ด",
|
310 |
-
value='auto',
|
311 |
-
elem_classes="custom-dropdown"
|
312 |
-
)
|
313 |
-
|
314 |
-
with gr.Row():
|
315 |
-
formula_enable = gr.Checkbox(
|
316 |
-
label="์์ ์ธ์ ํ์ฑํ",
|
317 |
-
value=True,
|
318 |
-
elem_classes="custom-checkbox"
|
319 |
-
)
|
320 |
-
is_ocr = gr.Checkbox(
|
321 |
-
label="OCR ๊ฐ์ ํ์ฑํ",
|
322 |
-
value=False,
|
323 |
-
elem_classes="custom-checkbox"
|
324 |
-
)
|
325 |
-
table_enable = gr.Checkbox(
|
326 |
-
label="ํ ์ธ์ ํ์ฑํ(ํ
์คํธ)",
|
327 |
-
value=True,
|
328 |
-
elem_classes="custom-checkbox"
|
329 |
-
)
|
330 |
-
|
331 |
-
with gr.Row():
|
332 |
-
change_bu = gr.Button(
|
333 |
-
"๋ณํ",
|
334 |
-
elem_classes="primary-button"
|
335 |
-
)
|
336 |
-
clear_bu = gr.ClearButton(
|
337 |
-
value="์ด๊ธฐํ",
|
338 |
-
elem_classes="secondary-button"
|
339 |
)
|
340 |
-
|
341 |
-
pdf_show = PDF(
|
342 |
-
label='PDF ๋ฏธ๋ฆฌ๋ณด๊ธฐ',
|
343 |
-
interactive=False,
|
344 |
-
visible=True,
|
345 |
-
height=800,
|
346 |
-
elem_classes="pdf-preview"
|
347 |
)
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
356 |
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
interactive=False,
|
362 |
-
elem_classes="output-file"
|
363 |
)
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
388 |
)
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
inputs=[
|
393 |
-
file,
|
394 |
-
max_pages,
|
395 |
-
is_ocr,
|
396 |
-
layout_mode,
|
397 |
-
formula_enable,
|
398 |
-
table_enable,
|
399 |
-
language
|
400 |
-
],
|
401 |
-
outputs=[
|
402 |
-
md,
|
403 |
-
md_text,
|
404 |
-
output_file,
|
405 |
-
pdf_show
|
406 |
-
],
|
407 |
-
api_name=False
|
408 |
)
|
409 |
-
|
410 |
-
clear_bu.add([file, md, pdf_show, md_text, output_file, is_ocr])
|
411 |
|
412 |
-
#
|
413 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
##############################
|
2 |
+
# 1) ๊ธฐ์กด PDF ์ฒ๋ฆฌ ์ฝ๋
|
3 |
+
##############################
|
4 |
+
|
5 |
import base64
|
6 |
import json
|
7 |
import os
|
|
|
12 |
import uuid
|
13 |
import pymupdf
|
14 |
|
15 |
+
# ์๋ ์ฝ๋์ ์๋ os.system() ํธ์ถ๋ค
|
16 |
os.system('pip uninstall -y magic-pdf')
|
17 |
os.system('pip install git+https://github.com/opendatalab/MinerU.git@dev')
|
|
|
18 |
os.system('wget https://github.com/opendatalab/MinerU/raw/dev/scripts/download_models_hf.py -O download_models_hf.py')
|
19 |
os.system('python download_models_hf.py')
|
20 |
|
|
|
30 |
json.dump(data, file, indent=4)
|
31 |
|
32 |
os.system('cp -r paddleocr /home/user/.paddleocr')
|
33 |
+
# from gradio_pdf import PDF # PDF ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋ฅผ ์ํ ์ปดํฌ๋ํธ์ด์ง๋ง, ์ง๊ธ์ ์จ๊ธธ ์์
|
34 |
|
35 |
import gradio as gr
|
36 |
from loguru import logger
|
|
|
69 |
color: #6B7280;
|
70 |
font-size: 1.1rem;
|
71 |
}
|
72 |
+
/* ์จ๊ธธ ์์ ์ธ ์ปดํฌ๋ํธ ์คํ์ผ */
|
73 |
+
.invisible {
|
74 |
+
display: none !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
}
|
76 |
"""
|
77 |
|
|
|
134 |
return f""
|
135 |
return re.sub(pattern, replace, markdown_text)
|
136 |
|
137 |
+
def to_pdf(file_path):
|
138 |
+
"""
|
139 |
+
PDF๊ฐ ์๋ ๊ฒฝ์ฐ(์: PNG, JPG ํ์ผ)์๋ pymupdf๋ฅผ ์ด์ฉํ์ฌ PDF๋ก ๋ณํํ๊ธฐ ์ํ ํจ์.
|
140 |
+
"""
|
141 |
+
with pymupdf.open(file_path) as f:
|
142 |
+
if f.is_pdf:
|
143 |
+
return file_path
|
144 |
+
else:
|
145 |
+
pdf_bytes = f.convert_to_pdf()
|
146 |
+
unique_filename = f"{uuid.uuid4()}.pdf"
|
147 |
+
tmp_file_path = os.path.join(os.path.dirname(file_path), unique_filename)
|
148 |
+
with open(tmp_file_path, 'wb') as tmp_pdf_file:
|
149 |
+
tmp_pdf_file.write(pdf_bytes)
|
150 |
+
return tmp_file_path
|
151 |
+
|
152 |
def to_markdown(file_path, end_pages, is_ocr, layout_mode, formula_enable, table_enable, language):
|
153 |
+
"""
|
154 |
+
ํ์ผ์ ๋ฐ์ ์ต๋ end_pages ํ์ด์ง๊น์ง ๋งํฌ๋ค์ด ์ถ์ถ ํ,
|
155 |
+
base64 ์ด๋ฏธ์ง๊ฐ ํฌํจ๋ md_content๋ฅผ ๋ฐํ.
|
156 |
+
"""
|
157 |
file_path = to_pdf(file_path)
|
158 |
if end_pages > 20:
|
159 |
end_pages = 20
|
160 |
local_md_dir, file_name = parse_pdf(file_path, './output', end_pages - 1, is_ocr,
|
161 |
+
layout_mode, formula_enable, table_enable, language)
|
162 |
archive_zip_path = os.path.join("./output", compute_sha256(local_md_dir) + ".zip")
|
163 |
zip_archive_success = compress_directory_to_zip(local_md_dir, archive_zip_path)
|
164 |
if zip_archive_success == 0:
|
165 |
logger.info("์์ถ ์ฑ๊ณต")
|
166 |
else:
|
167 |
logger.error("์์ถ ์คํจ")
|
168 |
+
|
169 |
md_path = os.path.join(local_md_dir, file_name + ".md")
|
170 |
with open(md_path, 'r', encoding='utf-8') as f:
|
171 |
txt_content = f.read()
|
172 |
+
|
173 |
md_content = replace_image_with_base64(txt_content, local_md_dir)
|
174 |
+
# new_pdf_path = os.path.join(local_md_dir, file_name + "_layout.pdf") # ์๋ pdf ๋ฏธ๋ฆฌ๋ณด๊ธฐ์ฉ
|
|
|
175 |
|
176 |
+
return md_content # base64 ์ด๋ฏธ์ง๊ฐ ํฌํจ๋ ์ต์ข
๋งํฌ๋ค์ด ํ
์คํธ๋ง ๋ฐํ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
|
178 |
+
latex_delimiters = [
|
179 |
+
{"left": "$$", "right": "$$", "display": True},
|
180 |
+
{"left": '$', "right": '$', "display": False}
|
181 |
+
]
|
182 |
|
183 |
def init_model():
|
184 |
+
"""
|
185 |
+
magic_pdf์ ๋ชจ๋ธ์ ๋ฏธ๋ฆฌ ์ด๊ธฐํ.
|
186 |
+
"""
|
187 |
from magic_pdf.model.doc_analyze_by_custom_model import ModelSingleton
|
188 |
try:
|
189 |
model_manager = ModelSingleton()
|
|
|
219 |
all_lang = ['', 'auto']
|
220 |
all_lang.extend([*other_lang, *latin_lang, *arabic_lang, *cyrillic_lang, *devanagari_lang])
|
221 |
|
222 |
+
|
223 |
+
##############################
|
224 |
+
# 2) Gemini LLM ์ฑ ์ฝ๋
|
225 |
+
##############################
|
226 |
+
|
227 |
+
# (์ค๋ณต import์ด์ง๋ง "๋๋ฝ ์์ด" ์ถ๋ ฅํด์ผ ํ๋ฏ๋ก ์ฃผ์ ์ฒ๋ฆฌ)
|
228 |
+
# import os
|
229 |
+
# import gradio as gr
|
230 |
+
from gradio import ChatMessage
|
231 |
+
from typing import Iterator
|
232 |
+
import google.generativeai as genai
|
233 |
+
import time
|
234 |
+
|
235 |
+
# get Gemini API Key from the environ variable
|
236 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
237 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
238 |
+
|
239 |
+
# we will be using the Gemini 2.0 Flash model with Thinking capabilities
|
240 |
+
model = genai.GenerativeModel("gemini-2.0-flash-thinking-exp-1219")
|
241 |
+
|
242 |
+
def format_chat_history(messages: list) -> list:
|
243 |
+
"""
|
244 |
+
Formats the chat history into a structure Gemini can understand
|
245 |
+
"""
|
246 |
+
formatted_history = []
|
247 |
+
for message in messages:
|
248 |
+
# Skip thinking messages (messages with metadata)
|
249 |
+
if not (message.get("role") == "assistant" and "metadata" in message):
|
250 |
+
formatted_history.append({
|
251 |
+
"role": "user" if message.get("role") == "user" else "assistant",
|
252 |
+
"parts": [message.get("content", "")]
|
253 |
+
})
|
254 |
+
return formatted_history
|
255 |
+
|
256 |
+
def stream_gemini_response(user_message: str, messages: list) -> Iterator[list]:
|
257 |
+
"""
|
258 |
+
Streams thoughts and response with conversation history support for text input only.
|
259 |
+
"""
|
260 |
+
if not user_message.strip():
|
261 |
+
messages.append(ChatMessage(role="assistant", content="Please provide a non-empty text message. Empty input is not allowed."))
|
262 |
+
yield messages
|
263 |
+
return
|
264 |
+
|
265 |
+
try:
|
266 |
+
print(f"\n=== New Request (Text) ===")
|
267 |
+
print(f"User message: {user_message}")
|
268 |
+
|
269 |
+
chat_history = format_chat_history(messages)
|
270 |
+
chat = model.start_chat(history=chat_history)
|
271 |
+
response = chat.send_message(user_message, stream=True)
|
272 |
+
|
273 |
+
thought_buffer = ""
|
274 |
+
response_buffer = ""
|
275 |
+
thinking_complete = False
|
276 |
+
|
277 |
+
messages.append(
|
278 |
+
ChatMessage(
|
279 |
+
role="assistant",
|
280 |
+
content="",
|
281 |
+
metadata={"title": "โ๏ธ Thinking: *The thoughts produced by the model are experimental"}
|
282 |
+
)
|
283 |
+
)
|
284 |
+
|
285 |
+
for chunk in response:
|
286 |
+
parts = chunk.candidates[0].content.parts
|
287 |
+
current_chunk = parts[0].text
|
288 |
+
|
289 |
+
if len(parts) == 2 and not thinking_complete:
|
290 |
+
# Complete thought and start response
|
291 |
+
thought_buffer += current_chunk
|
292 |
+
print(f"\n=== Complete Thought ===\n{thought_buffer}")
|
293 |
+
|
294 |
+
messages[-1] = ChatMessage(
|
295 |
+
role="assistant",
|
296 |
+
content=thought_buffer,
|
297 |
+
metadata={"title": "โ๏ธ Thinking: *The thoughts produced by the model are experimental"}
|
298 |
)
|
299 |
+
yield messages
|
300 |
+
|
301 |
+
# Start response
|
302 |
+
response_buffer = parts[1].text
|
303 |
+
print(f"\n=== Starting Response ===\n{response_buffer}")
|
304 |
+
|
305 |
+
messages.append(
|
306 |
+
ChatMessage(
|
307 |
+
role="assistant",
|
308 |
+
content=response_buffer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
309 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
310 |
)
|
311 |
+
thinking_complete = True
|
312 |
+
|
313 |
+
elif thinking_complete:
|
314 |
+
response_buffer += current_chunk
|
315 |
+
print(f"\n=== Response Chunk ===\n{current_chunk}")
|
316 |
+
|
317 |
+
messages[-1] = ChatMessage(
|
318 |
+
role="assistant",
|
319 |
+
content=response_buffer
|
320 |
+
)
|
321 |
+
|
322 |
+
else:
|
323 |
+
thought_buffer += current_chunk
|
324 |
+
print(f"\n=== Thinking Chunk ===\n{current_chunk}")
|
325 |
|
326 |
+
messages[-1] = ChatMessage(
|
327 |
+
role="assistant",
|
328 |
+
content=thought_buffer,
|
329 |
+
metadata={"title": "โ๏ธ Thinking: *The thoughts produced by the model are experimental"}
|
|
|
|
|
330 |
)
|
331 |
+
|
332 |
+
# time.sleep(0.05) #Optional debugging delay
|
333 |
+
yield messages
|
334 |
+
|
335 |
+
print(f"\n=== Final Response ===\n{response_buffer}")
|
336 |
+
|
337 |
+
except Exception as e:
|
338 |
+
print(f"\n=== Error ===\n{str(e)}")
|
339 |
+
messages.append(
|
340 |
+
ChatMessage(
|
341 |
+
role="assistant",
|
342 |
+
content=f"I apologize, but I encountered an error: {str(e)}"
|
343 |
+
)
|
344 |
+
)
|
345 |
+
yield messages
|
346 |
+
|
347 |
+
def user_message(msg: str, history: list) -> tuple[str, list]:
|
348 |
+
"""Adds user message to chat history"""
|
349 |
+
history.append(ChatMessage(role="user", content=msg))
|
350 |
+
return "", history
|
351 |
+
|
352 |
+
|
353 |
+
######################################################
|
354 |
+
# 3) ํตํฉ Gradio ์ฑ ๊ตฌ์ฑ
|
355 |
+
# - PDF ์
๋ก๋๋ง ๋ณด์ด๊ฒ ํ๊ณ (๋๋จธ์ง๋ hidden)
|
356 |
+
# - ์
๋ก๋ ํ "๋ณํ" ๋ฒํผ ํด๋ฆญ ์, ๋งํฌ๋ค์ด์ ๋ง๋ค์ด
|
357 |
+
# Chatbot๊ณผ ๋ํํ ์ ์๋๋ก ์ ๋ฌ
|
358 |
+
######################################################
|
359 |
+
with gr.Blocks(title="ํตํฉ OCR & Gemini Chat", css=create_css(), theme=gr.themes.Soft(primary_hue="teal", secondary_hue="slate", neutral_hue="neutral")) as demo:
|
360 |
+
gr.HTML("""
|
361 |
+
<div class="title-area">
|
362 |
+
<h1>OCR FLEX + Gemini Chat</h1>
|
363 |
+
<p>PDF/์ด๋ฏธ์ง -> ํ
์คํธ(๋งํฌ๋ค์ด) ๋ณํ ํ, LLM Gemini์ ๋ํ</p>
|
364 |
+
</div>
|
365 |
+
""")
|
366 |
+
|
367 |
+
# ๋ด๋ถ ์ํ(๋งํฌ๋ค์ด ํ
์คํธ)
|
368 |
+
md_state = gr.State("")
|
369 |
+
chat_history = gr.State([]) # Gemini ์ฑ ๊ธฐ๋ก ์ํ
|
370 |
+
|
371 |
+
# 1) ํ์ผ ์
๋ก๋ UI
|
372 |
+
with gr.Row():
|
373 |
+
file = gr.File(
|
374 |
+
label="PDF ๋๋ ์ด๋ฏธ์ง ํ์ผ ์
๋ก๋",
|
375 |
+
file_types=[".pdf", ".png", ".jpeg", ".jpg"],
|
376 |
+
interactive=True
|
377 |
)
|
378 |
+
convert_btn = gr.Button(
|
379 |
+
"๋ณํ",
|
380 |
+
elem_classes="primary-button"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
381 |
)
|
|
|
|
|
382 |
|
383 |
+
# 2) ์๋ ์กด์ฌํ๋ ์ฌ๋ผ์ด๋, ์ฒดํฌ๋ฐ์ค ๋ฑ์ ์ ๋ถ hidden
|
384 |
+
max_pages = gr.Slider(
|
385 |
+
1, 20, 10,
|
386 |
+
step=1,
|
387 |
+
label='์ต๋ ๋ณํ ํ์ด์ง ์',
|
388 |
+
elem_classes="invisible",
|
389 |
+
visible=False
|
390 |
+
)
|
391 |
+
layout_mode = gr.Dropdown(
|
392 |
+
["layoutlmv3", "doclayout_yolo"],
|
393 |
+
label="๋ ์ด์์ ๋ชจ๋ธ",
|
394 |
+
value="doclayout_yolo",
|
395 |
+
elem_classes="invisible",
|
396 |
+
visible=False
|
397 |
+
)
|
398 |
+
language = gr.Dropdown(
|
399 |
+
all_lang,
|
400 |
+
label="์ธ์ด",
|
401 |
+
value='auto',
|
402 |
+
elem_classes="invisible",
|
403 |
+
visible=False
|
404 |
+
)
|
405 |
+
formula_enable = gr.Checkbox(
|
406 |
+
label="์์ ์ธ์ ํ์ฑํ",
|
407 |
+
value=True,
|
408 |
+
elem_classes="invisible",
|
409 |
+
visible=False
|
410 |
+
)
|
411 |
+
is_ocr = gr.Checkbox(
|
412 |
+
label="OCR ๊ฐ์ ํ์ฑํ",
|
413 |
+
value=False,
|
414 |
+
elem_classes="invisible",
|
415 |
+
visible=False
|
416 |
+
)
|
417 |
+
table_enable = gr.Checkbox(
|
418 |
+
label="ํ ์ธ์ ํ์ฑํ(ํ
์คํธ)",
|
419 |
+
value=True,
|
420 |
+
elem_classes="invisible",
|
421 |
+
visible=False
|
422 |
+
)
|
423 |
+
|
424 |
+
# 3) ์ถ๋ ฅ ๊ฒฐ๊ณผ(ํ์ผ, ๋งํฌ๋ค์ด ๋ฑ)๋ ์จ๊น
|
425 |
+
# ํ์ํ๋ฉด ์ฃผ์ ํด์ ํ์ฌ ํ์ธ ๊ฐ๋ฅ
|
426 |
+
# output_file = gr.File(
|
427 |
+
# label="๋ณํ ๊ฒฐ๊ณผ",
|
428 |
+
# interactive=False,
|
429 |
+
# visible=False
|
430 |
+
# )
|
431 |
+
# md = gr.Markdown(
|
432 |
+
# label="๋งํฌ๋ค์ด ๋ ๋๋ง",
|
433 |
+
# visible=False
|
434 |
+
# )
|
435 |
+
# md_text = gr.TextArea(
|
436 |
+
# lines=45,
|
437 |
+
# visible=False
|
438 |
+
# )
|
439 |
+
# pdf_show = PDF(
|
440 |
+
# label='PDF ๋ฏธ๋ฆฌ๋ณด๊ธฐ',
|
441 |
+
# interactive=False,
|
442 |
+
# visible=False,
|
443 |
+
# height=800
|
444 |
+
# )
|
445 |
+
|
446 |
+
# 4) ํ์ผ ์
๋ก๋ -> '๋ณํ' ๋ฒํผ ํด๋ฆญ์ ๋์:
|
447 |
+
# to_markdown ํจ์๋ฅผ ํตํด md_state์ ๋งํฌ๋ค์ด ์ ์ฅ
|
448 |
+
convert_btn.click(
|
449 |
+
fn=to_markdown,
|
450 |
+
inputs=[file, max_pages, is_ocr, layout_mode, formula_enable, table_enable, language],
|
451 |
+
outputs=md_state
|
452 |
+
)
|
453 |
+
|
454 |
+
# ==========================
|
455 |
+
# Gemini Chat ๋ถ๋ถ
|
456 |
+
# ==========================
|
457 |
+
gr.Markdown("## Gemini 2.0 Flash (With Thinking) Chat")
|
458 |
+
|
459 |
+
chatbot = gr.Chatbot(
|
460 |
+
label="Gemini2.0 Chatbot (Streaming Output)",
|
461 |
+
render_markdown=True,
|
462 |
+
height=400
|
463 |
+
)
|
464 |
+
|
465 |
+
with gr.Row():
|
466 |
+
chat_input = gr.Textbox(
|
467 |
+
lines=1,
|
468 |
+
label="์ง๋ฌธ ์
๋ ฅ",
|
469 |
+
placeholder="์ถ์ถ๋ ๋ฌธ์(๋งํฌ๋ค์ด ๋ด์ฉ)์ ๋ํด ๊ถ๊ธํ ์ ์ ๋ฌผ์ด๋ณด์ธ์..."
|
470 |
+
)
|
471 |
+
clear_button = gr.Button("๋ํ ์ด๊ธฐํ")
|
472 |
+
|
473 |
+
# ์ฌ์ฉ์๊ฐ ์ง๋ฌธ -> user_message -> Gemini ์ฒ๋ฆฌ -> stream_gemini_response
|
474 |
+
def user_message_wrapper(msg, history, doc_text):
|
475 |
+
"""
|
476 |
+
์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๋๋ง๋ค, doc_text(๋งํฌ๋ค์ด)๋ฅผ ์ฐธ๊ณ ํ๋๋ก
|
477 |
+
์ง๋ฌธ์ ์ฝ๊ฐ ๋ณํํด์ history์ ์ถ๊ฐํ๋ ๋ฐฉ์(๊ฐ๋จ ์์).
|
478 |
+
"""
|
479 |
+
if not doc_text:
|
480 |
+
# ์์ง ๋ณํ๋ ๋ฌธ์๊ฐ ์๋ค๋ฉด ๊ทธ๋ฅ ์ง๋ฌธ
|
481 |
+
user_query = msg
|
482 |
+
else:
|
483 |
+
# ๋ฌธ์ ๋ด์ฉ(doc_text)์ "์ฐธ๊ณ " ์์ฒญํ๋ ๊ฐ๋จ ํ๋กฌํํธ ์์
|
484 |
+
user_query = f"๋ค์ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ์ฌ ๋ต๋ณ:\n\n{doc_text}\n\n์ง๋ฌธ: {msg}"
|
485 |
+
|
486 |
+
history.append(ChatMessage(role="user", content=user_query))
|
487 |
+
return "", history
|
488 |
+
|
489 |
+
chat_input.submit(
|
490 |
+
fn=user_message_wrapper,
|
491 |
+
inputs=[chat_input, chat_history, md_state],
|
492 |
+
outputs=[chat_input, chat_history]
|
493 |
+
).then(
|
494 |
+
fn=stream_gemini_response,
|
495 |
+
inputs=[chat_input, chat_history],
|
496 |
+
outputs=chat_history
|
497 |
+
).then(
|
498 |
+
fn=lambda h: h,
|
499 |
+
inputs=chat_history,
|
500 |
+
outputs=chatbot
|
501 |
+
)
|
502 |
+
|
503 |
+
clear_button.click(
|
504 |
+
fn=lambda: ([], ""),
|
505 |
+
inputs=[],
|
506 |
+
outputs=[chat_history, md_state]
|
507 |
+
).then(
|
508 |
+
fn=lambda: [],
|
509 |
+
inputs=[],
|
510 |
+
outputs=chatbot
|
511 |
+
)
|
512 |
+
|
513 |
+
|
514 |
+
##############################
|
515 |
+
# 4) ์ค์ ์คํ
|
516 |
+
##############################
|
517 |
+
if __name__ == "__main__":
|
518 |
+
# ์ฒซ ๋ฒ์งธ demo.launch() - ํตํฉ ์ฑ ์คํ
|
519 |
+
demo.launch(ssr_mode=True, debug=True)
|
520 |
+
|
521 |
+
|
522 |
+
###############################################
|
523 |
+
# ์๋๋ "Gemini ์ฑ ์ฝ๋" ์๋ณธ์ ์๋
|
524 |
+
# ๋ณ๋์ demo.launch() ๋ถ๋ถ (๋๋ฝ ์์ด ์ฃผ์ ๋ณด์กด)
|
525 |
+
###############################################
|
526 |
+
# if __name__ == "__main__":
|
527 |
+
# demo.launch(debug=True)
|
528 |
+
|