File size: 2,000 Bytes
1c6923c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Gradio Space: project showcase (README, figures and source files).



This project has no trained model weights in its repository, so a live

inference demo is not possible yet. The page presents the write-up and results

figures instead. See the README section "Running a live demo" for how to add one.

"""
from pathlib import Path

import gradio as gr

HERE = Path(__file__).parent
IMG_EXT = {".png", ".jpg", ".jpeg", ".gif", ".webp"}
SKIP_DIRS = {".git", "__pycache__", "node_modules"}
GALLERY_GLOBS = []


def readme_body():
    text = (HERE / "README.md").read_text(encoding="utf-8", errors="replace")
    if text.startswith("---"):
        text = text.split("---", 2)[2]  # drop the Space front matter
    return text


def gallery():
    imgs = []
    for pattern in GALLERY_GLOBS:
        imgs += [p for p in sorted(HERE.glob(pattern)) if p.suffix.lower() in IMG_EXT]
    return [(str(p), p.stem.replace("_", " ")) for p in dict.fromkeys(imgs)][:40]


def source_files():
    return sorted(
        str(p.relative_to(HERE)) for p in HERE.rglob("*")
        if p.is_file() and p.suffix in {".py", ".ipynb", ".yaml", ".yml", ".txt", ".c"}
        and not SKIP_DIRS & set(p.parts) and p.name != "app.py"
    )


def show(path):
    p = (HERE / path).resolve()
    if HERE.resolve() not in p.parents:
        return ""
    return p.read_text(encoding="utf-8", errors="replace")[:60000]


with gr.Blocks(title='Image web scraper') as demo:
    with gr.Tab("Overview"):
        gr.Markdown(readme_body())
    imgs = gallery()
    if imgs:
        with gr.Tab("Results"):
            gr.Gallery(imgs, columns=3, height=640)
    with gr.Tab("Source"):
        files = source_files()
        pick = gr.Dropdown(files, value=files[0] if files else None, label="File")
        code = gr.Code(value=show(files[0]) if files else "", language="python", lines=30)
        pick.change(show, pick, code)

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