GitHub - lllyasviel/Fooocus: Focus on prompting and generating
Colab
(Last tested - 2024 Aug 12 by mashb1t)
!pip install pygit2==1.15.1
%cd /content
!git clone https://github.com/lllyasviel/Fooocus.git
%cd /content/Fooocus
!python entry_with_update.py --share --always-high-vram
Python
import os
import subprocess
def run_command(command, cwd=None):
"""Helper function to run shell commands and handle errors."""
try:
subprocess.run(command, shell=True, check=True, cwd=cwd)
except subprocess.CalledProcessError as e:
print(f"Error while running command: {command}\n{e}")
exit(1)
# Step 1: 安装依赖
run_command("pip install pygit2==1.15.1")
# Step 2: 设置工作目录
workspace_dir = "/content"
fooocus_dir = os.path.join(workspace_dir, "Fooocus")
# 确保工作目录存在
if not os.path.exists(workspace_dir):
os.makedirs(workspace_dir)
# 切换到工作目录
os.chdir(workspace_dir)
# Step 3: 克隆项目
if not os.path.exists(fooocus_dir):
run_command("git clone https://github.com/lllyasviel/Fooocus.git")
else:
print("Fooocus 项目已存在,跳过克隆步骤")
# Step 4: 切换到项目目录
if os.path.exists(fooocus_dir):
os.chdir(fooocus_dir)
else:
raise FileNotFoundError("Fooocus 项目未正确克隆,请检查网络连接或仓库URL")
# Step 5: 检查脚本是否存在并运行
entry_script = "entry_with_update.py"
if os.path.exists(entry_script):
run_command(f"python {entry_script} --share --always-high-vram", cwd=fooocus_dir)
else:
raise FileNotFoundError(f"{entry_script} 文件未找到,请确认 Fooocus 项目是否完整")
konieshadow/fooocus-api – Run with an API on Replicate
import replicate
import os
import requests
from PIL import Image
from io import BytesIO
# pip install replicate Pillow requests
# 在Python脚本中直接设置REPLICATE_API_TOKEN
os.environ['REPLICATE_API_TOKEN'] = 'r8_********'
# 输入提示词
prompt_text = input("prompt:")
# 使用 replicate.run 运行指定的模型
image_url_list = replicate.run(
"konieshadow/fooocus-api:65c59c96f2fb45332edec7f9f740b2a4d494a5f9c43b193dc98066d15da55a8c",
input={"prompt": prompt_text}
)
# 从返回的列表中提取实际的图片URL
actual_image_url = image_url_list[0]
# 从URL中提取特定部分用作文件名
image_id = actual_image_url.split("/")[-1].split(".")[0]
image_filename = f"{image_id}.png"
# 从URL获取图片数据
response = requests.get(actual_image_url)
# 将图片数据写入到本地文件
with open(image_filename, 'wb') as file:
file.write(response.content)
print(f"api-tokens: https://replicate.com/account/api-tokens")
print(f"images:https://replicate.com/predictions")
print(f"png:{image_filename}")