ArtVentureX / sd-webui-agent-scheduler

634 stars 67 forks source link

How to effectively cancel or prohibit the automatic saving of pictures generated through the API? #89

Closed Bruce337f closed 1 year ago

Bruce337f commented 1 year ago

When I modify the program in the api, and comment out some codes that seem to need to be canceled, but through the api call, the picture will still be generated to the corresponding directory folder?

this is code block location: https://github.com/AUTOMATIC1111/stable-diffusion-webui/blob/3715ece0adce7bf7c5e9c5ab3710b2fdc3848f39/modules/api/api.py#L181-L186

populate = txt2imgreq.copy(update={ # Override __init__ params
            "sampler_name": validate_sampler_name(txt2imgreq.sampler_name or txt2imgreq.sampler_index),
            "do_not_save_samples": False,   
            "do_not_save_grid": False,
            "outpath_samples":no opts.outdir_txt2img_samples,   #you can set another folder if you want, I didn't test it though 
            "outpath_grids": no opts.outdir_txt2img_grids,
            }
        )

I tried this code,but it's useless for me!

Bruce337f commented 1 year ago

I'm very sorry, I don't quite understand the function of the callback function, do I need to pass parameters to query the task status, why not directly return the progress or status of the image generation? Is the result obtained by task_completed query task more accurate?

from fastapi import FastAPI, UploadFile, File, Form

@app.post("/task_completed")
async def handle_task_completed(
    task_id: Annotated[str, Form()],
    status: Annotated[str, Form()],
    files: Optional[List[UploadFile]] = File(None),
):
    print(f"Received {len(files)} files for task {task_id} with status {status}")
    for file in files:
        print(f"* {file.filename} {file.content_type} {file.size}")
        # ... do something with the file contents ...

# Received 1 files for task 3cf8b150-f260-4489-b6e8-d86ed8a564ca with status done
# * 00008-3322209480.png image/png 416400

To sum up, I want to return to the same visual picture state as midjourney. When I need to modify the code, what should I do? It;s very good project,expectly your reply!

artventuredev commented 1 year ago

When I modify the program in the api, and comment out some codes that seem to need to be canceled, but through the api call, the picture will still be generated to the corresponding directory folder?

Hmm, I'm not really get your idea. Are you mentioning the original SD APIs (/sdapi/txt2img) or the queue APIs from the extension (/agent-scheduler/v1/queue/txt2img)?

I'm very sorry, I don't quite understand the function of the callback function, do I need to pass parameters to query the task status, why not directly return the progress or status of the image generation?

This is the example code you should implement on your end end handle callbacks from the extension. Explicitly, the body contains 3 fields:

Bruce337f commented 1 year ago

Thank you so much for your prompt reply! !

Hmm, I'm not really get your idea. Are you mentioning the original SD APIs () or the queue APIs from the extension ()?/sdapi/txt2img/agent-scheduler/v1/queue/txt2img It should be the api in the original sd,/sdapi/txt2img because I have modified your code, but it has no effect.

his is the example code you should implement on your end end handle callbacks from the extension. Explicitly, the body contains 3 fields: I don't quite understand why parameters are passed, especially status, shouldn't this be the result returned by the extension itself? If you still need to pass parameters, it will become a custom image state?

artventuredev commented 1 year ago

It should be the api in the original sd,/sdapi/txt2img because I have modified your code, but it has no effect.

If you're calling the /agent-scheduler/v1/queue/txt2img API, then the generated images will always be saved. If you're calling the /sdapi/txt2img API, you can pass the param "save_images": false to the request. Eg:

POST /sdapi/txt2img
{
  "prompt": "1girl",
  ...
  "save_images": false
}

I don't quite understand why parameters are passed, especially status, shouldn't this be the result returned by the extension itself? If you still need to pass parameters, it will become a custom image state?

The code provided is an example for implementing the callback API handler on YOUR SERVER. If you're not using the callback feature, you can simply ignore it. The purpose of the callback API is to be called by the extension when the task is completed. The status parameter is included in the body to indicate whether the generation was successful or not. Please note that the handler should be mapped to an API endpoint and not called as a function directly.

Bruce337f commented 1 year ago

Thanks for your reply, I will try again! But I got this error after update, what should I do?

[ArtVenture] Connection error while uploading result
[ArtVenture] Retrying 1...

This is a great project, thanks for your hard work!

Bruce337f commented 1 year ago

Is there any update here please?

tungnguyensipher commented 1 year ago

Is there any update here please?

@Bruce337f This's mostly because your callback is not accessible from your SD node. What's your callback_url setting?

Bruce337f commented 1 year ago

https://github.com/ArtVentureX/sd-webui-agent-scheduler/issues/89#issuecomment-1652957267 Thank you for your reply! ! Is it because the callback callback_url causes such an error? The callback function I set is: http://localhost:port/task_completed, but I think it should have nothing to do with the error.

[ArtVenture] Connection error while uploading result
[ArtVenture] Retrying 1...
artventuredev commented 1 year ago

You should just ignore the callback_url, don't set it in your request body. The callback is to notify a remote server when a task is completed.

Bruce337f commented 1 year ago

Thank you for your reply, I didn't expect that the callback function caused such an error.

That's a good suggestion, I'm going to try it!

Bruce337f commented 1 year ago

Is it effective to set the callback address as a normal php link instead of http://localhost:port/task_completed?

If it works, how should I set the corresponding function, similar to:

from fastapi import FastAPI, UploadFile, File, Form

@app.post("/task_completed")
async def handle_task_completed(
     task_id: Annotated[str, Form()],
     status: Annotated[str, Form()],
     files: Optional[List[UploadFile]] = File(None),
):
     print(f"Received {len(files)} files for task {task_id} with status {status}")
     for file in files:
         print(f"* {file. filename} {file. content_type} {file. size}")
         # ... do something with the file contents ...

# Received 1 files for task 3cf8b150-f260-4489-b6e8-d86ed8a564ca with status done
# * 00008-3322209480.png image/png 416400

What should I do, looking forward to your reply! !