Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure gradio cc publish uploads the documentation space, if it exists. #7159

Merged
merged 5 commits into from Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/some-birds-live.md
@@ -0,0 +1,5 @@
---
"gradio": minor
---

feat:Ensure `gradio cc publish` uploads the documentation space, if it exists.
68 changes: 57 additions & 11 deletions gradio/cli/commands/components/publish.py
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path
from typing import List, Optional

import httpx
import semantic_version
from huggingface_hub import HfApi
from rich import print
Expand All @@ -31,7 +32,9 @@
---
"""

DOCKERFILE = """

def make_dockerfile(demo):
return f"""
FROM python:3.9

WORKDIR /code
Expand All @@ -51,7 +54,7 @@
GRADIO_SERVER_PORT=7860 \
SYSTEM=spaces

CMD ["python", "app.py"]
CMD ["python", "{demo}"]
"""


Expand Down Expand Up @@ -102,18 +105,27 @@ def _publish(
] = None,
source_dir: Annotated[
Optional[Path],
Option(
help="Path to the source directory of the custom component. To share with community."
),
Option(help="Path to the source directory of the custom component."),
] = None,
hf_token: Annotated[
Optional[str],
Option(
help="HuggingFace token for uploading demo. Can be omitted if already logged in via huggingface cli."
),
] = None,
prefer_local: Annotated[
bool,
Option(
help="Install the package from the local wheel in the demo space, even if it exists on PyPi."
),
] = False,
upload_source: Annotated[
bool,
Option(
help="Whether to upload the source code of the custom component, to share with the community."
),
] = True,
):
upload_source = source_dir is not None
console = Console()
dist_dir = dist_dir.resolve()
name = None
Expand Down Expand Up @@ -206,7 +218,7 @@ def _publish(
demo_dir_ = demo_dir_ or str(Path(".") / "demo")
demo_dir = Path(demo_dir_).resolve()

if upload_demo and not source_dir:
if upload_source and not source_dir:
panel = Panel(
"It is recommended that you share your [magenta]source code[/] so that others can learn from and improve your component."
)
Expand All @@ -219,10 +231,32 @@ def _publish(
source_dir_ = source_dir_ or str(Path("."))
source_dir = Path(source_dir_).resolve()
if upload_demo:
pyproject_toml_path = (
(source_dir / "pyproject.toml")
if source_dir
else Path(".") / "pyproject.toml"
)

try:
pyproject_toml = parse(pyproject_toml_path.read_text())
package_name = pyproject_toml["project"]["name"] # type: ignore
except Exception:
(package_name, version) = wheel_file.name.split("-")[:2]

try:
latest_release = httpx.get(
f"https://pypi.org/pypi/{package_name}/json"
).json()["info"]["version"]
except Exception:
latest_release = None

assert demo_dir
if not (demo_dir / "app.py").exists():
raise FileNotFoundError("app.py not found in demo directory.")
additional_reqs = [wheel_file.name]
demo_path = resolve_demo(demo_dir)

if prefer_local or not latest_release:
additional_reqs = [wheel_file.name]
else:
additional_reqs = [f"{package_name}=={latest_release}"]
if (demo_dir / "requirements.txt").exists():
reqs = (demo_dir / "requirements.txt").read_text().splitlines()
reqs += additional_reqs
Expand Down Expand Up @@ -276,7 +310,7 @@ def _publish(

readme.write_text(readme_text)
dockerfile = Path(tempdir) / "Dockerfile"
dockerfile.write_text(DOCKERFILE)
dockerfile.write_text(make_dockerfile(demo_path.name))

api = HfApi()
new_space = api.create_repo(
Expand All @@ -302,3 +336,15 @@ def _publish(
)
print("\n")
print(f"Demo uploaded to {new_space} !")


def resolve_demo(demo_dir: Path) -> Path:
_demo_dir = demo_dir.resolve()
if (_demo_dir / "space.py").exists():
return _demo_dir / "space.py"
elif (_demo_dir / "app.py").exists():
return _demo_dir / "app.py"
else:
raise FileNotFoundError(
f'Could not find "space.py" or "app.py" in "{demo_dir}".'
)