Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,49 @@
|
|
| 1 |
-
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
demo.launch()
|
|
|
|
| 1 |
+
|
| 2 |
import os
|
| 3 |
+
from huggingface_hub import login, hf_hub_download
|
| 4 |
+
|
| 5 |
+
# :white_check_mark: トークン取得 & ログイン(重要)
|
| 6 |
+
hf_token = os.environ.get("HUGGINGFACE_HUB_TOKEN")
|
| 7 |
+
if not hf_token:
|
| 8 |
+
raise ValueError("環境変数 'HUGGINGFACE_HUB_TOKEN' が設定されていません。Spacesの Secrets に設定してください。")
|
| 9 |
+
login(hf_token) # 必ず login() を呼び出す
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
from PIL import Image
|
| 13 |
+
import base64
|
| 14 |
+
import io
|
| 15 |
+
|
| 16 |
+
# :white_check_mark: モデルファイルのダウンロード(token 明示)
|
| 17 |
+
model_path = hf_hub_download(
|
| 18 |
+
repo_id="revi13/ip-adapter-faceid-private",
|
| 19 |
+
filename="ip-adapter-faceid-plusv2_sd15.bin",
|
| 20 |
+
token=hf_token
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# ===== :brain: 推論パイプラインの読み込み =====
|
| 24 |
+
from pipeline import IPFacePlusV2Pipeline
|
| 25 |
+
pipeline = IPFacePlusV2Pipeline(model_path)
|
| 26 |
+
|
| 27 |
+
# ===== :art: 画像生成関数 =====
|
| 28 |
+
def generate_image(base64_image: str, prompt: str):
|
| 29 |
+
try:
|
| 30 |
+
image_data = base64.b64decode(base64_image.split(',')[-1])
|
| 31 |
+
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 32 |
+
output = pipeline.generate(image, prompt)
|
| 33 |
+
return output
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Error: {str(e)}"
|
| 36 |
|
| 37 |
+
# ===== :control_knobs: Gradio UI構築 =====
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=generate_image,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.Text(label="Base64 Image"),
|
| 42 |
+
gr.Text(label="Prompt")
|
| 43 |
+
],
|
| 44 |
+
outputs=gr.Image(type="pil"),
|
| 45 |
+
allow_flagging="never"
|
| 46 |
+
)
|
| 47 |
|
| 48 |
+
# :rocket: Spacesでは __main__ チェック不要
|
| 49 |
demo.launch()
|