File size: 1,853 Bytes
395720c
0f531f8
 
395720c
9f8919d
6a1f04e
395720c
 
0f531f8
 
9f8919d
0f531f8
9f8919d
0f531f8
9f8919d
395720c
9f8919d
 
 
 
 
0f531f8
6e0f5ef
9f8919d
 
 
 
 
 
 
0f531f8
 
9f8919d
 
0f531f8
 
395720c
9f8919d
0f531f8
395720c
 
 
0f531f8
395720c
 
 
9f8919d
 
395720c
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import gradio as gr
import torch
from diffusers import FluxPriorReduxPipeline, FluxPipeline
from PIL import Image
from huggingface_hub import login
import os

# Hugging Face HubのAPIキーを設定
login(os.getenv("HF_API_KEY"))

# Lazy Loadingを実現するため、モデルロードは関数内で行う
def process_image(image_path):
    # 入力画像をロードしてリサイズ
    image = Image.open(image_path).convert("RGB")
    image = image.resize((256, 256))  # サイズを256x256に制限してメモリ節約

    # Prior Reduxパイプラインのロードと処理
    pipe_prior_redux = FluxPriorReduxPipeline.from_pretrained(
        "black-forest-labs/FLUX.1-Redux-dev",
        use_auth_token=True
    ).to("cpu")
    pipe_prior_output = pipe_prior_redux(image)

    # FLUXパイプラインのロードと処理
    pipe = FluxPipeline.from_pretrained(
        "black-forest-labs/FLUX.1-dev",
        use_auth_token=True,
        text_encoder=None,
        text_encoder_2=None
    ).to("cpu")
    images = pipe(
        guidance_scale=2.5,
        num_inference_steps=25,  # 推論ステップを減らしてメモリ節約
        generator=torch.Generator("cpu").manual_seed(0),  # 再現性のためのシード値
        **pipe_prior_output,
    ).images

    # 結果画像を返す
    return images[0]

# Gradioインターフェースを構築
def infer(image):
    result_image = process_image(image)
    return result_image

with gr.Blocks() as demo:
    gr.Markdown("# FLUX Image Generation App (Optimized for CPU)")

    with gr.Row():
        input_image = gr.Image(type="filepath", label="Input Image")
        output_image = gr.Image(type="pil", label="Generated Image")

    submit_button = gr.Button("Generate")

    submit_button.click(fn=infer, inputs=[input_image], outputs=[output_image])

demo.launch()