Skip to content

Commit

Permalink
Merge pull request #38 from gizatechxyz/feature/add-deployments-proofs
Browse files Browse the repository at this point in the history
Add Proof retrieval for Deployments
  • Loading branch information
Gonmeso authored Jan 31, 2024
2 parents 138e5b3 + 6e2770f commit 1d7a754
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
description: Giza CLI 0.7.0
description: Giza CLI 0.9.0
---

# Giza CLI
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/full_transpilation.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pip install -r requirements.txt
Or:

```bash
pip install giza-cli==0.7.0 onnx==1.14.1 torch==2.1.0 torchvision==0.16.0
pip install giza-cli==0.9.0 onnx==1.14.1 torch==2.1.0 torchvision==0.16.0
```

We will use the libraries for the following purposes:
Expand Down
2 changes: 1 addition & 1 deletion examples/mnist/mnist_pytorch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"Or:\n",
"\n",
"```bash\n",
"pip install giza-cli==0.7.0 onnx==1.14.1 torch==2.1.0 torchvision==0.16.0\n",
"pip install giza-cli==0.9.0 onnx==1.14.1 torch==2.1.0 torchvision==0.16.0\n",
"```\n",
"\n",
"We will use the libraries for the following purposes:\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/mnist/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
giza-cli==0.7.0
giza-cli==0.9.0
onnx==1.14.1
tf2onnx==1.15.1
torch==2.1.0
Expand Down
2 changes: 1 addition & 1 deletion giza/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os

__version__ = "0.8.0"
__version__ = "0.9.0"
# Until DNS is fixed
API_HOST = os.environ.get("GIZA_API_HOST", "https://api.gizatech.xyz")
111 changes: 110 additions & 1 deletion giza/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from giza.schemas.jobs import Job, JobCreate
from giza.schemas.message import Msg
from giza.schemas.models import Model, ModelCreate, ModelList, ModelUpdate
from giza.schemas.proofs import Proof
from giza.schemas.proofs import Proof, ProofList
from giza.schemas.token import TokenResponse
from giza.schemas.versions import Version, VersionCreate, VersionList, VersionUpdate
from giza.schemas.workspaces import Workspace
Expand Down Expand Up @@ -512,6 +512,115 @@ def list(self, model_id: int, version_id: int) -> DeploymentsList:
__root__=[Deployment(**deployment) for deployment in response.json()]
)

@auth
def list_proofs(
self, model_id: int, version_id: int, deployment_id: int
) -> ProofList:
"""
List proofs.
Returns:
A list of proofs created by the user
"""
headers = copy.deepcopy(self.default_headers)
headers.update(self._get_auth_header())

response = self.session.get(
os.path.join(
self.url,
self.MODELS_ENDPOINT,
str(model_id),
self.VERSIONS_ENDPOINT,
str(version_id),
self.DEPLOYMENTS_ENDPOINT,
str(deployment_id),
"proofs",
),
headers=headers,
)
self._echo_debug(str(response))

response.raise_for_status()

return ProofList(__root__=[Proof(**proof) for proof in response.json()])

@auth
def get_proof(
self, model_id: int, version_id: int, deployment_id: int, proof_id: int
) -> Proof:
"""
Return information about a specific proof.
`proof_if` is the identifier of the proof that can be a integer or the request id.
Returns:
A proof created by the user
"""
headers = copy.deepcopy(self.default_headers)
headers.update(self._get_auth_header())

response = self.session.get(
os.path.join(
self.url,
self.MODELS_ENDPOINT,
str(model_id),
self.VERSIONS_ENDPOINT,
str(version_id),
self.DEPLOYMENTS_ENDPOINT,
str(deployment_id),
"proofs",
str(proof_id),
),
headers=headers,
)
self._echo_debug(str(response))

response.raise_for_status()

return Proof(**response.json())

@auth
def download_proof(
self, model_id: int, version_id: int, deployment_id: int, proof_id: int
) -> bytes:
"""
Download a proof.
Args:
proof_id: Proof identifier
Returns:
The proof binary file
"""
headers = copy.deepcopy(self.default_headers)
headers.update(self._get_auth_header())

response = self.session.get(
os.path.join(
self.url,
self.MODELS_ENDPOINT,
str(model_id),
self.VERSIONS_ENDPOINT,
str(version_id),
self.DEPLOYMENTS_ENDPOINT,
str(deployment_id),
"proofs",
f"{proof_id}:download",
),
headers=headers,
)

self._echo_debug(str(response))
response.raise_for_status()

url = response.json()["download_url"]

download_response = self.session.get(url)

self._echo_debug(str(download_response))
download_response.raise_for_status()

return download_response.content

@auth
def get(self, model_id: int, version_id: int, deployment_id: int) -> Deployment:
"""
Expand Down
175 changes: 168 additions & 7 deletions giza/commands/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from giza.frameworks import cairo
from giza.options import DEBUG_OPTION
from giza.schemas.deployments import DeploymentsList
from giza.schemas.proofs import Proof, ProofList
from giza.utils import echo, get_response_info
from giza.utils.enums import Framework, ServiceSize

Expand All @@ -20,10 +21,13 @@
def deploy(
data: str = typer.Argument(None),
model_id: int = typer.Option(
None, help="The ID of the model where a deployment will be created"
None,
"--model-id",
"-m",
help="The ID of the model where a deployment will be created",
),
version_id: int = typer.Option(
None, help="The ID of the version that will be deployed"
None, "--version-id", "-v", help="The ID of the version that will be deployed"
),
size: ServiceSize = typer.Option(ServiceSize.S, "--size", "-s"),
framework: Framework = typer.Option(Framework.CAIRO, "--framework", "-f"),
Expand Down Expand Up @@ -73,8 +77,10 @@ def deploy(
""",
)
def list(
model_id: int = typer.Option(None, help="The ID of the model"),
version_id: int = typer.Option(None, help="The ID of the version"),
model_id: int = typer.Option(None, "--model-id", "-m", help="The ID of the model"),
version_id: int = typer.Option(
None, "--version-id", "-v", help="The ID of the version"
),
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
echo("Listing deployments ✅ ")
Expand Down Expand Up @@ -113,9 +119,13 @@ def list(
""",
)
def get(
model_id: int = typer.Option(None, help="The ID of the model"),
version_id: int = typer.Option(None, help="The ID of the version"),
deployment_id: int = typer.Option(None, help="The ID of the version"),
model_id: int = typer.Option(None, "--model-id", "-m", help="The ID of the model"),
version_id: int = typer.Option(
None, "--version-id", "-v", help="The ID of the version"
),
deployment_id: int = typer.Option(
None, "--deployment-id", "-d", help="The ID of the version"
),
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
echo(f"Getting deployment {deployment_id} ✅ ")
Expand All @@ -142,3 +152,154 @@ def get(
raise e
sys.exit(1)
print_json(deployment.json())


@app.command(
name="list-proofs",
short_help="🔒 List proofs from a deployment.",
help="""🔒 List proofs from a deployment.
This command retrieves and displays the proofs generated by a specific deployment stored in the server.
The proofs' information is printed in a json format for easy readability and further processing.
If the deployment is not available, an error message is printed.
""",
)
def list_proofs(
model_id: int = typer.Option(None, "--model-id", "-m", help="The ID of the model"),
version_id: int = typer.Option(
None, "--version-id", "-v", help="The ID of the version"
),
deployment_id: int = typer.Option(
None, "--deployment-id", "-d", help="The ID of the version"
),
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
echo(f"Getting proofs from deployment {deployment_id} ✅ ")
try:
client = DeploymentsClient(API_HOST)
proofs: ProofList = client.list_proofs(model_id, version_id, deployment_id)
except ValidationError as e:
echo.error("Could not retrieve proofs from deployment")
echo.error("Review the provided information")
if debug:
raise e
echo.error(str(e))
sys.exit(1)
except HTTPError as e:
info = get_response_info(e.response)
echo.error(f"⛔️Could not get deployment {deployment_id}")
echo.error(f"⛔️Detail -> {info.get('detail')}⛔️")
echo.error(f"⛔️Status code -> {info.get('status_code')}⛔️")
echo.error(f"⛔️Error message -> {info.get('content')}⛔️")
echo.error(
f"⛔️Request ID: Give this to an administrator to trace the error -> {info.get('request_id')}⛔️"
) if info.get("request_id") else None
if debug:
raise e
sys.exit(1)
print_json(proofs.json())


@app.command(
name="get-proof",
short_help="🔒 Retrieves information about a proof from a deployment.",
help="""🔒 Retrieves information about a proof from a deployment.
This command retrieves and displays the proof generated by a specific deployment stored in the server.
The proof information is printed in a json format for easy readability and further processing.
If the deployment is not available, an error message is printed.
""",
)
def get_proof(
model_id: int = typer.Option(None, "--model-id", "-m", help="The ID of the model"),
version_id: int = typer.Option(
None, "--version-id", "-v", help="The ID of the version"
),
deployment_id: int = typer.Option(
None, "--deployment-id", "-d", help="The ID of the version"
),
proof_id: str = typer.Option(
None, "--proof-id", "-p", help="The ID or request id of the proof"
),
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
echo(f"Getting proof from deployment {deployment_id} ✅ ")
try:
client = DeploymentsClient(API_HOST)
proof: Proof = client.get_proof(model_id, version_id, deployment_id, proof_id)
except ValidationError as e:
echo.error("Could not retrieve proof from deployment")
echo.error("Review the provided information")
if debug:
raise e
echo.error(str(e))
sys.exit(1)
except HTTPError as e:
info = get_response_info(e.response)
echo.error(f"⛔️Could not get deployment {deployment_id}")
echo.error(f"⛔️Detail -> {info.get('detail')}⛔️")
echo.error(f"⛔️Status code -> {info.get('status_code')}⛔️")
echo.error(f"⛔️Error message -> {info.get('content')}⛔️")
echo.error(
f"⛔️Request ID: Give this to an administrator to trace the error -> {info.get('request_id')}⛔️"
) if info.get("request_id") else None
if debug:
raise e
sys.exit(1)
print_json(proof.json())


@app.command(
name="download-proof",
short_help="🔒 Downloads a proof from a deployment to the specified path.",
help="""🔒 Downloads a proof from a deployment to the specified path.
This command retrieves the proof created in Giza from a specific deployment.
The proof information is stored in the specified path, defaulting to the current path.
If the deployment is not available, an error message is printed.
""",
)
def download_proof(
model_id: int = typer.Option(None, "--model-id", "-m", help="The ID of the model"),
version_id: int = typer.Option(
None, "--version-id", "-v", help="The ID of the version"
),
deployment_id: int = typer.Option(
None, "--deployment-id", "-d", help="The ID of the version"
),
proof_id: str = typer.Option(
None, "--proof-id", "-p", help="The ID or request id of the proof"
),
output_path: str = typer.Option(
"zk.proof",
"--output-path",
"-o",
help="The path where the proof will be stored",
),
debug: Optional[bool] = DEBUG_OPTION,
) -> None:
echo(f"Getting proof from deployment {deployment_id} ✅ ")
try:
client = DeploymentsClient(API_HOST)
proof: bytes = client.download_proof(
model_id, version_id, deployment_id, proof_id
)
with open(output_path, "wb") as f:
f.write(proof)
except ValidationError as e:
echo.error("Could not retrieve proof from deployment")
echo.error("Review the provided information")
if debug:
raise e
echo.error(str(e))
sys.exit(1)
except HTTPError as e:
info = get_response_info(e.response)
echo.error(f"⛔️Could not get deployment {deployment_id}")
echo.error(f"⛔️Detail -> {info.get('detail')}⛔️")
echo.error(f"⛔️Status code -> {info.get('status_code')}⛔️")
echo.error(f"⛔️Error message -> {info.get('content')}⛔️")
echo.error(
f"⛔️Request ID: Give this to an administrator to trace the error -> {info.get('request_id')}⛔️"
) if info.get("request_id") else None
if debug:
raise e
sys.exit(1)
echo(f"Proof downloaded to {output_path} ✅ ")
4 changes: 4 additions & 0 deletions giza/schemas/proofs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ class Proof(BaseModel):
cairo_execution_time: Optional[float] = None
metrics: Optional[dict] = None
created_date: datetime.datetime


class ProofList(BaseModel):
__root__: list[Proof]
Loading

0 comments on commit 1d7a754

Please sign in to comment.