Anirudh1950's picture
Publish project from GitHub
1c6923c verified
Raw History Blame Contribute Delete
2 kB
"""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()