Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
CHANGED
|
@@ -181,7 +181,9 @@ def process_pdf(
|
|
| 181 |
# プリセット選択時もテキストボックスを編集可能にする
|
| 182 |
system_instruction = instruction_text or SYSTEM_PROMPTS[preset_name]
|
| 183 |
|
| 184 |
-
progress(
|
|
|
|
|
|
|
| 185 |
|
| 186 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 187 |
# PDFファイルパス
|
|
@@ -193,11 +195,11 @@ def process_pdf(
|
|
| 193 |
)
|
| 194 |
|
| 195 |
# PDFを分割
|
| 196 |
-
|
| 197 |
split_pdf_paths = split_pdf(temp_pdf_path, temp_dir, pages_per_chunk)
|
| 198 |
|
| 199 |
total_chunks = len(split_pdf_paths)
|
| 200 |
-
|
| 201 |
|
| 202 |
# 各チャンクを最大3並列で処理
|
| 203 |
markdown_results = {}
|
|
@@ -205,7 +207,7 @@ def process_pdf(
|
|
| 205 |
|
| 206 |
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
| 207 |
future_map = {}
|
| 208 |
-
for
|
| 209 |
future = executor.submit(
|
| 210 |
process_pdf_chunk,
|
| 211 |
pdf_path=pdf_path,
|
|
@@ -228,19 +230,20 @@ def process_pdf(
|
|
| 228 |
markdown_results[start_page] = result
|
| 229 |
completed += 1
|
| 230 |
progress_value = 0.3 + 0.6 * (completed / total_chunks)
|
| 231 |
-
|
|
|
|
| 232 |
progress_value,
|
| 233 |
-
|
| 234 |
)
|
| 235 |
|
| 236 |
# 結果を結合
|
| 237 |
-
|
| 238 |
|
| 239 |
combined_markdown = "\n\n".join(
|
| 240 |
markdown_results[page] for page in sorted(markdown_results.keys())
|
| 241 |
)
|
| 242 |
|
| 243 |
-
|
| 244 |
|
| 245 |
return combined_markdown
|
| 246 |
|
|
@@ -254,6 +257,17 @@ def update_instruction_text(preset_name: str):
|
|
| 254 |
return gr.update(value=SYSTEM_PROMPTS[preset_name])
|
| 255 |
|
| 256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
def resolve_pdf_path(uploaded_pdf) -> str:
|
| 258 |
"""
|
| 259 |
GradioのFileコンポーネントから渡される値を安全にファイルパスへ変換
|
|
|
|
| 181 |
# プリセット選択時もテキストボックスを編集可能にする
|
| 182 |
system_instruction = instruction_text or SYSTEM_PROMPTS[preset_name]
|
| 183 |
|
| 184 |
+
progress_cb = progress if callable(progress) else (lambda *args, **kwargs: None)
|
| 185 |
+
|
| 186 |
+
log_progress(progress_cb, 0.1, "PDFを読み込み中...")
|
| 187 |
|
| 188 |
with tempfile.TemporaryDirectory() as temp_dir:
|
| 189 |
# PDFファイルパス
|
|
|
|
| 195 |
)
|
| 196 |
|
| 197 |
# PDFを分割
|
| 198 |
+
log_progress(progress_cb, 0.2, "PDFを分割中...")
|
| 199 |
split_pdf_paths = split_pdf(temp_pdf_path, temp_dir, pages_per_chunk)
|
| 200 |
|
| 201 |
total_chunks = len(split_pdf_paths)
|
| 202 |
+
log_progress(progress_cb, 0.3, f"{total_chunks}個のチャンクに分割完了")
|
| 203 |
|
| 204 |
# 各チャンクを最大3並列で処理
|
| 205 |
markdown_results = {}
|
|
|
|
| 207 |
|
| 208 |
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
| 209 |
future_map = {}
|
| 210 |
+
for start_page, pdf_path in split_pdf_paths:
|
| 211 |
future = executor.submit(
|
| 212 |
process_pdf_chunk,
|
| 213 |
pdf_path=pdf_path,
|
|
|
|
| 230 |
markdown_results[start_page] = result
|
| 231 |
completed += 1
|
| 232 |
progress_value = 0.3 + 0.6 * (completed / total_chunks)
|
| 233 |
+
log_progress(
|
| 234 |
+
progress_cb,
|
| 235 |
progress_value,
|
| 236 |
+
f"チャンク {completed}/{total_chunks} を処理中...",
|
| 237 |
)
|
| 238 |
|
| 239 |
# 結果を結合
|
| 240 |
+
log_progress(progress_cb, 0.9, "結果を結合中...")
|
| 241 |
|
| 242 |
combined_markdown = "\n\n".join(
|
| 243 |
markdown_results[page] for page in sorted(markdown_results.keys())
|
| 244 |
)
|
| 245 |
|
| 246 |
+
log_progress(progress_cb, 1.0, "完了!")
|
| 247 |
|
| 248 |
return combined_markdown
|
| 249 |
|
|
|
|
| 257 |
return gr.update(value=SYSTEM_PROMPTS[preset_name])
|
| 258 |
|
| 259 |
|
| 260 |
+
def log_progress(progress_cb, value: float, desc: str) -> None:
|
| 261 |
+
"""進捗バー更新と同時にログへも出力"""
|
| 262 |
+
|
| 263 |
+
try:
|
| 264 |
+
progress_cb(value, desc=desc)
|
| 265 |
+
except TypeError:
|
| 266 |
+
progress_cb(value)
|
| 267 |
+
|
| 268 |
+
print(f"[Progress] {value * 100:.0f}% - {desc}", flush=True)
|
| 269 |
+
|
| 270 |
+
|
| 271 |
def resolve_pdf_path(uploaded_pdf) -> str:
|
| 272 |
"""
|
| 273 |
GradioのFileコンポーネントから渡される値を安全にファイルパスへ変換
|