revi13 commited on
Commit
fcae93c
·
verified ·
1 Parent(s): 5cccad2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -8
app.py CHANGED
@@ -1,12 +1,62 @@
1
- import gradio as gr
2
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- def check_token():
5
- token = os.environ.get("HUGGINGFACE_HUB_TOKEN")
6
- if token:
7
- return " トークン取得成功(最初の10文字): " + token[:10] + "..."
8
- else:
9
- return "❌ トークンが取得できません。Spacesの Secrets に設定してください。"
 
 
 
 
10
 
11
- demo = gr.Interface(fn=check_token, inputs=[], outputs="text")
12
  demo.launch()
 
 
1
  import os
2
+ import gradio as gr
3
+ from huggingface_hub import hf_hub_download, login
4
+ import torch
5
+
6
+ try:
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
+ sd = torch.load(model_path, map_location="cpu")
42
+ logs.append("✅ torch.load OK")
43
+ if isinstance(sd, dict):
44
+ logs.append("Top-level keys: " + ", ".join(list(sd.keys())[:10]))
45
+ else:
46
+ logs.append(f"Loaded object type: {type(sd)}")
47
+ except Exception as e:
48
+ logs.append(f"❌ torch.load failed: {e}")
49
+ return "\n".join(logs)
50
 
51
+ demo = gr.Interface(
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()