Skip to content

Commit 70ee57e

Browse files
authored
Merge pull request #42 from SubstrateLabs/chris/format-str
format string operator
2 parents 93639c0 + 224a4d3 commit 70ee57e

File tree

6 files changed

+80
-82
lines changed

6 files changed

+80
-82
lines changed

substrate/GEN_VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20240617.20240724
1+
20240617.20240724

substrate/__init__.py

+35-77
Original file line numberDiff line numberDiff line change
@@ -4,96 +4,54 @@
44
20240617.20240724
55
"""
66

7-
from .run_python import RunPython
87
from .nodes import (
9-
10-
Experimental,
11-
12-
Box,
13-
8+
CLIP,
149
If,
15-
16-
ComputeText,
17-
18-
MultiComputeText,
19-
20-
BatchComputeText,
21-
22-
BatchComputeJSON,
23-
10+
Box,
11+
JinaV2,
12+
EmbedText,
13+
EmbedImage,
14+
EraseImage,
2415
ComputeJSON,
25-
26-
MultiComputeJSON,
27-
28-
Mistral7BInstruct,
29-
30-
Mixtral8x7BInstruct,
31-
32-
Llama3Instruct8B,
33-
34-
Llama3Instruct70B,
35-
16+
ComputeText,
17+
Experimental,
18+
FetchVectors,
3619
Firellava13B,
37-
38-
GenerateImage,
39-
40-
MultiGenerateImage,
41-
4220
InpaintImage,
43-
44-
MultiInpaintImage,
45-
46-
StableDiffusionXLLightning,
47-
48-
StableDiffusionXLInpaint,
49-
50-
StableDiffusionXLControlNet,
51-
52-
TranscribeSpeech,
53-
54-
GenerateSpeech,
55-
56-
RemoveBackground,
57-
58-
EraseImage,
59-
6021
UpscaleImage,
61-
62-
SegmentUnderPoint,
63-
64-
SegmentAnything,
65-
22+
DeleteVectors,
23+
GenerateImage,
6624
SplitDocument,
67-
68-
EmbedText,
69-
25+
UpdateVectors,
26+
GenerateSpeech,
7027
MultiEmbedText,
71-
72-
EmbedImage,
73-
7428
MultiEmbedImage,
75-
76-
JinaV2,
77-
78-
CLIP,
79-
80-
FindOrCreateVectorStore,
81-
29+
SegmentAnything,
30+
BatchComputeJSON,
31+
BatchComputeText,
8232
ListVectorStores,
83-
84-
DeleteVectorStore,
85-
33+
Llama3Instruct8B,
34+
MultiComputeJSON,
35+
MultiComputeText,
8636
QueryVectorStore,
87-
88-
FetchVectors,
89-
90-
UpdateVectors,
91-
92-
DeleteVectors,
93-
)
37+
RemoveBackground,
38+
TranscribeSpeech,
39+
DeleteVectorStore,
40+
Llama3Instruct70B,
41+
Mistral7BInstruct,
42+
MultiInpaintImage,
43+
SegmentUnderPoint,
44+
MultiGenerateImage,
45+
Mixtral8x7BInstruct,
46+
FindOrCreateVectorStore,
47+
StableDiffusionXLInpaint,
48+
StableDiffusionXLLightning,
49+
StableDiffusionXLControlNet,
50+
)
9451
from .core.sb import sb
9552
from ._version import __version__
9653
from .substrate import Substrate, SubstrateResponse
54+
from .run_python import RunPython
9755

9856
__all__ = [
9957
"__version__",
@@ -143,4 +101,4 @@
143101
"FetchVectors",
144102
"UpdateVectors",
145103
"DeleteVectors",
146-
]
104+
]

substrate/core/future_directive.py

+25-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
)
1515
from dataclasses import asdict, dataclass
1616

17-
OpType = Literal["trace", "string-concat", "jq", "jinja"]
17+
OpType = Literal["trace", "string-concat", "jq", "jinja", "format"]
1818

1919

2020
class BaseDirective(ABC):
@@ -63,7 +63,7 @@ class JinjaTemplate:
6363
class JinjaDirective(BaseDirective):
6464
template: JinjaTemplate
6565
variables: Dict[str, Any]
66-
type: Literal["jq"] = "jinja"
66+
type: Literal["jinja"] = "jinja"
6767

6868
def to_dict(self) -> Dict:
6969
from .base_future import BaseFuture
@@ -75,6 +75,29 @@ def to_dict(self) -> Dict:
7575
"variables": replaced,
7676
}
7777

78+
@dataclass
79+
class FString:
80+
future_id: Optional[str]
81+
val: Optional[str]
82+
83+
84+
@dataclass
85+
class FormatDirective(BaseDirective):
86+
f_string: FString
87+
variables: Dict[str, Any]
88+
type: Literal["format"] = "format"
89+
90+
def to_dict(self) -> Dict:
91+
from .base_future import BaseFuture
92+
93+
replaced = BaseFuture.replace_futures_with_placeholder(self.variables)
94+
return {
95+
"type": self.type,
96+
"f_string": asdict(self.f_string),
97+
"variables": replaced,
98+
}
99+
100+
78101

79102
TraceType = Literal["attr", "item"]
80103

substrate/core/sb.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
CORE ꩜ SUBSTRATE
33
"""
4+
from re import template
45
from typing import Any, Dict, Union
56

67
from .client.future import Future
@@ -12,6 +13,8 @@
1213
ConcatDirective,
1314
JQDirectiveTarget,
1415
ConcatDirectiveItem,
16+
FormatDirective,
17+
FString,
1518
)
1619
from .client.find_futures_client import find_futures_client
1720

@@ -80,3 +83,17 @@ def jinja(cls, template_str: Union[Future, str], variables: Dict[str, Any]) -> s
8083
for dep in find_futures_client(variables):
8184
result.FutureG.add_edge(dep, result)
8285
return result # type: ignore
86+
87+
@classmethod
88+
def format(cls, f_string: Union[Future, str], variables: Dict[str, Any]) -> str:
89+
future_id, val = (f_string.id, None) if isinstance(f_string, Future) else (None, f_string)
90+
directive = FormatDirective(
91+
f_string=FString(future_id=future_id, val=val), variables=variables,
92+
)
93+
result = Future(directive=directive)
94+
if isinstance(f_string, Future):
95+
result.FutureG.add_edge(f_string, result)
96+
for dep in find_futures_client(variables):
97+
result.FutureG.add_edge(dep, result)
98+
return result # type: ignore
99+

substrate/future_dataclass_models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ class FutureStableDiffusionXLControlNetIn:
11591159
(Future reference)
11601160
Text prompt.
11611161
"""
1162-
num_images: int
1162+
num_images: int = 1
11631163
"""
11641164
(Future reference)
11651165
Number of images to generate.

substrate/nodes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ def __init__(
891891
image_uri: str,
892892
control_method: Literal["edge", "depth", "illusion", "tile"],
893893
prompt: str,
894-
num_images: int,
894+
num_images: int = 1,
895895
output_resolution: int = 1024,
896896
negative_prompt: Optional[str] = None,
897897
store: Optional[str] = None,

0 commit comments

Comments
 (0)