Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,34 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
from safetensors import safe_open
|
| 8 |
-
SAFETENSORS_AVAILABLE = True
|
| 9 |
-
except ImportError:
|
| 10 |
-
SAFETENSORS_AVAILABLE = False
|
| 11 |
-
|
| 12 |
-
# Hugging Face Token(Secrets に登録しておく必要あり)
|
| 13 |
-
HF_TOKEN = os.environ.get("HUGGINGFACE_HUB_TOKEN")
|
| 14 |
-
if not HF_TOKEN:
|
| 15 |
-
raise RuntimeError("HUGGINGFACE_HUB_TOKEN が未設定です。SpacesのSecretsで設定してください。")
|
| 16 |
-
|
| 17 |
-
login(token=HF_TOKEN)
|
| 18 |
-
|
| 19 |
-
def check_model(repo_id, filename):
|
| 20 |
-
logs = []
|
| 21 |
-
try:
|
| 22 |
-
model_path = hf_hub_download(
|
| 23 |
-
repo_id=repo_id,
|
| 24 |
-
filename=filename,
|
| 25 |
-
token=HF_TOKEN
|
| 26 |
-
)
|
| 27 |
-
logs.append(f"📥 Downloaded: {model_path}")
|
| 28 |
-
except Exception as e:
|
| 29 |
-
return f"❌ Download failed: {str(e)}"
|
| 30 |
-
|
| 31 |
-
if SAFETENSORS_AVAILABLE and filename.endswith(".safetensors"):
|
| 32 |
-
try:
|
| 33 |
-
with safe_open(model_path, framework="pt", device="cpu") as f:
|
| 34 |
-
logs.append("✅ safetensors loaded successfully")
|
| 35 |
-
logs.append("Keys: " + ", ".join(list(f.keys())[:10]))
|
| 36 |
-
return "\n".join(logs)
|
| 37 |
-
except Exception as e:
|
| 38 |
-
logs.append(f"⚠ safetensors error: {e}")
|
| 39 |
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
fn=check_model,
|
| 53 |
-
inputs=[
|
| 54 |
-
gr.Textbox(label="Repo ID", value="revi13/ip-adapter-faceid-private"),
|
| 55 |
-
gr.Textbox(label="Filename", value="ip-adapter-faceid-plusv2_sd15.bin")
|
| 56 |
-
],
|
| 57 |
-
outputs=gr.Text(label="Result"),
|
| 58 |
-
title="🧪 Hugging Face Hub モデル重みファイル検証ツール",
|
| 59 |
-
description="指定した repo_id / filename のモデル重みをダウンロードして torch.load/safetensors で検証します。"
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
demo.launch()
|
|
|
|
| 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 |
+
# Base64文字列 → PIL画像
|
| 17 |
+
image_data = base64.b64decode(base64_image.split(",")[-1])
|
| 18 |
+
image = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 19 |
+
|
| 20 |
+
# 再エンコードしてBase64で返す
|
| 21 |
+
buffered = io.BytesIO()
|
| 22 |
+
image.save(buffered, format="PNG")
|
| 23 |
+
encoded_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 24 |
+
|
| 25 |
+
return JSONResponse(content={
|
| 26 |
+
"success": True,
|
| 27 |
+
"echo_prompt": prompt,
|
| 28 |
+
"image_base64": f"data:image/png;base64,{encoded_img}"
|
| 29 |
+
})
|
| 30 |
except Exception as e:
|
| 31 |
+
return JSONResponse(status_code=500, content={
|
| 32 |
+
"success": False,
|
| 33 |
+
"error": str(e)
|
| 34 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|