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
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
|
4 |
+
|
5 |
+
root = os.path.dirname(os.path.abspath(__file__))
|
6 |
+
sys.path.append(root)
|
7 |
+
os.chdir(root)
|
8 |
+
|
9 |
+
|
10 |
+
try:
|
11 |
+
import pygit2
|
12 |
+
pygit2.option(pygit2.GIT_OPT_SET_OWNER_VALIDATION, 0)
|
13 |
+
|
14 |
+
repo = pygit2.Repository(os.path.abspath(os.path.dirname(__file__)))
|
15 |
+
|
16 |
+
branch_name = repo.head.shorthand
|
17 |
+
|
18 |
+
remote_name = 'origin'
|
19 |
+
remote = repo.remotes[remote_name]
|
20 |
+
|
21 |
+
remote.fetch()
|
22 |
+
|
23 |
+
local_branch_ref = f'refs/heads/{branch_name}'
|
24 |
+
local_branch = repo.lookup_reference(local_branch_ref)
|
25 |
+
|
26 |
+
remote_reference = f'refs/remotes/{remote_name}/{branch_name}'
|
27 |
+
remote_commit = repo.revparse_single(remote_reference)
|
28 |
+
|
29 |
+
merge_result, _ = repo.merge_analysis(remote_commit.id)
|
30 |
+
|
31 |
+
if merge_result & pygit2.GIT_MERGE_ANALYSIS_UP_TO_DATE:
|
32 |
+
print("Already up-to-date")
|
33 |
+
elif merge_result & pygit2.GIT_MERGE_ANALYSIS_FASTFORWARD:
|
34 |
+
local_branch.set_target(remote_commit.id)
|
35 |
+
repo.head.set_target(remote_commit.id)
|
36 |
+
repo.checkout_tree(repo.get(remote_commit.id))
|
37 |
+
repo.reset(local_branch.target, pygit2.GIT_RESET_HARD)
|
38 |
+
print("Fast-forward merge")
|
39 |
+
elif merge_result & pygit2.GIT_MERGE_ANALYSIS_NORMAL:
|
40 |
+
print("Update failed - Did you modify any file?")
|
41 |
+
except Exception as e:
|
42 |
+
print('Update failed.')
|
43 |
+
print(str(e))
|
44 |
+
|
45 |
+
import os
|
46 |
+
import sys
|
47 |
+
import ssl
|
48 |
+
|
49 |
+
print('[System ARGV] ' + str(sys.argv))
|
50 |
+
|
51 |
+
root = os.path.dirname(os.path.abspath(__file__))
|
52 |
+
sys.path.append(root)
|
53 |
+
os.chdir(root)
|
54 |
+
|
55 |
+
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
|
56 |
+
os.environ["PYTORCH_MPS_HIGH_WATERMARK_RATIO"] = "0.0"
|
57 |
+
if "GRADIO_SERVER_PORT" not in os.environ:
|
58 |
+
os.environ["GRADIO_SERVER_PORT"] = "7865"
|
59 |
+
|
60 |
+
ssl._create_default_https_context = ssl._create_unverified_context
|
61 |
+
|
62 |
+
|
63 |
+
import platform
|
64 |
+
import fooocus_version
|
65 |
+
|
66 |
+
from build_launcher import build_launcher
|
67 |
+
from modules.launch_util import is_installed, run, python, run_pip, requirements_met
|
68 |
+
from modules.model_loader import load_file_from_url
|
69 |
+
|
70 |
+
|
71 |
+
REINSTALL_ALL = False
|
72 |
+
TRY_INSTALL_XFORMERS = False
|
73 |
+
|
74 |
+
|
75 |
+
def prepare_environment():
|
76 |
+
torch_index_url = os.environ.get('TORCH_INDEX_URL', "https://download.pytorch.org/whl/cu121")
|
77 |
+
torch_command = os.environ.get('TORCH_COMMAND',
|
78 |
+
f"pip install torch==2.1.0 torchvision==0.16.0 --extra-index-url {torch_index_url}")
|
79 |
+
requirements_file = os.environ.get('REQS_FILE', "requirements_versions.txt")
|
80 |
+
|
81 |
+
print(f"Python {sys.version}")
|
82 |
+
print(f"Fooocus version: {fooocus_version.version}")
|
83 |
+
|
84 |
+
if REINSTALL_ALL or not is_installed("torch") or not is_installed("torchvision"):
|
85 |
+
run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch", live=True)
|
86 |
+
|
87 |
+
if TRY_INSTALL_XFORMERS:
|
88 |
+
if REINSTALL_ALL or not is_installed("xformers"):
|
89 |
+
xformers_package = os.environ.get('XFORMERS_PACKAGE', 'xformers==0.0.23')
|
90 |
+
if platform.system() == "Windows":
|
91 |
+
if platform.python_version().startswith("3.10"):
|
92 |
+
run_pip(f"install -U -I --no-deps {xformers_package}", "xformers", live=True)
|
93 |
+
else:
|
94 |
+
print("Installation of xformers is not supported in this version of Python.")
|
95 |
+
print(
|
96 |
+
"You can also check this and build manually: https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Xformers#building-xformers-on-windows-by-duckness")
|
97 |
+
if not is_installed("xformers"):
|
98 |
+
exit(0)
|
99 |
+
elif platform.system() == "Linux":
|
100 |
+
run_pip(f"install -U -I --no-deps {xformers_package}", "xformers")
|
101 |
+
|
102 |
+
if REINSTALL_ALL or not requirements_met(requirements_file):
|
103 |
+
run_pip(f"install -r \"{requirements_file}\"", "requirements")
|
104 |
+
|
105 |
+
return
|
106 |
+
|
107 |
+
|
108 |
+
vae_approx_filenames = [
|
109 |
+
('xlvaeapp.pth', 'https://huggingface.co/lllyasviel/misc/resolve/main/xlvaeapp.pth'),
|
110 |
+
('vaeapp_sd15.pth', 'https://huggingface.co/lllyasviel/misc/resolve/main/vaeapp_sd15.pt'),
|
111 |
+
('xl-to-v1_interposer-v3.1.safetensors',
|
112 |
+
'https://huggingface.co/lllyasviel/misc/resolve/main/xl-to-v1_interposer-v3.1.safetensors')
|
113 |
+
]
|
114 |
+
|
115 |
+
def ini_args():
|
116 |
+
from args_manager import args
|
117 |
+
return args
|
118 |
+
|
119 |
+
|
120 |
+
prepare_environment()
|
121 |
+
build_launcher()
|
122 |
+
args = ini_args()
|
123 |
+
|
124 |
+
|
125 |
+
if args.gpu_device_id is not None:
|
126 |
+
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu_device_id)
|
127 |
+
print("Set device to:", args.gpu_device_id)
|
128 |
+
|
129 |
+
|
130 |
+
from modules import config
|
131 |
+
|
132 |
+
def download_models():
|
133 |
+
for file_name, url in vae_approx_filenames:
|
134 |
+
load_file_from_url(url=url, model_dir=config.path_vae_approx, file_name=file_name)
|
135 |
+
|
136 |
+
load_file_from_url(
|
137 |
+
url='https://huggingface.co/lllyasviel/misc/resolve/main/fooocus_expansion.bin',
|
138 |
+
model_dir=config.path_fooocus_expansion,
|
139 |
+
file_name='pytorch_model.bin'
|
140 |
)
|
141 |
|
142 |
+
if args.disable_preset_download:
|
143 |
+
print('Skipped model download.')
|
144 |
+
return
|
145 |
+
|
146 |
+
if not args.always_download_new_model:
|
147 |
+
if not os.path.exists(os.path.join(config.paths_checkpoints[0], config.default_base_model_name)):
|
148 |
+
for alternative_model_name in config.previous_default_models:
|
149 |
+
if os.path.exists(os.path.join(config.paths_checkpoints[0], alternative_model_name)):
|
150 |
+
print(f'You do not have [{config.default_base_model_name}] but you have [{alternative_model_name}].')
|
151 |
+
print(f'Fooocus will use [{alternative_model_name}] to avoid downloading new models, '
|
152 |
+
f'but you are not using latest models.')
|
153 |
+
print('Use --always-download-new-model to avoid fallback and always get new models.')
|
154 |
+
config.checkpoint_downloads = {}
|
155 |
+
config.default_base_model_name = alternative_model_name
|
156 |
+
break
|
157 |
+
|
158 |
+
for file_name, url in config.checkpoint_downloads.items():
|
159 |
+
load_file_from_url(url=url, model_dir=config.paths_checkpoints[0], file_name=file_name)
|
160 |
+
for file_name, url in config.embeddings_downloads.items():
|
161 |
+
load_file_from_url(url=url, model_dir=config.path_embeddings, file_name=file_name)
|
162 |
+
for file_name, url in config.lora_downloads.items():
|
163 |
+
load_file_from_url(url=url, model_dir=config.paths_loras[0], file_name=file_name)
|
164 |
+
|
165 |
+
return
|
166 |
+
|
167 |
+
|
168 |
+
download_models()
|
169 |
+
|
170 |
+
import gradio as gr
|
171 |
+
import modules.gradio_hijack as grh
|
172 |
+
from extras.interrogate import default_interrogator as default_interrogator_photo
|
173 |
+
from extras.wd14tagger import default_interrogator as default_interrogator_anime
|
174 |
+
import modules.flags as flags
|
175 |
+
|
176 |
+
def interrogatorFunction(img, value):
|
177 |
+
if value == flags.desc_type_photo: # Menggunakan operator perbandingan '==' untuk memeriksa kesamaan
|
178 |
+
output = default_interrogator_photo(img)
|
179 |
+
print(output)
|
180 |
+
else:
|
181 |
+
output = default_interrogator_anime(img)
|
182 |
+
print(output)
|
183 |
+
return output
|
184 |
+
|
185 |
+
ui = gr.Interface(
|
186 |
+
fn=interrogatorFunction,
|
187 |
+
inputs=[
|
188 |
+
grh.Image(label='Input', source='upload', type='numpy'),
|
189 |
+
gr.Radio(
|
190 |
+
label='Content Type',
|
191 |
+
choices=[flags.desc_type_photo, flags.desc_type_anime],
|
192 |
+
value=flags.desc_type_photo
|
193 |
+
)
|
194 |
+
],
|
195 |
+
outputs=gr.Textbox(type="text", label="Output", show_copy_button=True)
|
196 |
+
)
|
197 |
+
|
198 |
+
ui.launch()
|