From 224a4d3ce03e0b33fc5cdb3c1ac996acd27cd848 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 | 112 +++++++++------------------ substrate/core/future_directive.py | 27 ++++++- substrate/core/sb.py | 17 ++++ substrate/future_dataclass_models.py | 2 +- substrate/nodes.py | 2 +- 6 files changed, 80 insertions(+), 82 deletions(-) diff --git a/substrate/GEN_VERSION b/substrate/GEN_VERSION index e3fbb5a..5e76ea0 100644 --- a/substrate/GEN_VERSION +++ b/substrate/GEN_VERSION @@ -1 +1 @@ -20240617.20240724 \ No newline at end of file +20240617.20240724 diff --git a/substrate/__init__.py b/substrate/__init__.py index 3fd46f5..d71bd5f 100644 --- a/substrate/__init__.py +++ b/substrate/__init__.py @@ -4,96 +4,54 @@ 20240617.20240724 """ -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/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 7aefdf2..3212e51 100644 --- a/substrate/future_dataclass_models.py +++ b/substrate/future_dataclass_models.py @@ -1159,7 +1159,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 1ddcc54..37ee4f2 100644 --- a/substrate/nodes.py +++ b/substrate/nodes.py @@ -891,7 +891,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,