revi13 commited on
Commit
31f8b71
·
verified ·
1 Parent(s): 234057e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -17
app.py CHANGED
@@ -1,30 +1,70 @@
1
- from fastapi import FastAPI, Request
2
- from fastapi.responses import JSONResponse
3
- import base64
4
  import io
 
5
  from PIL import Image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
 
 
 
 
 
7
  app = FastAPI()
8
 
 
9
  @app.post("/generate")
10
- async def generate_image(request: Request):
11
  try:
12
  body = await request.json()
13
  base64_image = body.get("image")
14
- prompt = body.get("prompt", "")
15
-
16
- # 画像を受け取ってそのまま再エンコードして返す
17
- image_data = base64.b64decode(base64_image.split(",")[-1])
18
- image = Image.open(io.BytesIO(image_data)).convert("RGB")
19
 
20
- buffered = io.BytesIO()
21
- image.save(buffered, format="PNG")
22
- encoded_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
23
 
24
- return JSONResponse(content={
25
- "success": True,
26
- "echo_prompt": prompt,
27
- "image_base64": f"data:image/png;base64,{encoded_img}"
28
- })
29
  except Exception as e:
30
  return JSONResponse(status_code=500, content={"success": False, "error": str(e)})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
 
 
2
  import io
3
+ import base64
4
  from PIL import Image
5
+ from huggingface_hub import login, hf_hub_download
6
+
7
+ from fastapi import FastAPI, Request
8
+ from fastapi.responses import JSONResponse
9
+ import gradio as gr
10
+ from gradio.routes import mount_gradio_app
11
+
12
+ # ✅ Hugging Face トークンの取得とログイン
13
+ hf_token = os.environ.get("HUGGINGFACE_HUB_TOKEN")
14
+ if not hf_token:
15
+ raise ValueError("環境変数 'HUGGINGFACE_HUB_TOKEN' が設定されていません。Spacesの Secrets に設定してください。")
16
+ login(hf_token)
17
+
18
+ # ✅ モデルファイルのダウンロード
19
+ model_path = hf_hub_download(
20
+ repo_id="revi13/ip-adapter-faceid-private",
21
+ filename="ip-adapter-faceid-plusv2_sd15.bin",
22
+ token=hf_token
23
+ )
24
 
25
+ # ✅ 推論パイプラインの読み込み
26
+ from pipeline import IPFacePlusV2Pipeline
27
+ pipeline = IPFacePlusV2Pipeline(model_path)
28
+
29
+ # ✅ FastAPI アプリ作成
30
  app = FastAPI()
31
 
32
+ # 🎯 APIエンドポイント(POST /generate)
33
  @app.post("/generate")
34
+ async def generate_image_api(request: Request):
35
  try:
36
  body = await request.json()
37
  base64_image = body.get("image")
38
+ prompt = body.get("prompt", "portrait")
 
 
 
 
39
 
40
+ # `generate_image` を使用するのが正しい
41
+ image_base64 = pipeline.generate_image(base64_image, prompt)
 
42
 
43
+ return JSONResponse(content={"success": True, "image_base64": image_base64})
 
 
 
 
44
  except Exception as e:
45
  return JSONResponse(status_code=500, content={"success": False, "error": str(e)})
46
+
47
+ # ✅ Gradio UI(手動確認用)
48
+ def generate_image_gradio(base64_image: str, prompt: str):
49
+ try:
50
+ return pipeline.generate_image(base64_image, prompt)
51
+ except Exception as e:
52
+ return f"Error: {str(e)}"
53
+
54
+ demo = gr.Interface(
55
+ fn=generate_image_gradio,
56
+ inputs=[
57
+ gr.Text(label="Base64 Image (data:image/png;base64,...)"),
58
+ gr.Text(label="Prompt")
59
+ ],
60
+ outputs=gr.Image(type="pil", label="Output Image"),
61
+ allow_flagging="never",
62
+ title="IP-Adapter FaceID Plus v2",
63
+ description="Base64で画像を入力してください。"
64
+ )
65
+
66
+ # Gradio を FastAPI にマウント
67
+ app = mount_gradio_app(app, demo, path="/")
68
+
69
+ # Spacesでの実行に必要(CLIでは無視される)
70
+ demo.queue().launch()