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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor catalog command to show local catalog by default #1420

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions ersilia/cli/commands/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ def catalog_cmd():
@ersilia_cli.command(help="List a catalog of models")
@click.option(
"-l",
"--local/--hub",
"--local",
is_flag=True,
default=False,
help="Show catalog of models available in the local computer",
)
@click.option(
"-h",
"--hub",
is_flag=True,
default=False,
help="Show catalog of models available in the model hub",
)
@click.option(
"--file_name", "-f", default=None, type=click.STRING, help="Catalog file name"
)
Expand Down Expand Up @@ -48,6 +55,7 @@ def catalog_cmd():
)
def catalog(
local=False,
hub=False,
file_name=None,
browser=False,
more=False,
Expand Down Expand Up @@ -78,32 +86,46 @@ def catalog(
except Exception as e:
click.echo(click.style(f"Error fetching model metadata: {e}", fg="red"))
return
if local is True and browser is True:

# The idea here is to deter the user from running ersilia catalog --local --hub
if local and hub:
click.echo(
click.style(
"You cannot show the local model catalog in the browser",
fg="red",
)
"Error: Cannot show local and hub models together", fg="red"
),
err=True,
)
return
if more:
only_identifier = False
else:
only_identifier = True
mc = ModelCatalog(only_identifier=only_identifier)
if browser:
mc.airtable()
return
catalog_table = mc.local() if local else mc.hub()

mc = ModelCatalog()
mc.only_identifier = False if more else True

if local and not catalog_table.data:
click.echo(
if hub:
if browser:
mc.airtable()
return

catalog_table = mc.hub()

else: # This will work even if the user doesn't explicitly specify the --local flag
if browser:
click.echo(
click.style(
"Error: Cannot show local models in the browser.\nPlease use the --hub option to see models in the browser.",
fg="red"
)
)
return
catalog_table = mc.local()
if not catalog_table.data:
click.echo(
click.style(
"No local model is available. Please fetch a model by running 'ersilia fetch' command",
"No local models available. Please fetch a model by running 'ersilia fetch' command",
fg="red",
)
)
)
return
return

if file_name is None:
catalog = catalog_table.as_table() if as_table else catalog_table.as_json()
else:
Expand Down
2 changes: 1 addition & 1 deletion ersilia/db/hubdata/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class JsonModelsInterface(ErsiliaBase):
def __init__(self, config_json):
def __init__(self, config_json=None):
ErsiliaBase.__init__(self, config_json=config_json)
self.json_file_name = MODELS_JSON
self.url = f"https://{ERSILIA_MODEL_HUB_S3_BUCKET}.s3.eu-central-1.amazonaws.com/{MODELS_JSON}"
Expand Down
1 change: 1 addition & 0 deletions ersilia/hub/content/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ def __init__(self, config_json=None):
self.config_json = config_json

def _get(self, model_id, slug):
print(model_id)
lc = LocalCard(config_json=self.config_json)
card = lc.get(model_id, slug)
if card is not None:
Expand Down
81 changes: 21 additions & 60 deletions ersilia/hub/content/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from .card import ModelCard
from ... import ErsiliaBase
from ...utils.identifiers.model import ModelIdentifier
from ...auth.auth import Auth
from ...default import GITHUB_ORG, BENTOML_PATH, MODEL_SOURCE_FILE
from ...db.hubdata.interfaces import JsonModelsInterface
from ...default import BENTOML_PATH, MODEL_SOURCE_FILE
from ...default import TableConstants
from ... import logger

Expand Down Expand Up @@ -238,68 +238,29 @@ def airtable(self):
if webbrowser:
webbrowser.open("https://airtable.com/shrUcrUnd7jB9ChZV") #TODO Hardcoded

def _get_all_github_public_repos(self):
url = "https://api.github.com/users/{0}/repos".format(GITHUB_ORG)
while url:
response = requests.get(url, params={"per_page": 100})
response.raise_for_status()
yield from response.json()
if "next" in response.links:
url = response.links["next"]["url"] # get the next page
else:
break # no more pages, stop the loop

def github(self):
"""List models available in the GitHub model hub repository"""
if Github is None:
token = None
else:
token = Auth().oauth_token()
logger.debug(
"Looking for model repositories in {0} organization".format(GITHUB_ORG)
)
if token:
self.logger.debug("Token provided: ***")
g = Github(token)
repo_list = [i for i in g.get_user().get_repos()]
repos = []
for r in repo_list:
owner, name = r.full_name.split("/")
if owner != GITHUB_ORG:
continue
repos += [name]
else:
self.logger.debug("Token not provided!")
repos = []
for repo in self._get_all_github_public_repos():
repos += [repo["name"]]
models = []
for repo in repos:
if self._is_eos(repo):
models += [repo]
logger.info("Found {0} models".format(len(models)))
return models

def hub(self):
"""List models available in Ersilia model hub repository"""
"""List models available in Ersilia model hub from the S3 JSON"""
mc = ModelCard()
models = self.github()
if self.only_identifier:
R = []
for model_id in models:
R += [[model_id]]
return CatalogTable(R, columns=["Identifier"])
else:
R = []
for model_id in models:
card = mc.get(model_id)
if card is None:
continue
slug = self._get_slug(card)
title = self._get_title(card)
R += [[model_id, slug, title]]
return CatalogTable(R, columns=["Identifier", "Slug", "Title"])
ji = JsonModelsInterface()
models = ji.items_all()
R = []
for model in models:
status = self._get_status(model)
if status == "In Progress":
continue

identifier = self._get_item(model, "identifier")
if self.only_identifier:
R += [[identifier]]
else:
slug = self._get_slug(model)
title = self._get_title(model)
R += [[identifier, slug, title]]

columns = ["Identifier"] if self.only_identifier else ["Identifier", "Slug", "Title"]
return CatalogTable(R, columns=columns)

def local(self):
"""List models available locally"""
mc = ModelCard()
Expand Down
Loading