File size: 1,491 Bytes
f5c57cb
e600655
ebe3ec6
 
 
 
 
8001cbd
ebe3ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8001cbd
ebe3ec6
 
8001cbd
 
ebe3ec6
 
8001cbd
ebe3ec6
8001cbd
 
ebe3ec6
8001cbd
 
ebe3ec6
 
 
f5c57cb
e600655
f5c57cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
from scraper import scrape_to_markdown
import mistune


def process(url: str):
    """
    Get website link -> Extract content -> Save in Markdown file -> Generate HTML for display in user interface.
    """
    # اجرای تابع وب‌اسکرپر
    result = scrape_to_markdown(url)

    # اگر خروجی tuple بود، فقط markdown (عنصر اول) رو نگه داریم
    md = result[0] if isinstance(result, tuple) else result  

    # ذخیره‌سازی در فایل Markdown
    filename = "output.md"
    with open(filename, "w", encoding="utf-8") as f:
        f.write(md)

    # تبدیل به HTML برای نمایش
    html = mistune.create_markdown()(md)

    return md, filename, html


# رابط کاربری Gradio
with gr.Blocks(css="style.css") as demo:
    gr.Markdown("## 🌐 Web to Markdown Scraper and Converter")

    with gr.Row():
        url_input = gr.Textbox(label="🔗 Enter the website link", placeholder="https://example.com")
        submit_btn = gr.Button("🔥 Content Extraction")

    with gr.Tab("📄 Markdown"):
        md_output = gr.Textbox(label="Markdown Content", lines=20)

    with gr.Tab("💾 Download"):
        file_output = gr.File(label="Download Markdown file")

    with gr.Tab("🌍 Preview HTML"):
        html_output = gr.HTML(label="HTML View")

    submit_btn.click(process, inputs=url_input, outputs=[md_output, file_output, html_output])


if __name__ == "__main__":
    demo.launch()