revi13 commited on
Commit
c90e8d6
·
verified ·
1 Parent(s): 5f35f33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -1,45 +1,54 @@
1
  import os
2
  import io
3
  import base64
 
4
  from PIL import Image
5
- from huggingface_hub import login, hf_hub_download
6
  from fastapi import FastAPI, Request
7
  from fastapi.responses import JSONResponse
8
- import traceback
9
 
10
- # 書き込み可能なキャッシュディレクトリに変更
11
- os.environ["HUGGINGFACE_HUB_CACHE"] = "/tmp/huggingface"
 
 
 
 
12
 
13
- # ✅ トークン取得・ログイン
14
  hf_token = os.environ.get("HUGGINGFACE_HUB_TOKEN")
15
  if not hf_token:
16
  raise ValueError("環境変数 'HUGGINGFACE_HUB_TOKEN' が設定されていません。")
17
- login(hf_token)
18
 
19
- # ✅ モデルファイル読み込み
20
  model_path = hf_hub_download(
21
  repo_id="revi13/ip-adapter-faceid-private",
22
  filename="ip-adapter-faceid-plusv2_sd15.bin",
23
  token=hf_token
24
  )
25
 
26
- from pipeline import IPFacePlusV2Pipeline
27
  pipeline = IPFacePlusV2Pipeline(model_path)
28
 
 
29
  app = FastAPI()
30
 
31
  @app.get("/")
32
  def root():
33
  return {"status": "ok"}
34
 
 
35
  @app.post("/generate")
36
  async def generate_image_api(request: Request):
37
  try:
38
  body = await request.json()
39
  base64_image = body.get("image")
40
  prompt = body.get("prompt", "portrait")
 
 
41
  image_base64 = pipeline.generate_image(base64_image, prompt)
 
42
  return JSONResponse(content={"success": True, "image_base64": image_base64})
43
  except Exception as e:
 
44
  traceback.print_exc()
 
45
  return JSONResponse(status_code=500, content={"success": False, "error": str(e)})
 
1
  import os
2
  import io
3
  import base64
4
+ import traceback
5
  from PIL import Image
 
6
  from fastapi import FastAPI, Request
7
  from fastapi.responses import JSONResponse
 
8
 
9
+ from huggingface_hub import hf_hub_download
10
+ from pipeline import IPFacePlusV2Pipeline
11
+
12
+ # ✅ HuggingFace Spaces に対応するキャッシュディレクトリ設定
13
+ os.environ["HUGGINGFACE_HUB_CACHE"] = "/tmp/.cache"
14
+ os.environ["HF_HOME"] = "/tmp/.cache"
15
 
16
+ # ✅ トークン取得(Spacesでは Secrets に登録しておく)
17
  hf_token = os.environ.get("HUGGINGFACE_HUB_TOKEN")
18
  if not hf_token:
19
  raise ValueError("環境変数 'HUGGINGFACE_HUB_TOKEN' が設定されていません。")
 
20
 
21
+ # ✅ モデルファイルを直接トークン付きでダウンロード(login()不要)
22
  model_path = hf_hub_download(
23
  repo_id="revi13/ip-adapter-faceid-private",
24
  filename="ip-adapter-faceid-plusv2_sd15.bin",
25
  token=hf_token
26
  )
27
 
28
+ # パイプライン初期化
29
  pipeline = IPFacePlusV2Pipeline(model_path)
30
 
31
+ # ✅ FastAPI アプリ作成
32
  app = FastAPI()
33
 
34
  @app.get("/")
35
  def root():
36
  return {"status": "ok"}
37
 
38
+ # ✅ POST /generate API
39
  @app.post("/generate")
40
  async def generate_image_api(request: Request):
41
  try:
42
  body = await request.json()
43
  base64_image = body.get("image")
44
  prompt = body.get("prompt", "portrait")
45
+
46
+ # ✅ パイプラインで画像生成
47
  image_base64 = pipeline.generate_image(base64_image, prompt)
48
+
49
  return JSONResponse(content={"success": True, "image_base64": image_base64})
50
  except Exception as e:
51
+ print("=== ERROR OCCURRED ===")
52
  traceback.print_exc()
53
+ print("======================")
54
  return JSONResponse(status_code=500, content={"success": False, "error": str(e)})