|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | """ML Streaming utility module."""
|
| 3 | +import os |
3 | 4 | import re
|
| 5 | +from ..pymilo_param import URL_REGEX |
4 | 6 |
|
5 | 7 |
|
6 | 8 | def validate_websocket_url(url: str) -> str:
|
@@ -39,3 +41,71 @@ def validate_http_url(url: str) -> str:
|
39 | 41 | if not re.match(full_pattern, url):
|
40 | 42 | return False, None
|
41 | 43 | return True, url
|
| 44 | + |
| 45 | + |
| 46 | +def generate_dockerfile( |
| 47 | + dockerfile_name="Dockerfile", |
| 48 | + model_path=None, |
| 49 | + compression='NULL', |
| 50 | + protocol='REST', |
| 51 | + port=8000, |
| 52 | + init_model=None, |
| 53 | + bare=False |
| 54 | +): |
| 55 | + """ |
| 56 | + Generate a Dockerfile for running a PyMilo server with specified configurations. |
| 57 | +
|
| 58 | + :param dockerfile_name: Name of the dockerfile. |
| 59 | + :type dockerfile_name: str |
| 60 | + :param model_path: Path or URL to the exported model JSON file. |
| 61 | + :type model_path: str |
| 62 | + :param compression: Compression method (default: NULL). |
| 63 | + :type compression: str |
| 64 | + :param protocol: Communication protocol (default: REST). |
| 65 | + :type protocol: str |
| 66 | + :param port: Port for the PyMilo server (default: 8000). |
| 67 | + :type port: int |
| 68 | + :param init_model: The model that the server initialized with. |
| 69 | + :type init_model: boolean |
| 70 | + :param bare: A flag that sets if the server runs without an internal ML model. |
| 71 | + :type bare: boolean |
| 72 | + """ |
| 73 | + dockerfile_content = f"""# Use an official Python runtime as a parent image |
| 74 | +FROM python:3.11-slim |
| 75 | +
|
| 76 | +# Set the working directory in the container |
| 77 | +WORKDIR /app |
| 78 | +
|
| 79 | +# Install pymilo |
| 80 | +RUN pip install pymilo[streaming] |
| 81 | + """ |
| 82 | + is_url = False |
| 83 | + if model_path: |
| 84 | + if re.match(URL_REGEX, model_path): |
| 85 | + is_url = True |
| 86 | + else: |
| 87 | + dockerfile_content += f"\nCOPY {os.path.basename(model_path)} /app/model.json" |
| 88 | + |
| 89 | + # Expose the specified port |
| 90 | + dockerfile_content += f"\nEXPOSE {port}" |
| 91 | + |
| 92 | + cmd = "CMD [\"python\", \"-m\", \"pymilo\"" |
| 93 | + cmd += f", \"--compression\", \"{compression}\"" |
| 94 | + cmd += f", \"--protocol\", \"{protocol}\"" |
| 95 | + cmd += f", \"--port\", \"{port}\"" |
| 96 | + |
| 97 | + if model_path: |
| 98 | + if is_url: |
| 99 | + cmd += f", \"--load\", \"{model_path}\"" |
| 100 | + else: |
| 101 | + cmd += f", \"--load\", \"/app/model.json\"" |
| 102 | + elif init_model: |
| 103 | + cmd += f", \"--init\" \"{init_model}\"" |
| 104 | + elif bare: |
| 105 | + cmd += f", \"--bare\"" |
| 106 | + |
| 107 | + cmd += "]" |
| 108 | + dockerfile_content += f"\n{cmd}\n" |
| 109 | + |
| 110 | + with open(dockerfile_name, 'w') as f: |
| 111 | + f.write(dockerfile_content) |
0 commit comments