Skip to content

Commit bdc9083

Browse files
committed
Add a barebones interrogate API
1 parent 737eb28 commit bdc9083

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

launch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def prepare_enviroment():
198198
def start_webui():
199199
print(f"Launching Web UI with arguments: {' '.join(sys.argv[1:])}")
200200
import webui
201-
webui.webui()
201+
webui.webui_or_api()
202202

203203

204204
if __name__ == "__main__":

modules/api/api.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from modules.api.models import StableDiffusionTxt2ImgProcessingAPI, StableDiffusionImg2ImgProcessingAPI
1+
from modules.api.models import StableDiffusionTxt2ImgProcessingAPI, StableDiffusionImg2ImgProcessingAPI, InterrogateAPI
22
from modules.processing import StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img, process_images
33
from modules.sd_samplers import all_samplers
44
from modules.extras import run_pnginfo
@@ -25,6 +25,11 @@ class ImageToImageResponse(BaseModel):
2525
parameters: Json
2626
info: Json
2727

28+
class InterrogateResponse(BaseModel):
29+
caption: str = Field(default=None, title="Caption", description="The generated caption for the image.")
30+
parameters: Json
31+
info: Json
32+
2833

2934
class Api:
3035
def __init__(self, app, queue_lock):
@@ -33,6 +38,7 @@ def __init__(self, app, queue_lock):
3338
self.queue_lock = queue_lock
3439
self.app.add_api_route("/sdapi/v1/txt2img", self.text2imgapi, methods=["POST"])
3540
self.app.add_api_route("/sdapi/v1/img2img", self.img2imgapi, methods=["POST"])
41+
self.app.add_api_route("/sdapi/v1/interrogate", self.interrogateapi, methods=["POST"])
3642

3743
def __base64_to_image(self, base64_string):
3844
# if has a comma, deal with prefix
@@ -118,6 +124,23 @@ def img2imgapi(self, img2imgreq: StableDiffusionImg2ImgProcessingAPI):
118124

119125
return ImageToImageResponse(images=b64images, parameters=json.dumps(vars(img2imgreq)), info=processed.js())
120126

127+
def interrogateapi(self, interrogatereq: InterrogateAPI):
128+
image_b64 = interrogatereq.image
129+
if image_b64 is None:
130+
raise HTTPException(status_code=404, detail="Image not found")
131+
132+
populate = interrogatereq.copy(update={ # Override __init__ params
133+
}
134+
)
135+
136+
img = self.__base64_to_image(image_b64)
137+
138+
# Override object param
139+
with self.queue_lock:
140+
processed = shared.interrogator.interrogate(img)
141+
142+
return InterrogateResponse(caption=processed, parameters=json.dumps(vars(interrogatereq)), info=None)
143+
121144
def extrasapi(self):
122145
raise NotImplementedError
123146

modules/api/models.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ def merge_class_params(class_):
6363

6464

6565
self._model_name = model_name
66-
self._class_data = merge_class_params(class_instance)
66+
67+
if class_instance is not None:
68+
self._class_data = merge_class_params(class_instance)
69+
else:
70+
self._class_data = {}
71+
6772
self._model_def = [
6873
ModelDef(
6974
field=underscore(k),
@@ -105,4 +110,10 @@ def generate_model(self):
105110
"StableDiffusionProcessingImg2Img",
106111
StableDiffusionProcessingImg2Img,
107112
[{"key": "sampler_index", "type": str, "default": "Euler"}, {"key": "init_images", "type": list, "default": None}, {"key": "denoising_strength", "type": float, "default": 0.75}, {"key": "mask", "type": str, "default": None}, {"key": "include_init_images", "type": bool, "default": False, "exclude" : True}]
113+
).generate_model()
114+
115+
InterrogateAPI = PydanticModelGenerator(
116+
"Interrogate",
117+
None,
118+
[{"key": "image", "type": str, "default": None}]
108119
).generate_model()

webui.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ def webui():
146146
app.add_middleware(GZipMiddleware, minimum_size=1000)
147147

148148
if (launch_api):
149-
create_api(app)
149+
print('launching API')
150+
api = create_api(app)
151+
api.launch(server_name="0.0.0.0" if cmd_opts.listen else "127.0.0.1", port=cmd_opts.port if cmd_opts.port else 7861)
150152

151153
wait_on_server(demo)
152154

@@ -161,10 +163,12 @@ def webui():
161163
print('Restarting Gradio')
162164

163165

164-
165-
task = []
166-
if __name__ == "__main__":
166+
def webui_or_api():
167167
if cmd_opts.nowebui:
168168
api_only()
169169
else:
170170
webui()
171+
172+
task = []
173+
if __name__ == "__main__":
174+
webui_or_api()

0 commit comments

Comments
 (0)