赞
踩
用fastapi写的前后端,确实是好代码,没用webui了,这样可以直接在内网就打开页面,还是非常实用的,以往用FastAPI封的很简单,这块值得学习。
- prepare_environment()->
- - prepare_submodules()
- - check_dirs()
- - validate_requirements()
- - setup_windows_bitsandbytes()
- - setup_onnxruntime()
- run_tag_editor()->
- run_tensorboard()->
-
- uvicorn.run()->mikazuki.app->
- - mikazuki.app.application->
- -- app=FastAPI(lifespan)
- -- app.include_router(proxy_router) # 可以处理该路由器定义的http请求
- -- app.include_router(api_router,prefix="/api")->
- --- router = APIRouter()
- --- @router.post("/interrogate")
- ---- async def run_interrogate(req:TaggerInterrogateRequest,background_task)->
- ---- interrogator = available_interrogators.get(req.interrogator_model, available_interrogators["wd14-convnextv2-v2"])
- ---- background_tasks.add_task()
- ----- on_interrogate()->
- ----- ratings,tags = interrogator.interrogate(image)->
- ----- processed_tags = Interrogator.postprocess_tags(tag,*postprocess_opts)
-
- --- @router.post("/run")
- --- async def create_toml_file(request:Request)->
- --- json_data = await request.body()->
- --- config = json.loads(json_data.decode("utf-8"))->
- --- trainer_file = trainer_mapping[model_train_type]->
- --- validated, message = train_utils.validate_model(config["pretrained_model_name_or_path"])
- --- result = process.run_train(toml_file, trainer_file, gpu_ids, suggest_cpu_threads)
- ---- python -m accelerate.commands.launch --num_cpu_threads_per_process=4 \
- "/home/image_team/image_team_docker_home/lgd/e_commerce_sd/tools/sd_lora/sd-scripts/train_network.py" \
- --config_file="E:\comprehensive_library\e_commerce_sd\tools\sd_lora\config\lora_sd_sciprts.toml"
主要是两个接口
- import sys
- import json
- import requests
- from pathlib import Path
-
- root_dir = Path(__file__).resolve().parent.parent
- sys.path.append(str(root_dir))
-
- interrogator = False
- lora_new = True
- lora_expert = False
-
- input_json_interrogator = {
- "path": "/home/image_team/image_team_docker_home/lgd/common/lora-scripts-main/lion/",
- "interrogator_model": "wd14-convnextv2-v2",
- "threshold": 0.35,
- "additional_tags": "",
- "exclude_tags": "",
- "escape_tag": True,
- "batch_input_recursive": False,
- "batch_output_action_on_conflict": "ignore",
- "replace_underscore": True
- }
-
- input_json_lora_new = {
- 'pretrained_model_name_or_path': '/home/image_team/image_team_docker_home/lgd/e_commerce_sd/stable-diffusion-webui/models/Stable-diffusion/revAnimated_v121.safetensors',
- 'train_data_dir': '/home/image_team/image_team_docker_home/lgd/common/lora-scripts-main/lion/',
- 'resolution': '512,512',
- 'enable_bucket': True,
- 'min_bucket_reso': 256,
- 'max_bucket_reso': 1024,
- 'output_name': 'aki',
- 'output_dir': './output',
- 'save_model_as': 'safetensors',
- 'save_every_n_epochs': 2,
- 'max_train_epochs': 10,
- 'train_batch_size': 1,
- 'network_train_unet_only': False,
- 'network_train_text_encoder_only': False,
- 'learning_rate': 0.0001,
- 'unet_lr': 0.0001,
- 'text_encoder_lr': 1e-05,
- 'lr_scheduler': 'cosine_with_restarts',
- 'optimizer_type': 'AdamW8bit',
- 'lr_scheduler_num_cycles': 1,
- 'network_module': 'networks.lora',
- 'network_dim': 32,
- 'network_alpha': 32,
- 'logging_dir': './logs',
- 'caption_extension': '.txt',
- 'shuffle_caption': True,
- 'keep_tokens': 0,
- 'max_token_length': 255,
- 'seed': 1337,
- 'prior_loss_weight': 1,
- 'clip_skip': 2,
- 'mixed_precision': 'fp16',
- 'save_precision': 'fp16',
- 'xformers': True,
- 'cache_latents': True,
- 'persistent_data_loader_workers': True,
- 'lr_warmup_steps': 0,
- 'gpu_ids': ['0', '1', '2', '3']}
-
- # input_json_lora_expert = {
- #
- # }
-
- if interrogator:
- results = requests.post("http://10.111.132.198:28000/api/interrogate", json=input_json_interrogator)
- print(results.json())
-
- if lora_new:
- results = requests.post("http://10.111.132.198:28000/api/run", json=input_json_lora_new)
- print(results.json())
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。