Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import tempfile
|
|
| 7 |
import base64
|
| 8 |
from concurrent.futures import ThreadPoolExecutor
|
| 9 |
import logging
|
| 10 |
-
import time
|
| 11 |
|
| 12 |
# ロギング設定
|
| 13 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
@@ -98,7 +98,7 @@ def process_pdf(pdf_file, progress=gr.Progress()):
|
|
| 98 |
# PDFを分割
|
| 99 |
split_pdf_paths = split_pdf(temp_pdf_path, temp_dir)
|
| 100 |
logging.info(f"Split PDF paths: {split_pdf_paths}")
|
| 101 |
-
progress(0.2, desc="PDFを分割中...")
|
| 102 |
|
| 103 |
# 並列処理でOCR変換
|
| 104 |
markdown_results = []
|
|
@@ -109,17 +109,17 @@ def process_pdf(pdf_file, progress=gr.Progress()):
|
|
| 109 |
try:
|
| 110 |
result = future.result()
|
| 111 |
markdown_results.append(result)
|
| 112 |
-
progress(0.2 + 0.6 * (i + 1) / len(futures), desc="OCR処理中...")
|
| 113 |
except Exception as e:
|
| 114 |
logging.error(f"Error processing split PDF: {e}")
|
| 115 |
markdown_results.append(f"分割PDFの処理中にエラーが発生しました: {e}")
|
| 116 |
|
| 117 |
logging.info(f"Markdown results length: {len(markdown_results)}")
|
| 118 |
-
progress(0.8, desc="結果を結合中...")# 進捗更新
|
| 119 |
# 結果を結合
|
| 120 |
combined_markdown = "\n\n".join(markdown_results)
|
| 121 |
-
progress(1.0, desc="完了")
|
| 122 |
-
time.sleep(0.5)
|
| 123 |
|
| 124 |
return combined_markdown
|
| 125 |
|
|
@@ -134,21 +134,31 @@ def create_interface():
|
|
| 134 |
pdf_input = gr.File(label="PDFファイルをアップロード", file_types=[".pdf"])
|
| 135 |
|
| 136 |
with gr.Row():
|
| 137 |
-
convert_btn = gr.Button("変換開始", variant="primary",
|
|
|
|
| 138 |
|
| 139 |
with gr.Row():
|
| 140 |
-
markdown_output = gr.Textbox(label="変換結果", lines=10,
|
|
|
|
| 141 |
|
| 142 |
with gr.Row():
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
#
|
| 147 |
-
|
| 148 |
-
() => {
|
| 149 |
-
document.getElementById('convert-button').style.backgroundColor = 'orange';
|
| 150 |
-
}
|
| 151 |
-
""")
|
| 152 |
|
| 153 |
|
| 154 |
convert_btn.click(
|
|
@@ -167,10 +177,10 @@ def create_interface():
|
|
| 167 |
|
| 168 |
# ダウンロード
|
| 169 |
download_btn.click(
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
const blob = new Blob([x], {{type: 'text/markdown;charset=utf-8'}});
|
| 175 |
const url = URL.createObjectURL(blob);
|
| 176 |
const a = document.createElement('a');
|
|
|
|
| 7 |
import base64
|
| 8 |
from concurrent.futures import ThreadPoolExecutor
|
| 9 |
import logging
|
| 10 |
+
import time
|
| 11 |
|
| 12 |
# ロギング設定
|
| 13 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
| 98 |
# PDFを分割
|
| 99 |
split_pdf_paths = split_pdf(temp_pdf_path, temp_dir)
|
| 100 |
logging.info(f"Split PDF paths: {split_pdf_paths}")
|
| 101 |
+
progress(0.2, desc="PDFを分割中...") # 進捗更新
|
| 102 |
|
| 103 |
# 並列処理でOCR変換
|
| 104 |
markdown_results = []
|
|
|
|
| 109 |
try:
|
| 110 |
result = future.result()
|
| 111 |
markdown_results.append(result)
|
| 112 |
+
progress(0.2 + 0.6 * (i + 1) / len(futures), desc="OCR処理中...") # 進捗更新
|
| 113 |
except Exception as e:
|
| 114 |
logging.error(f"Error processing split PDF: {e}")
|
| 115 |
markdown_results.append(f"分割PDFの処理中にエラーが発生しました: {e}")
|
| 116 |
|
| 117 |
logging.info(f"Markdown results length: {len(markdown_results)}")
|
| 118 |
+
progress(0.8, desc="結果を結合中...") # 進捗更新
|
| 119 |
# 結果を結合
|
| 120 |
combined_markdown = "\n\n".join(markdown_results)
|
| 121 |
+
progress(1.0, desc="完了") # 進捗更新
|
| 122 |
+
time.sleep(0.5) # 完了表示のため少し待つ
|
| 123 |
|
| 124 |
return combined_markdown
|
| 125 |
|
|
|
|
| 134 |
pdf_input = gr.File(label="PDFファイルをアップロード", file_types=[".pdf"])
|
| 135 |
|
| 136 |
with gr.Row():
|
| 137 |
+
convert_btn = gr.Button("変換開始", variant="primary",
|
| 138 |
+
elem_id="convert-button") # variantとelem_idを追加
|
| 139 |
|
| 140 |
with gr.Row():
|
| 141 |
+
markdown_output = gr.Textbox(label="変換結果", lines=10,
|
| 142 |
+
max_lines=20) # MarkdownからTextboxに変更、行数を指定
|
| 143 |
|
| 144 |
with gr.Row():
|
| 145 |
+
copy_btn = gr.Button("クリップボードにコピー")
|
| 146 |
+
download_btn = gr.Button("ダウンロード")
|
| 147 |
+
|
| 148 |
+
# JavaScriptコードを埋め込むためのHTMLコンポーネント
|
| 149 |
+
js_code = gr.HTML(
|
| 150 |
+
"""
|
| 151 |
+
<script>
|
| 152 |
+
function styleButton() {
|
| 153 |
+
document.getElementById('convert-button').style.backgroundColor = 'orange';
|
| 154 |
+
}
|
| 155 |
+
</script>
|
| 156 |
+
""",
|
| 157 |
+
visible=False, # 非表示にしておく
|
| 158 |
+
)
|
| 159 |
|
| 160 |
+
# js_code が読み込まれた後に styleButton 関数を実行
|
| 161 |
+
pdf_input.upload(None, [], [], js="styleButton")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
|
| 164 |
convert_btn.click(
|
|
|
|
| 177 |
|
| 178 |
# ダウンロード
|
| 179 |
download_btn.click(
|
| 180 |
+
None,
|
| 181 |
+
markdown_output,
|
| 182 |
+
[],
|
| 183 |
+
js=f"""(x) =>{{
|
| 184 |
const blob = new Blob([x], {{type: 'text/markdown;charset=utf-8'}});
|
| 185 |
const url = URL.createObjectURL(blob);
|
| 186 |
const a = document.createElement('a');
|