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

format string operator #42

Merged
merged 1 commit into from
Jul 25, 2024
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
2 changes: 1 addition & 1 deletion substrate/GEN_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20240617.20240724
20240617.20240724
112 changes: 35 additions & 77 deletions substrate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__",
Expand Down Expand Up @@ -143,4 +101,4 @@
"FetchVectors",
"UpdateVectors",
"DeleteVectors",
]
]
27 changes: 25 additions & 2 deletions substrate/core/future_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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"]

Expand Down
17 changes: 17 additions & 0 deletions substrate/core/sb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
CORE ꩜ SUBSTRATE
"""
from re import template
from typing import Any, Dict, Union

from .client.future import Future
Expand All @@ -12,6 +13,8 @@
ConcatDirective,
JQDirectiveTarget,
ConcatDirectiveItem,
FormatDirective,
FString,
)
from .client.find_futures_client import find_futures_client

Expand Down Expand Up @@ -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

2 changes: 1 addition & 1 deletion substrate/future_dataclass_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ class FutureStableDiffusionXLControlNetIn:
(Future reference)
Text prompt.
"""
num_images: int
num_images: int = 1
"""
(Future reference)
Number of images to generate.
Expand Down
2 changes: 1 addition & 1 deletion substrate/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading