Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,61 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from diffusers import FluxPriorReduxPipeline, FluxPipeline
|
4 |
-
from diffusers.utils import load_image
|
5 |
-
from huggingface_hub import login
|
6 |
from PIL import Image
|
7 |
-
import
|
8 |
|
9 |
# Hugging Face HubのAPIキーを設定
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
pipe_prior_redux = FluxPriorReduxPipeline.from_pretrained(
|
14 |
-
"black-forest-labs/FLUX.1-Redux-dev",
|
15 |
-
use_auth_token=True # APIキーを使用して認証
|
16 |
-
).to("cpu") # CPUに変更
|
17 |
-
|
18 |
-
# メインのFLUXパイプラインをロード (CPU対応)
|
19 |
-
pipe = FluxPipeline.from_pretrained(
|
20 |
-
"black-forest-labs/FLUX.1-dev",
|
21 |
-
use_auth_token=True, # APIキーを使用して認証
|
22 |
-
text_encoder=None,
|
23 |
-
text_encoder_2=None
|
24 |
-
).to("cpu") # CPUに変更
|
25 |
-
|
26 |
-
def process_image(image_path):
|
27 |
-
# 入力画像をロード
|
28 |
-
image = Image.open(image_path).convert("RGB")
|
29 |
|
30 |
-
|
31 |
-
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
**pipe_prior_output,
|
39 |
-
).images
|
40 |
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
# Gradioインターフェースを構築
|
45 |
def infer(image):
|
46 |
-
result_image =
|
47 |
return result_image
|
48 |
|
49 |
with gr.Blocks() as demo:
|
50 |
-
gr.Markdown("# FLUX Image Generation App")
|
51 |
|
52 |
with gr.Row():
|
53 |
input_image = gr.Image(type="filepath", label="Input Image")
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
|
|
|
|
|
|
3 |
from PIL import Image
|
4 |
+
from io import BytesIO
|
5 |
|
6 |
# Hugging Face HubのAPIキーを設定
|
7 |
+
HF_API_KEY = os.getenv("HF_API_KEY") # 環境変数からAPIキーを取得
|
8 |
+
API_URL_PRIOR = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-Redux-dev"
|
9 |
+
API_URL_FLUX = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
headers = {
|
12 |
+
"Authorization": f"Bearer {HF_API_KEY}"
|
13 |
+
}
|
14 |
|
15 |
+
def call_hf_api(api_url, payload):
|
16 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
17 |
+
if response.status_code != 200:
|
18 |
+
raise Exception(f"Request failed with status {response.status_code}, {response.text}")
|
19 |
+
return response.json()
|
|
|
|
|
20 |
|
21 |
+
def process_image_with_api(image_path):
|
22 |
+
# 入力画像をロード
|
23 |
+
with open(image_path, "rb") as f:
|
24 |
+
image_bytes = f.read()
|
25 |
+
|
26 |
+
# Prior Reduxモデルで事前処理
|
27 |
+
prior_payload = {"inputs": image_bytes}
|
28 |
+
prior_response = call_hf_api(API_URL_PRIOR, prior_payload)
|
29 |
+
|
30 |
+
# FLUXモデルで画像生成
|
31 |
+
flux_payload = {
|
32 |
+
"inputs": prior_response, # Prior Reduxの出力をFLUXモデルに渡す
|
33 |
+
"parameters": {
|
34 |
+
"guidance_scale": 2.5,
|
35 |
+
"num_inference_steps": 50,
|
36 |
+
"seed": 0, # 再現性のためのシード値
|
37 |
+
}
|
38 |
+
}
|
39 |
+
flux_response = call_hf_api(API_URL_FLUX, flux_payload)
|
40 |
+
|
41 |
+
# 生成された画像を取得
|
42 |
+
generated_image_url = flux_response.get("generated_image_url")
|
43 |
+
if not generated_image_url:
|
44 |
+
raise Exception("Generated image URL not found in the response.")
|
45 |
+
|
46 |
+
# URLから画像をダウンロード
|
47 |
+
response = requests.get(generated_image_url)
|
48 |
+
generated_image = Image.open(BytesIO(response.content))
|
49 |
+
|
50 |
+
return generated_image
|
51 |
|
52 |
# Gradioインターフェースを構築
|
53 |
def infer(image):
|
54 |
+
result_image = process_image_with_api(image)
|
55 |
return result_image
|
56 |
|
57 |
with gr.Blocks() as demo:
|
58 |
+
gr.Markdown("# FLUX Image Generation App (via Hugging Face API)")
|
59 |
|
60 |
with gr.Row():
|
61 |
input_image = gr.Image(type="filepath", label="Input Image")
|