mrhan1993 / Fooocus-API

FastAPI powered API for Fooocus
GNU General Public License v3.0
562 stars 150 forks source link

Aspect ratio setting ignored #57

Closed davefojtik closed 10 months ago

davefojtik commented 10 months ago

Hello. First of all, I want to thank you for making this much needed API.

I'm implementing it as a Discord bot in docker container on RunPod.ai and everything works fine so far. But I ran into a problem as my aspect ratio settings are being ignored and the default value (1152x896) is always used instead.

Expected Behavior

When specifying different resolution in "aspect_ratios_selection" parameter, the image is generated with the chosen resolution.

Current Behavior

The "aspect_ratios_selection" is being ignored.

Steps to Reproduce

Things I tried:

konieshadow commented 10 months ago

@davefojtik Thanks for your feedback.

I think it's because the api only accept the value contains unicode ×, not ASCII code x or *. Off cause, it can be more smater. I will let it accept x and *.

konieshadow commented 10 months ago

@davefojtik Now you can try with v0.3.15. "1024x1024" or "1024*1024" will all be ok.

davefojtik commented 10 months ago

Omg now I feel like the guy who was pulling his hair wondering why his code doesn't work when his friends changed his semicolons to an identical looking greek question marks xD I would never think of this thanks. Also for the quick update and responses you do amazing job!

john-mnz commented 9 months ago

Hello. First of all, I want to thank you for making this much needed API.

I'm implementing it as a Discord bot in docker container on RunPod.ai and everything works fine so far. But I ran into a problem as my aspect ratio settings are being ignored and the default value (1152x896) is always used instead.

Expected Behavior

When specifying different resolution in "aspect_ratios_selection" parameter, the image is generated with the chosen resolution.

Current Behavior

The "aspect_ratios_selection" is being ignored.

Steps to Reproduce

Things I tried:

  • Using both older and latest docker images
  • I checked the documentation and found no answer. The behaviour is present even when using the example request in the FastAPI docs directly in the browser.
  • I checked to make sure that this issue has not already been filed
  • I tried different values like resolutions (e.g. 1024x1024, 1600*640, ...) and aspect ratios (e.g. 1:1, 1.0, 5:2, 2.5, ...)
  • And of course I tried making the request different ways (Web Docs, PostMan, RunPod UI, ...) to confirm that it's not my code doing it.

@davefojtik I'm currently trying to implement this as a runpod serverless endpoint but couldn't really figure out how since you mentioned you did this I was hoping you could give me general guidence on the steps

davefojtik commented 9 months ago

@john-mnz Sure. I was heavily inspired by Generative Labs' tutorial and code on RunPod Serverless Auto1111. The steps are the following:

  1. Create RunPod network volume. 15GB is just enough for the generic Foocus with Juggernaut model. You can increase its size any time if you need additional models, loras etc. But unfortunately, it cannot be reduced back.
  2. Create a custom Pod Template and use the konieshadow/fooocus-api:latest image. I went with 30GB disk sizes, mount path /workspace, and expose http 8888 and tcp 22.
  3. Run the network volume with the custom fooocus-api image you've just created. You don't need a strong GPU pod, the installation is CPU and download-intensive, but be aware that some older-gen pods might not support the latest CUDA versions. Let it download and install everything. After the Juggernaut model is downloaded, use the connect button to load into the Fooocus-API docs running on the pod's 8888 port. Here you should try all the API methods you plan to use because additional models are downloaded once you run inpaint, outpaint, upscale, vary and image inputs (canny, face swap etc.) endpoints for the first time.
  4. After that you are ready to connect to the pod's console and use cp -r /app/* /workspace/ to copy everything into the persistent network volume
  5. Once everything is copied successfully, you can terminate the pod. You have the network volume ready. ‎
  6. Now you need to create your custom docker image that will run on the actual serverless API. I've published my files into this repo which you can use to build your own. Just be aware that some things might need to be adjusted in handler.py based on how you want to make requests and it's parameters.
  7. Once you build it, upload it to the Docker Hub.
  8. Now you create a custom Serverless Pod Template using the Docker Hub image you've just uploaded. 17GB container disk is enough for all dependencies in my case.
  9. Create a new Serverless API Endpoint. Make sure to choose your Docker Hub image and not the konieshadow/fooocus-api:latest from step 2. In Advanced settings choose your created network volume.
  10. Other settings are your choice, but I personally found that using 4090/L4 GPUs + Flashboot is the most cost-effective one. In frequent use, the 4090 is able to return an image in ~13s including cold start, making it 3-4x cheaper to run this on RunPod than for example using DALLE-3 API. This fact can of course vary based on datacenter locations and GPU availability.

If you have any questions, don't hesitate to ask here or in the newly created repo. Good luck!

john-mnz commented 9 months ago

@davefojtik thank you sooo much man I can't even express how helpful it was and how grateful I am it worked perfectly and it was super easy to follow your guide thanks, a lot

ardha27 commented 8 months ago

@john-mnz Sure. I was heavily inspired by Generative Labs' tutorial and code on RunPod Serverless Auto1111. The steps are the following:

  1. Create RunPod network volume. 15GB is just enough for the generic Foocus with Juggernaut model. You can increase its size any time if you need additional models, loras etc. But unfortunately, it cannot be reduced back.
  2. Create a custom Pod Template and use the konieshadow/fooocus-api:latest image. I went with 30GB disk sizes, mount path /workspace, and expose http 8888 and tcp 22.
  3. Run the network volume with the custom fooocus-api image you've just created. You don't need a strong GPU pod, the installation is CPU and download-intensive, but be aware that some older-gen pods might not support the latest CUDA versions. Let it download and install everything. After the Juggernaut model is downloaded, use the connect button to load into the Fooocus-API docs running on the pod's 8888 port. Here you should try all the API methods you plan to use because additional models are downloaded once you run inpaint, outpaint, upscale, vary and image inputs (canny, face swap etc.) endpoints for the first time.
  4. After that you are ready to connect to the pod's console and use cp -r /app/* /workspace/ to copy everything into the persistent network volume
  5. Once everything is copied successfully, you can terminate the pod. You have the network volume ready. ‎
  6. Now you need to create your custom docker image that will run on the actual serverless API. I've published my files into this repo which you can use to build your own. Just be aware that some things might need to be adjusted in handler.py based on how you want to make requests and it's parameters.
  7. Once you build it, upload it to the Docker Hub.
  8. Now you create a custom Serverless Pod Template using the Docker Hub image you've just uploaded. 17GB container disk is enough for all dependencies in my case.
  9. Create a new Serverless API Endpoint. Make sure to choose your Docker Hub image and not the konieshadow/fooocus-api:latest from step 2. In Advanced settings choose your created network volume.
  10. Other settings are your choice, but I personally found that using 4090/L4 GPUs + Flashboot is the most cost-effective one. In frequent use, the 4090 is able to return an image in ~13s including cold start, making it 3-4x cheaper to run this on RunPod than for example using DALLE-3 API. This fact can of course vary based on datacenter locations and GPU availability.

If you have any questions, don't hesitate to ask here or in the newly created repo. Good luck!

i have a question, what happen if i skip step 1-5 and on step 9 i make a new fresh volume?

davefojtik commented 8 months ago

@ardha27 Then it won't work. That's the part where you download and install the Fooocus-API.

You can imagine the serverless worker as a device with only temporary storage that gets wiped each time it stops. That means if you would install the Fooocus API directly onto it, each time it runs, it would have to download all these 7+GBs of models and go through the preparation process. You don't want that.

Instead, we install and prepare it onto the persistent network volume storage that doesn't get wiped each time and is shared between all serverless workers. On these workers, we run just a "launching code" to that Fooocus API installed on the network volume. If you would skip steps 1-5 the worker would be trying to launch something that doesn't exist.

You can do steps 6-10 first and set up and connect the volume afterwards, as well as change and update the network volume through pods at any time. But the 1-5 steps have to be done at some point.