-
Notifications
You must be signed in to change notification settings - Fork 3
Demo model service #140
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
Merged
Merged
Demo model service #140
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6de1d1c
demo service wip
themoenen 7d06a62
Moved demo service into subprocess
themoenen af4bd85
Updated pyproject.toml
themoenen 3cf8e96
docstrings
themoenen 1220778
typo
themoenen 3a864db
Demo service improvements
themoenen 8358c68
fixes in demo model process
themoenen 000d5f1
black
themoenen cf525ea
demo ux improvements
themoenen bb09a7e
reset pyproject lock
themoenen 1417252
Remove critical logger
themoenen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| """ | ||
| Launch and shutdown of the model service demo. | ||
| See model_service_demo.py for more info and the actual demo service. | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
| import threading | ||
| import subprocess | ||
| from openad.helpers.general import confirm_prompt | ||
| from openad.helpers.output import output_error, output_text, output_success, output_warning | ||
|
|
||
| DEMO_PROCESS = None | ||
|
|
||
|
|
||
| def launch_model_service_demo(restart=False, debug=False): | ||
| """ | ||
| Spin up the model service demo in a subprocess. | ||
| """ | ||
|
|
||
| global DEMO_PROCESS | ||
|
|
||
| # Process already running | ||
| if DEMO_PROCESS: | ||
| # Try restart | ||
| if restart or debug: | ||
| success = terminate_model_service_demo() | ||
| if not success: | ||
| return | ||
|
|
||
| # Remind instructions | ||
| else: | ||
| return _print_success(new=False) | ||
|
|
||
| # Make sure openad_service_utils are installed | ||
| utils_installed = _verify_utils_installed() | ||
| if not utils_installed: | ||
| return | ||
|
|
||
| service_path = os.path.join(os.path.dirname(__file__), "model_service_demo.py") | ||
| command = [sys.executable, service_path] | ||
|
|
||
| try: | ||
| DEMO_PROCESS = subprocess.Popen( | ||
| command, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.STDOUT, # Redirect stderr to stdout for combined logging | ||
| text=True, # Decode output as text (Python 3.6+) | ||
| bufsize=1, # Line-buffered output | ||
| ) | ||
|
|
||
| # Log the subprocess' stdout | ||
| if debug: | ||
|
|
||
| def log_output(): | ||
| for line in iter(DEMO_PROCESS.stdout.readline, ""): | ||
| print(f"DEMO SERVICE: {line.strip()}") | ||
| DEMO_PROCESS.stdout.close() | ||
|
|
||
| # Start the logging thread | ||
| log_thread = threading.Thread(target=log_output, daemon=True) | ||
| log_thread.start() | ||
|
|
||
| # Success message | ||
| return _print_success() | ||
| except Exception as e: # pylint: disable=broad-except | ||
| return output_error(f"Failed to start model service demo: {e}") | ||
|
|
||
|
|
||
| def _verify_utils_installed(): | ||
| """ | ||
| Make sure openad_service_utils are installed. | ||
| """ | ||
| try: | ||
| from openad_service_utils import start_server | ||
|
|
||
| return True | ||
| except ImportError: | ||
| msg = ( | ||
| "Install openad_service_utils to use the demo model service:\n" | ||
| "<cmd>pip install git+https://github.com/acceleratedscience/openad_service_utils.git@0.3.1</cmd>" | ||
| ) | ||
| output_warning(msg, return_val=False) | ||
| return False | ||
|
|
||
|
|
||
| def _print_success(new=True): | ||
| """ | ||
| Success message & instructions. | ||
| """ | ||
| main_msg = ( | ||
| ( | ||
| "<success>Demo model service started at <yellow>http://localhost:8034</yellow></success>\n" | ||
| f"<soft>PID: {DEMO_PROCESS.pid}</soft>" | ||
| ) | ||
| if new | ||
| else ( | ||
| "<yellow>Demo model service already running at <reset>http://localhost:8034</reset></yellow>\n" | ||
| f"<soft>PID: {DEMO_PROCESS.pid} / To restart the demo service, run <cmd>model service demo restart</cmd></soft>" | ||
| ) | ||
| ) | ||
|
|
||
| msg = [ | ||
| main_msg, | ||
| "", | ||
| "Next up, run:", | ||
| "<cmd>catalog model service from remote 'http://localhost:8034' as demo_service</cmd>", | ||
| "", | ||
| "To test the service:", | ||
| "<cmd>demo_service ?</cmd>", | ||
| "<cmd>demo_service get molecule property num_atoms for CC</cmd>", | ||
| "<cmd>demo_service get molecule property num_atoms for NCCc1c[nH]c2ccc(O)cc12</cmd>", | ||
| ] | ||
| return output_text("\n".join(msg), edge=True, pad=1) | ||
|
|
||
|
|
||
| def terminate_model_service_demo(): | ||
| """ | ||
| Terminate the model service demo. | ||
| """ | ||
| global DEMO_PROCESS | ||
| if DEMO_PROCESS is None: | ||
| return True | ||
|
|
||
| if DEMO_PROCESS: | ||
| try: | ||
| DEMO_PROCESS.terminate() | ||
| DEMO_PROCESS.wait(timeout=1) | ||
| output_success(f"Demo model service terminated - PID: {DEMO_PROCESS.pid}", return_val=False) | ||
| DEMO_PROCESS = None | ||
| return True | ||
| except Exception as err1: # pylint: disable=broad-except | ||
| try: | ||
| # Force kill if terminate fails | ||
| DEMO_PROCESS.kill() | ||
| DEMO_PROCESS.wait(timeout=5) | ||
| output_success(f"Demo model service killed - PID: {DEMO_PROCESS.pid}", return_val=False) | ||
| DEMO_PROCESS = None | ||
| return True | ||
| except Exception as err2: # pylint: disable=broad-except | ||
| output_error( | ||
| [f"Failed to terminate model service demo with PID: {DEMO_PROCESS.pid}", err1, err2], | ||
| return_val=False, | ||
| ) | ||
| return False | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """ | ||
| Model service demo used for tutorials and examples. | ||
|
|
||
| To launch: | ||
| model service demo | ||
|
|
||
| Model repo: | ||
| https://github.com/acceleratedscience/openad-service-demo | ||
| """ | ||
|
|
||
| import os | ||
| from typing import Any | ||
| from pydantic.v1 import Field | ||
| from openad_service_utils import ( | ||
| start_server, | ||
| SimplePredictor, | ||
| PredictorTypes, | ||
| DomainSubmodule, | ||
| ) | ||
|
|
||
|
|
||
| # Model imports | ||
| from rdkit import Chem | ||
|
|
||
|
|
||
| class DemoPredictor(SimplePredictor): | ||
| """ | ||
| Return the number of atoms in a molecule. | ||
| """ | ||
|
|
||
| # fmt:off | ||
| domain: DomainSubmodule = DomainSubmodule("molecules") # <-- edit here | ||
| algorithm_name: str = "rdkit" # <-- edit here | ||
| algorithm_application: str = "num_atoms" # <-- edit here | ||
| algorithm_version: str = "v0" | ||
| property_type: PredictorTypes = PredictorTypes.MOLECULE # <-- edit here | ||
| # fmt:on | ||
|
|
||
| # User provided params for api / model inference | ||
| batch_size: int = Field(description="Prediction batch size", default=128) | ||
| workers: int = Field(description="Number of data loading workers", default=8) | ||
| device: str = Field(description="Device to be used for inference", default="cpu") | ||
|
|
||
| def setup(self): | ||
| """Model setup. Loads the model and tokenizer, if any. Runs once. | ||
|
|
||
| To wrap a model, copy and modify the standalone model setup and load | ||
| code here. Remember to change variables to instance variables, so they | ||
| can be used in the `predict` method. | ||
| """ | ||
| self.model = None | ||
| self.tokenizer = [] | ||
| self.model_path = os.path.join(self.get_model_location(), "model.ckpt") # load model | ||
|
|
||
| def predict(self, sample: Any): | ||
| """ | ||
| Run predictions. | ||
| """ | ||
| # -----------------------User Code goes in here------------------------ | ||
| smiles = sample | ||
| mol = Chem.MolFromSmiles(smiles) # pylint: disable=no-member | ||
| num_atoms = mol.GetNumAtoms() | ||
| result = num_atoms | ||
| # --------------------------------------------------------------------- | ||
| return result | ||
|
|
||
|
|
||
| # Register the class in global scope | ||
| DemoPredictor.register(no_model=True) | ||
|
|
||
| if __name__ == "__main__": | ||
| # Start the server | ||
| start_server(port=8034) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.