Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit34e3aa1

Browse files
committed
parent9eed191
author Kyle Schouviller <kyle0654@hotmail.com> 1669872800 -0800committer Kyle Schouviller <kyle0654@hotmail.com> 1676240900 -0800Adding base node architectureFix type annotation errorsRuns and generates, but breaks in saving sessionFix default model value setting. Fix deprecation warning.Fixed node apiAdding markdown docsSimplifying Generate construction in apps[nodes] A few minor changes (invoke-ai#2510)* Pin api-related requirements* Remove confusing extra CORS origins list* Adds response models for HTTP 200[nodes] Adding graph_execution_state to soon replace session. Adding tests with pytest.Minor typing fixes[nodes] Fix some small output query hookups[node] Fixing some additional typing issues[nodes] Move and expand graph code. Add base item storage and sqlite implementation.Update startup to match new code[nodes] Add callbacks to item storage[nodes] Adding an InvocationContext object to use for invocations to provide easier extensibility[nodes] New execution model that handles iteration[nodes] Fixing the CLI[nodes] Adding a note to the CLI[nodes] Split processing thread into separate service[node] Add error message on node processing failureRemoving old files and duplicated packagesAdding python-multipart
1 parent49ffb64 commit34e3aa1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4510
-0
lines changed

‎.coveragerc‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[run]
2+
omit='.env/*'
3+
source='.'
4+
5+
[report]
6+
show_missing = true

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ htmlcov/
6868
.cache
6969
nosetests.xml
7070
coverage.xml
71+
cov.xml
7172
*.cover
7273
*.py,cover
7374
.hypothesis/

‎.pytest.ini‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[pytest]
2+
DJANGO_SETTINGS_MODULE = webtas.settings
3+
; python_files = tests.py test_*.py *_tests.py
4+
5+
addopts = --cov=. --cov-config=.coveragerc --cov-report xml:cov.xml

‎docs/contributing/ARCHITECTURE.md‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#Invoke.AI Architecture
2+
3+
```mermaid
4+
flowchart TB
5+
6+
subgraph apps[Applications]
7+
webui[WebUI]
8+
cli[CLI]
9+
10+
subgraph webapi[Web API]
11+
api[HTTP API]
12+
sio[Socket.IO]
13+
end
14+
15+
end
16+
17+
subgraph invoke[Invoke]
18+
direction LR
19+
invoker
20+
services
21+
sessions
22+
invocations
23+
end
24+
25+
subgraph core[AI Core]
26+
Generate
27+
end
28+
29+
webui --> webapi
30+
webapi --> invoke
31+
cli --> invoke
32+
33+
invoker --> services & sessions
34+
invocations --> services
35+
sessions --> invocations
36+
37+
services --> core
38+
39+
%% Styles
40+
classDef sg fill:#5028C8,font-weight:bold,stroke-width:2,color:#fff,stroke:#14141A
41+
classDef default stroke-width:2px,stroke:#F6B314,color:#fff,fill:#14141A
42+
43+
class apps,webapi,invoke,core sg
44+
45+
```
46+
47+
##Applications
48+
49+
Applications are built on top of the invoke framework. They should construct`invoker` and then interact through it. They should avoid interacting directly with core code in order to support a variety of configurations.
50+
51+
###Web UI
52+
53+
The Web UI is built on top of an HTTP API built with[FastAPI](https://fastapi.tiangolo.com/) and[Socket.IO](https://socket.io/). The frontend code is found in`/frontend` and the backend code is found in`/ldm/invoke/app/api_app.py` and`/ldm/invoke/app/api/`. The code is further organized as such:
54+
55+
| Component| Description|
56+
| ---| ---|
57+
| api_app.py| Sets up the API app, annotates the OpenAPI spec with additional data, and runs the API|
58+
| dependencies| Creates all invoker services and the invoker, and provides them to the API|
59+
| events| An eventing system that could in the future be adapted to support horizontal scale-out|
60+
| sockets| The Socket.IO interface - handles listening to and emitting session events (events are defined in the events service module)|
61+
| routers| API definitions for different areas of API functionality|
62+
63+
###CLI
64+
65+
The CLI is built automatically from invocation metadata, and also supports invocation piping and auto-linking. Code is available in`/ldm/invoke/app/cli_app.py`.
66+
67+
##Invoke
68+
69+
The Invoke framework provides the interface to the underlying AI systems and is built with flexibility and extensibility in mind. There are four major concepts: invoker, sessions, invocations, and services.
70+
71+
###Invoker
72+
73+
The invoker (`/ldm/invoke/app/services/invoker.py`) is the primary interface through which applications interact with the framework. Its primary purpose is to create, manage, and invoke sessions. It also maintains two sets of services:
74+
-**invocation services**, which are used by invocations to interact with core functionality.
75+
-**invoker services**, which are used by the invoker to manage sessions and manage the invocation queue.
76+
77+
###Sessions
78+
79+
Invocations and links between them form a graph, which is maintained in a session. Sessions can be queued for invocation, which will execute their graph (either the next ready invocation, or all invocations). Sessions also maintain execution history for the graph (including storage of any outputs). An invocation may be added to a session at any time, and there is capability to add and entire graph at once, as well as to automatically link new invocations to previous invocations. Invocations can not be deleted or modified once added.
80+
81+
The session graph does not support looping. This is left as an application problem to prevent additional complexity in the graph.
82+
83+
###Invocations
84+
85+
Invocations represent individual units of execution, with inputs and outputs. All invocations are located in`/ldm/invoke/app/invocations`, and are all automatically discovered and made available in the applications. These are the primary way to expose new functionality in Invoke.AI, and the[implementation guide](INVOCATIONS.md) explains how to add new invocations.
86+
87+
###Services
88+
89+
Services provide invocations access AI Core functionality and other necessary functionality (e.g. image storage). These are available in`/ldm/invoke/app/services`. As a general rule, new services should provide an interface as an abstract base class, and may provide a lightweight local implementation by default in their module. The goal for all services should be to enable the usage of different implementations (e.g. using cloud storage for image storage), but should not load any module dependencies unless that implementation has been used (i.e. don't import anything that won't be used, especially if it's expensive to import).
90+
91+
##AI Core
92+
93+
The AI Core is represented by the rest of the code base (i.e. the code outside of`/ldm/invoke/app/`).

‎docs/contributing/INVOCATIONS.md‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#Invocations
2+
3+
Invocations represent a single operation, its inputs, and its outputs. These operations and their outputs can be chained together to generate and modify images.
4+
5+
##Creating a new invocation
6+
7+
To create a new invocation, either find the appropriate module file in`/ldm/invoke/app/invocations` to add your invocation to, or create a new one in that folder. All invocations in that folder will be discovered and made available to the CLI and API automatically. Invocations make use of[typing](https://docs.python.org/3/library/typing.html) and[pydantic](https://pydantic-docs.helpmanual.io/) for validation and integration into the CLI and API.
8+
9+
An invocation looks like this:
10+
11+
```py
12+
classUpscaleInvocation(BaseInvocation):
13+
"""Upscales an image."""
14+
type: Literal['upscale']='upscale'
15+
16+
# Inputs
17+
image: Union[ImageField,None]= Field(description="The input image")
18+
strength:float= Field(default=0.75,gt=0,le=1,description="The strength")
19+
level: Literal[2,4]= Field(default=2,description="The upscale level")
20+
21+
definvoke(self,context: InvocationContext) -> ImageOutput:
22+
image= context.services.images.get(self.image.image_type,self.image.image_name)
23+
results= context.services.generate.upscale_and_reconstruct(
24+
image_list= [[image,0]],
25+
upscale= (self.level,self.strength),
26+
strength=0.0,# GFPGAN strength
27+
save_original=False,
28+
image_callback=None,
29+
)
30+
31+
# Results are image and seed, unwrap for now
32+
#TODO: can this return multiple results?
33+
image_type= ImageType.RESULT
34+
image_name= context.services.images.create_name(context.graph_execution_state_id,self.id)
35+
context.services.images.save(image_type, image_name, results[0][0])
36+
return ImageOutput(
37+
image= ImageField(image_type= image_type,image_name= image_name)
38+
)
39+
```
40+
41+
Each portion is important to implement correctly.
42+
43+
###Class definition and type
44+
```py
45+
classUpscaleInvocation(BaseInvocation):
46+
"""Upscales an image."""
47+
type: Literal['upscale']='upscale'
48+
```
49+
All invocations must derive from`BaseInvocation`. They should have a docstring that declares what they do in a single, short line. They should also have a`type` with a type hint that's`Literal["command_name"]`, where`command_name` is what the user will type on the CLI or use in the API to create this invocation. The`command_name` must be unique. The`type` must be assigned to the value of the literal in the type hint.
50+
51+
###Inputs
52+
```py
53+
# Inputs
54+
image: Union[ImageField,None]= Field(description="The input image")
55+
strength:float= Field(default=0.75,gt=0,le=1,description="The strength")
56+
level: Literal[2,4]= Field(default=2,description="The upscale level")
57+
```
58+
Inputs consist of three parts: a name, a type hint, and a`Field` with default, description, and validation information. For example:
59+
| Part| Value| Description|
60+
| ----| -----| -----------|
61+
| Name|`strength`| This field is referred to as`strength`|
62+
| Type Hint|`float`| This field must be of type`float`|
63+
| Field|`Field(default=0.75, gt=0, le=1, description="The strength")`| The default value is`0.75`, the value must be in the range (0,1], and help text will show "The strength" for this field.|
64+
65+
Notice that`image` has type`Union[ImageField,None]`. The`Union` allows this field to be parsed with`None` as a value, which enables linking to previous invocations. All fields should either provide a default value or allow`None` as a value, so that they can be overwritten with a linked output from another invocation.
66+
67+
The special type`ImageField` is also used here. All images are passed as`ImageField`, which protects them from pydantic validation errors (since images only ever come from links).
68+
69+
Finally, note that for all linking, the`type` of the linked fields must match. If the`name` also matches, then the field can be**automatically linked** to a previous invocation by name and matching.
70+
71+
###Invoke Function
72+
```py
73+
definvoke(self,context: InvocationContext) -> ImageOutput:
74+
image= context.services.images.get(self.image.image_type,self.image.image_name)
75+
results= context.services.generate.upscale_and_reconstruct(
76+
image_list= [[image,0]],
77+
upscale= (self.level,self.strength),
78+
strength=0.0,# GFPGAN strength
79+
save_original=False,
80+
image_callback=None,
81+
)
82+
83+
# Results are image and seed, unwrap for now
84+
image_type= ImageType.RESULT
85+
image_name= context.services.images.create_name(context.graph_execution_state_id,self.id)
86+
context.services.images.save(image_type, image_name, results[0][0])
87+
return ImageOutput(
88+
image= ImageField(image_type= image_type,image_name= image_name)
89+
)
90+
```
91+
The`invoke` function is the last portion of an invocation. It is provided an`InvocationContext` which contains services to perform work as well as a`session_id` for use as needed. It should return a class with output values that derives from`BaseInvocationOutput`.
92+
93+
Before being called, the invocation will have all of its fields set from defaults, inputs, and finally links (overriding in that order).
94+
95+
Assume that this invocation may be running simultaneously with other invocations, may be running on another machine, or in other interesting scenarios. If you need functionality, please provide it as a service in the`InvocationServices` class, and make sure it can be overridden.
96+
97+
###Outputs
98+
```py
99+
classImageOutput(BaseInvocationOutput):
100+
"""Base class for invocations that output an image"""
101+
type: Literal['image']='image'
102+
103+
image: ImageField= Field(default=None,description="The output image")
104+
```
105+
Output classes look like an invocation class without the invoke method. Prefer to use an existing output class if available, and prefer to name inputs the same as outputs when possible, to promote automatic invocation linking.

‎ldm/generate.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,8 @@ def upscale_and_reconstruct(
10301030
image_callback=None,
10311031
prefix=None,
10321032
):
1033+
1034+
results= []
10331035
forrinimage_list:
10341036
image,seed=r
10351037
try:
@@ -1083,6 +1085,10 @@ def upscale_and_reconstruct(
10831085
else:
10841086
r[0]=image
10851087

1088+
results.append([image,seed])
1089+
1090+
returnresults
1091+
10861092
defapply_textmask(
10871093
self,image_path:str,prompt:str,callback,threshold:float=0.5
10881094
):

‎ldm/invoke/app/api/dependencies.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654)
2+
3+
fromargparseimportNamespace
4+
importos
5+
6+
from ..services.processorimportDefaultInvocationProcessor
7+
8+
from ..services.graphimportGraphExecutionState
9+
from ..services.sqliteimportSqliteItemStorage
10+
11+
from ...globalsimportGlobals
12+
13+
from ..services.image_storageimportDiskImageStorage
14+
from ..services.invocation_queueimportMemoryInvocationQueue
15+
from ..services.invocation_servicesimportInvocationServices
16+
from ..services.invokerimportInvoker,InvokerServices
17+
from ..services.generate_initializerimportget_generate
18+
from .eventsimportFastAPIEventService
19+
20+
21+
# TODO: is there a better way to achieve this?
22+
defcheck_internet()->bool:
23+
'''
24+
Return true if the internet is reachable.
25+
It does this by pinging huggingface.co.
26+
'''
27+
importurllib.request
28+
host='http://huggingface.co'
29+
try:
30+
urllib.request.urlopen(host,timeout=1)
31+
returnTrue
32+
except:
33+
returnFalse
34+
35+
36+
classApiDependencies:
37+
"""Contains and initializes all dependencies for the API"""
38+
invoker:Invoker=None
39+
40+
@staticmethod
41+
definitialize(
42+
args,
43+
config,
44+
event_handler_id:int
45+
):
46+
Globals.try_patchmatch=args.patchmatch
47+
Globals.always_use_cpu=args.always_use_cpu
48+
Globals.internet_available=args.internet_availableandcheck_internet()
49+
Globals.disable_xformers=notargs.xformers
50+
Globals.ckpt_convert=args.ckpt_convert
51+
52+
# TODO: Use a logger
53+
print(f'>> Internet connectivity is{Globals.internet_available}')
54+
55+
generate=get_generate(args,config)
56+
57+
events=FastAPIEventService(event_handler_id)
58+
59+
output_folder=os.path.abspath(os.path.join(os.path.dirname(__file__),'../../../../outputs'))
60+
61+
images=DiskImageStorage(output_folder)
62+
63+
services=InvocationServices(
64+
generate=generate,
65+
events=events,
66+
images=images
67+
)
68+
69+
# TODO: build a file/path manager?
70+
db_location=os.path.join(output_folder,'invokeai.db')
71+
72+
invoker_services=InvokerServices(
73+
queue=MemoryInvocationQueue(),
74+
graph_execution_manager=SqliteItemStorage[GraphExecutionState](filename=db_location,table_name='graph_executions'),
75+
processor=DefaultInvocationProcessor()
76+
)
77+
78+
ApiDependencies.invoker=Invoker(services,invoker_services)
79+
80+
@staticmethod
81+
defshutdown():
82+
ifApiDependencies.invoker:
83+
ApiDependencies.invoker.stop()

‎ldm/invoke/app/api/events.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654)
2+
3+
importasyncio
4+
fromqueueimportEmpty,Queue
5+
fromtypingimportAny
6+
fromfastapi_events.dispatcherimportdispatch
7+
from ..services.eventsimportEventServiceBase
8+
importthreading
9+
10+
classFastAPIEventService(EventServiceBase):
11+
event_handler_id:int
12+
__queue:Queue
13+
__stop_event:threading.Event
14+
15+
def__init__(self,event_handler_id:int)->None:
16+
self.event_handler_id=event_handler_id
17+
self.__queue=Queue()
18+
self.__stop_event=threading.Event()
19+
asyncio.create_task(self.__dispatch_from_queue(stop_event=self.__stop_event))
20+
21+
super().__init__()
22+
23+
24+
defstop(self,*args,**kwargs):
25+
self.__stop_event.set()
26+
self.__queue.put(None)
27+
28+
29+
defdispatch(self,event_name:str,payload:Any)->None:
30+
self.__queue.put(dict(
31+
event_name=event_name,
32+
payload=payload
33+
))
34+
35+
36+
asyncdef__dispatch_from_queue(self,stop_event:threading.Event):
37+
"""Get events on from the queue and dispatch them, from the correct thread"""
38+
whilenotstop_event.is_set():
39+
try:
40+
event=self.__queue.get(block=False)
41+
ifnotevent:# Probably stopping
42+
continue
43+
44+
dispatch(
45+
event.get('event_name'),
46+
payload=event.get('payload'),
47+
middleware_id=self.event_handler_id)
48+
49+
exceptEmpty:
50+
awaitasyncio.sleep(0.001)
51+
pass
52+
53+
exceptasyncio.CancelledErrorase:
54+
raisee# Raise a proper error

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp