From a93d59602f43fd7efa89fc28fcb78b83e6d32cef Mon Sep 17 00:00:00 2001 From: Chris Sreesangkom Date: Wed, 24 Jul 2024 10:51:31 -0400 Subject: [PATCH] format string operator --- substrate/GEN_VERSION | 2 +- substrate/__init__.py | 114 +++++++++------------------ substrate/core/future_directive.py | 27 ++++++- substrate/core/models.py | 39 ++++----- substrate/core/sb.py | 17 ++++ substrate/future_dataclass_models.py | 2 +- substrate/nodes.py | 2 +- 7 files changed, 102 insertions(+), 101 deletions(-) diff --git a/substrate/GEN_VERSION b/substrate/GEN_VERSION index 48f67c8..ad0a0a3 100644 --- a/substrate/GEN_VERSION +++ b/substrate/GEN_VERSION @@ -1 +1 @@ -20240617.20240718 \ No newline at end of file +20240617.20240719 \ No newline at end of file diff --git a/substrate/__init__.py b/substrate/__init__.py index b6de009..92b3560 100644 --- a/substrate/__init__.py +++ b/substrate/__init__.py @@ -1,99 +1,57 @@ """ 𐃏 Substrate Python SDK -20240617.20240718 +20240617.20240719 """ -from .run_python import RunPython from .nodes import ( - - Experimental, - - Box, - + CLIP, If, - - ComputeText, - - MultiComputeText, - - BatchComputeText, - - BatchComputeJSON, - + Box, + JinaV2, + EmbedText, + EmbedImage, + EraseImage, ComputeJSON, - - MultiComputeJSON, - - Mistral7BInstruct, - - Mixtral8x7BInstruct, - - Llama3Instruct8B, - - Llama3Instruct70B, - + ComputeText, + Experimental, + FetchVectors, Firellava13B, - - GenerateImage, - - MultiGenerateImage, - InpaintImage, - - MultiInpaintImage, - - StableDiffusionXLLightning, - - StableDiffusionXLInpaint, - - StableDiffusionXLControlNet, - - TranscribeSpeech, - - GenerateSpeech, - - RemoveBackground, - - EraseImage, - UpscaleImage, - - SegmentUnderPoint, - - SegmentAnything, - + DeleteVectors, + GenerateImage, SplitDocument, - - EmbedText, - + UpdateVectors, + GenerateSpeech, MultiEmbedText, - - EmbedImage, - MultiEmbedImage, - - JinaV2, - - CLIP, - - FindOrCreateVectorStore, - + SegmentAnything, + BatchComputeJSON, + BatchComputeText, ListVectorStores, - - DeleteVectorStore, - + Llama3Instruct8B, + MultiComputeJSON, + MultiComputeText, QueryVectorStore, - - FetchVectors, - - UpdateVectors, - - DeleteVectors, - ) + RemoveBackground, + TranscribeSpeech, + DeleteVectorStore, + Llama3Instruct70B, + Mistral7BInstruct, + MultiInpaintImage, + SegmentUnderPoint, + MultiGenerateImage, + Mixtral8x7BInstruct, + FindOrCreateVectorStore, + StableDiffusionXLInpaint, + StableDiffusionXLLightning, + StableDiffusionXLControlNet, +) from .core.sb import sb from ._version import __version__ from .substrate import Substrate, SubstrateResponse +from .run_python import RunPython __all__ = [ "__version__", @@ -143,4 +101,4 @@ "FetchVectors", "UpdateVectors", "DeleteVectors", - ] \ No newline at end of file +] diff --git a/substrate/core/future_directive.py b/substrate/core/future_directive.py index 5a9dc27..91c0305 100644 --- a/substrate/core/future_directive.py +++ b/substrate/core/future_directive.py @@ -14,7 +14,7 @@ ) from dataclasses import asdict, dataclass -OpType = Literal["trace", "string-concat", "jq", "jinja"] +OpType = Literal["trace", "string-concat", "jq", "jinja", "format"] class BaseDirective(ABC): @@ -63,7 +63,7 @@ class JinjaTemplate: class JinjaDirective(BaseDirective): template: JinjaTemplate variables: Dict[str, Any] - type: Literal["jq"] = "jinja" + type: Literal["jinja"] = "jinja" def to_dict(self) -> Dict: from .base_future import BaseFuture @@ -75,6 +75,29 @@ def to_dict(self) -> Dict: "variables": replaced, } +@dataclass +class FString: + future_id: Optional[str] + val: Optional[str] + + +@dataclass +class FormatDirective(BaseDirective): + f_string: FString + variables: Dict[str, Any] + type: Literal["format"] = "format" + + def to_dict(self) -> Dict: + from .base_future import BaseFuture + + replaced = BaseFuture.replace_futures_with_placeholder(self.variables) + return { + "type": self.type, + "f_string": asdict(self.f_string), + "variables": replaced, + } + + TraceType = Literal["attr", "item"] diff --git a/substrate/core/models.py b/substrate/core/models.py index 9af3706..efdd3d4 100644 --- a/substrate/core/models.py +++ b/substrate/core/models.py @@ -176,6 +176,9 @@ class Config: "Llama3Instruct8B", "Llama3Instruct70B", "Firellava13B", + "gpt-4o", + "gpt-4o-mini", + "claude-3-5-sonnet-20240620", ] = "Llama3Instruct8B" """ Selected model. `Firellava13B` is automatically selected when `image_uris` is provided. @@ -691,7 +694,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ @@ -719,7 +722,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ @@ -755,7 +758,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ height: Annotated[int, Field(ge=256, le=1536)] = 1024 """ @@ -817,7 +820,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ height: Annotated[int, Field(ge=256, le=1536)] = 1024 """ @@ -869,7 +872,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ width: Annotated[int, Field(ge=640, le=1536)] = 1024 """ @@ -925,7 +928,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ conditioning_scale: Annotated[float, Field(ge=0.0, le=1.0)] = 0.5 """ @@ -969,7 +972,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ @@ -1005,7 +1008,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ @@ -1049,7 +1052,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ strength: Annotated[float, Field(ge=0.0, le=1.0)] = 0.8 """ @@ -1121,7 +1124,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ @@ -1149,7 +1152,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ @@ -1185,7 +1188,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ @@ -1209,7 +1212,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ @@ -1241,7 +1244,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ @@ -1269,7 +1272,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ @@ -1301,7 +1304,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the image data will be returned as a base64-encoded string. + Use "hosted" to return an image URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the image data will be returned as a base64-encoded string. """ @@ -1439,7 +1442,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an audio URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the audio data will be returned as a base64-encoded string. + Use "hosted" to return an audio URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the audio data will be returned as a base64-encoded string. """ @@ -1471,7 +1474,7 @@ class Config: """ store: Optional[str] = None """ - Use "hosted" to return an audio URL hosted on Substrate. You can also provide a URL to a registered [file store](https://guides.substrate.run/guides/external-file-storage). If unset, the audio data will be returned as a base64-encoded string. + Use "hosted" to return an audio URL hosted on Substrate. You can also provide a URL to a registered [file store](https://docs.substrate.run/reference/external-files). If unset, the audio data will be returned as a base64-encoded string. """ diff --git a/substrate/core/sb.py b/substrate/core/sb.py index 58b213e..3bb03c4 100644 --- a/substrate/core/sb.py +++ b/substrate/core/sb.py @@ -1,6 +1,7 @@ """ CORE ꩜ SUBSTRATE """ +from re import template from typing import Any, Dict, Union from .client.future import Future @@ -12,6 +13,8 @@ ConcatDirective, JQDirectiveTarget, ConcatDirectiveItem, + FormatDirective, + FString, ) from .client.find_futures_client import find_futures_client @@ -80,3 +83,17 @@ def jinja(cls, template_str: Union[Future, str], variables: Dict[str, Any]) -> s for dep in find_futures_client(variables): result.FutureG.add_edge(dep, result) return result # type: ignore + + @classmethod + def format(cls, f_string: Union[Future, str], variables: Dict[str, Any]) -> str: + future_id, val = (f_string.id, None) if isinstance(f_string, Future) else (None, f_string) + directive = FormatDirective( + f_string=FString(future_id=future_id, val=val), variables=variables, + ) + result = Future(directive=directive) + if isinstance(f_string, Future): + result.FutureG.add_edge(f_string, result) + for dep in find_futures_client(variables): + result.FutureG.add_edge(dep, result) + return result # type: ignore + diff --git a/substrate/future_dataclass_models.py b/substrate/future_dataclass_models.py index f6ea96b..be4806e 100644 --- a/substrate/future_dataclass_models.py +++ b/substrate/future_dataclass_models.py @@ -1156,7 +1156,7 @@ class FutureStableDiffusionXLControlNetIn: (Future reference) Text prompt. """ - num_images: int + num_images: int = 1 """ (Future reference) Number of images to generate. diff --git a/substrate/nodes.py b/substrate/nodes.py index cc72b25..e3960ce 100644 --- a/substrate/nodes.py +++ b/substrate/nodes.py @@ -888,7 +888,7 @@ def __init__( image_uri: str, control_method: Literal["edge", "depth", "illusion", "tile"], prompt: str, - num_images: int, + num_images: int = 1, output_resolution: int = 1024, negative_prompt: Optional[str] = None, store: Optional[str] = None,