Update lollms_fooocus.py

This commit is contained in:
Saifeddine ALOUI 2024-05-31 11:09:29 +02:00 committed by GitHub
parent b6fd8d0434
commit 942fced394
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -54,14 +54,14 @@ def download_file(url, folder_path, local_filename):
def install_model(lollms_app:LollmsApplication, model_url):
root_dir = lollms_app.lollms_paths.personal_path
shared_folder = root_dir/"shared"
diffusers_folder = shared_folder / "diffusers"
if not PackageManager.check_package_installed("diffusers"):
PackageManager.install_or_update("diffusers")
fooocus_folder = shared_folder / "fooocus"
if not PackageManager.check_package_installed("fooocus"):
PackageManager.install_or_update("fooocus")
if not PackageManager.check_package_installed("torch"):
check_and_install_torch(True)
import torch
from diffusers import PixArtSigmaPipeline
from fooocus import PixArtSigmaPipeline
# You can replace the checkpoint id with "PixArt-alpha/PixArt-Sigma-XL-2-512-MS" too.
pipe = PixArtSigmaPipeline.from_pretrained(
@ -69,32 +69,32 @@ def install_model(lollms_app:LollmsApplication, model_url):
)
def install_diffusers(lollms_app:LollmsApplication):
def install_fooocus(lollms_app:LollmsApplication):
root_dir = lollms_app.lollms_paths.personal_path
shared_folder = root_dir/"shared"
diffusers_folder = shared_folder / "diffusers"
diffusers_folder.mkdir(exist_ok=True, parents=True)
if not PackageManager.check_package_installed("diffusers"):
PackageManager.install_or_update("diffusers")
PackageManager.install_or_update("xformers")
fooocus_folder = shared_folder / "fooocus"
fooocus_folder.mkdir(exist_ok=True, parents=True)
if not PackageManager.check_package_installed("fooocus"):
PackageManager.install_or_update("gradio_client")
def upgrade_diffusers(lollms_app:LollmsApplication):
PackageManager.install_or_update("diffusers")
def upgrade_fooocus(lollms_app:LollmsApplication):
PackageManager.install_or_update("fooocus")
PackageManager.install_or_update("xformers")
class LollmsFooocus(LollmsTTI):
has_controlnet = False
def __init__(
self,
app:LollmsApplication,
wm = "Artbot",
wm = "Artbot",
base_url="localhost:1024"
):
super().__init__(app)
self.ready = False
self.base_url = base_url
# Get the current directory
lollms_paths = app.lollms_paths
root_dir = lollms_paths.personal_path
@ -102,9 +102,9 @@ class LollmsFooocus(LollmsTTI):
self.wm = wm
shared_folder = root_dir/"shared"
self.diffusers_folder = shared_folder / "diffusers"
self.output_dir = root_dir / "outputs/diffusers"
self.models_dir = self.diffusers_folder / "models"
self.fooocus_folder = shared_folder / "fooocus"
self.output_dir = root_dir / "outputs/fooocus"
self.models_dir = self.fooocus_folder / "models"
self.output_dir.mkdir(parents=True, exist_ok=True)
self.models_dir.mkdir(parents=True, exist_ok=True)
@ -116,38 +116,32 @@ class LollmsFooocus(LollmsTTI):
ASCIIColors.red("\_____/\___/|_|_|_| |_| |_|___/ \_| \___/ \___/ \___/ \___|\__,_|___/")
ASCIIColors.red(" ______ ")
ASCIIColors.red(" |______| ")
import torch
from diffusers import PixArtSigmaPipeline
self.model = PixArtSigmaPipeline.from_pretrained(
app.config.diffusers_model, torch_dtype=torch.float16, cache_dir=self.models_dir,
use_safetensors=True,
)
# Enable memory optimizations.
if app.config.diffusers_offloading_mode=="sequential_cpu_offload":
self.model.enable_sequential_cpu_offload()
elif app.coinfig.diffusers_offloading_mode=="model_cpu_offload":
self.model.enable_model_cpu_offload()
if not PackageManager.check_package_installed("gradio_client"):
PackageManager.install_or_update("gradio_client")
from gradio_client import Client
self.client = Client(base_url)
@staticmethod
def verify(app:LollmsApplication):
# Clone repository
root_dir = app.lollms_paths.personal_path
shared_folder = root_dir/"shared"
diffusers_folder = shared_folder / "diffusers"
return diffusers_folder.exists()
fooocus_folder = shared_folder / "fooocus"
return fooocus_folder.exists()
def get(app:LollmsApplication):
root_dir = app.lollms_paths.personal_path
shared_folder = root_dir/"shared"
diffusers_folder = shared_folder / "diffusers"
diffusers_script_path = diffusers_folder / "lollms_diffusers.py"
git_pull(diffusers_folder)
fooocus_folder = shared_folder / "fooocus"
fooocus_script_path = fooocus_folder / "lollms_fooocus.py"
git_pull(fooocus_folder)
if diffusers_script_path.exists():
ASCIIColors.success("lollms_diffusers found.")
if fooocus_script_path.exists():
ASCIIColors.success("lollms_fooocus found.")
ASCIIColors.success("Loading source file...",end="")
# use importlib to load the module from the file path
from lollms.services.diffusers.lollms_diffusers import LollmsFooocus
from lollms.services.fooocus.lollms_fooocus import LollmsFooocus
ASCIIColors.success("ok")
return LollmsFooocus
@ -169,7 +163,8 @@ class LollmsFooocus(LollmsTTI):
):
if output_path is None:
output_path = self.output_dir
from diffusers.utils.pil_utils import pt_to_pil
self.client.predict()
image = self.model(positive_prompt, negative_prompt=negative_prompt, guidance_scale=scale, num_inference_steps=steps,).images[0]
output_path = Path(output_path)
fn = find_next_available_filename(output_path,"diff_img_")