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 3 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.
1 change: 0 additions & 1 deletion gradio/cli/commands/components/docs.py
Expand Up @@ -6,7 +6,6 @@

import requests
import tomlkit as toml
from rich import print
from typer import Argument, Option
from typing_extensions import Annotated

Expand Down
44 changes: 36 additions & 8 deletions gradio/cli/commands/components/publish.py
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path
from typing import List, Optional

import requests
pngwn marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -112,8 +115,9 @@ def _publish(
help="HuggingFace token for uploading demo. Can be omitted if already logged in via huggingface cli."
),
] = None,
prefer_local: Annotated[bool, Option(help="Install")] = False,
pngwn marked this conversation as resolved.
Show resolved Hide resolved
upload_source: Annotated[bool, Option(help="Upload source code")] = True,
):
upload_source = source_dir is not None
console = Console()
dist_dir = dist_dir.resolve()
name = None
Expand Down Expand Up @@ -206,7 +210,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 +223,22 @@ def _publish(
source_dir_ = source_dir_ or str(Path("."))
source_dir = Path(source_dir_).resolve()
if upload_demo:
package_name, version = wheel_file.name.split("-")[:2]
pngwn marked this conversation as resolved.
Show resolved Hide resolved

try:
latest_release = requests.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 +292,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 +318,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}".'
)