Fooocus 文生图代码调用

GitHub - lllyasviel/Fooocus: Focus on prompting and generating

Colab

(Last tested - 2024 Aug 12 by mashb1t)

https://colab.research.google.com/github/lllyasviel/Fooocus/blob/main/fooocus_colab.ipynb

!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}")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值