aka7774 commited on
Commit
a050721
·
verified ·
1 Parent(s): 1ba00ff

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from selenium import webdriver
3
+ from selenium.webdriver.chrome.service import Service
4
+ from selenium.webdriver.chrome.options import Options
5
+ from webdriver_manager.chrome import ChromeDriverManager
6
+ from markdownify import markdownify as md
7
+ import time
8
+
9
+ def fetch_and_convert_to_markdown(url: str):
10
+ """
11
+ 指定されたURLからWebページのHTMLを取得し、Markdown形式に変換する関数。
12
+
13
+ Args:
14
+ url (str): 取得したいWebページのURL。
15
+
16
+ Returns:
17
+ str: Markdown形式に変換されたテキスト、またはエラーメッセージ。
18
+ """
19
+ if not url or not url.startswith(('http://', 'https://')):
20
+ return "エラー: 有効なURLを入力してください (例: https://example.com)"
21
+
22
+ print(f"URLの処理を開始します: {url}")
23
+
24
+ # WebDriverを初期化するための変数
25
+ driver = None
26
+
27
+ try:
28
+ # --- Seleniumのセットアップ ---
29
+ # Chromeのオプションを設定
30
+ options = Options()
31
+ options.add_argument("--headless") # ブラウザUIなしでバックグラウンド実行
32
+ options.add_argument("--no-sandbox")
33
+ options.add_argument("--disable-dev-shm-usage")
34
+ options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
35
+
36
+ # webdriver-managerを使ってChromeDriverを自動でセットアップ
37
+ service = Service(ChromeDriverManager().install())
38
+
39
+ # WebDriverインスタンスを作成
40
+ driver = webdriver.Chrome(service=service, options=options)
41
+
42
+ # ページの読み込みタイムアウトを30秒に設定
43
+ driver.set_page_load_timeout(30)
44
+
45
+ # --- ページの取得 ---
46
+ print("ページにアクセスしています...")
47
+ driver.get(url)
48
+
49
+ # JavaScriptで動的に読み込まれるコンテンツを待つため、数秒待機(任意)
50
+ # time.sleep(3)
51
+
52
+ # ページのHTMLソースを取得
53
+ html_content = driver.page_source
54
+ print("HTMLの取得に成功しました。")
55
+
56
+ # --- HTMLからMarkdownへの変換 ---
57
+ print("Markdownへの変換を開始します...")
58
+ # heading_style="ATX" は見出しを # や ## で表現するスタイルです
59
+ markdown_content = md(html_content, heading_style="ATX")
60
+ print("変換が完了しました。")
61
+
62
+ return markdown_content
63
+
64
+ except Exception as e:
65
+ print(f"エラーが発生しました: {e}")
66
+ return f"エラーが発生しました。\nURLが正しいか、ページが存在するか確認してください。\n\n詳細: {str(e)}"
67
+
68
+ finally:
69
+ # --- WebDriverの終了 ---
70
+ # エラーが発生しても必ずブラウザを閉じる
71
+ if driver:
72
+ driver.quit()
73
+ print("WebDriverを終了しました。")
74
+
75
+
76
+ # --- Gradio UIの構築 ---
77
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
78
+ gr.Markdown(
79
+ """
80
+ # WebページをMarkdownに変換するアプリ
81
+ URLを入力すると、そのページの内容をAIが読みやすいMarkdown形式で取得します。
82
+ """
83
+ )
84
+
85
+ with gr.Row():
86
+ url_input = gr.Textbox(
87
+ label="URL",
88
+ placeholder="例: https://ja.wikipedia.org/wiki/Markdown",
89
+ scale=4, # 横幅の比率
90
+ )
91
+ submit_button = gr.Button("取得開始", variant="primary", scale=1)
92
+
93
+ output_markdown = gr.Textbox(
94
+ label="取得結果 (Markdown)",
95
+ lines=20,
96
+ interactive=False, # ユーザーが直接編集できないようにする
97
+ show_copy_button=True, # コピーボタンを表示
98
+ )
99
+
100
+ # ボタンがクリックされた時の動作を定義
101
+ submit_button.click(
102
+ fn=fetch_and_convert_to_markdown,
103
+ inputs=url_input,
104
+ outputs=output_markdown
105
+ )
106
+
107
+ gr.Examples(
108
+ examples=[
109
+ "https://www.gradio.app/",
110
+ "https://github.com/gradio-app/gradio",
111
+ "https://huggingface.co/blog/gradio-guides-jp"
112
+ ],
113
+ inputs=url_input,
114
+ fn=fetch_and_convert_to_markdown,
115
+ outputs=output_markdown,
116
+ cache_examples=True # 例の結果をキャッシュして高速化
117
+ )
118
+
119
+ # アプリケーションの起動
120
+ if __name__ == "__main__":
121
+ demo.launch()