tomo2chin2 commited on
Commit
15f27f7
·
verified ·
1 Parent(s): c2a91bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -4,7 +4,6 @@ import re
4
  import requests
5
  import gradio as gr
6
  from huggingface_hub import HfApi
7
- from civitai import Civitai
8
 
9
  # 環境変数から取得
10
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
@@ -15,9 +14,6 @@ CIVITAI_TOKEN = os.environ.get("CIVITAI_TOKEN", None) # Civitai用トークン
15
  # Hugging Face APIの初期化
16
  api = HfApi()
17
 
18
- # Civitai APIの初期化
19
- civitai = Civitai(auth=CIVITAI_TOKEN)
20
-
21
  def extract_model_id(url):
22
  """
23
  CivitaiのダウンロードURLからモデルIDを抽出する関数。
@@ -30,23 +26,36 @@ def extract_model_id(url):
30
  else:
31
  return None
32
 
33
- def get_metadata_civitai(model_id):
34
  """
35
- civitai-pyを使用してCivitai APIからモデルのトリガーワードと推奨強度を取得する関数。
36
  """
37
  if not model_id:
38
  return "N/A", "N/A"
 
 
 
 
 
39
 
40
  try:
41
- model = civitai.models.get(model_id)
 
 
 
 
 
 
42
 
43
  # トリガーワードの取得
44
- trigger_words = model.trainedWords if hasattr(model, 'trainedWords') else "N/A"
 
 
45
 
46
  # 推奨強度の取得
47
- # description フィールドから strength を抽出
48
- description = model.description if hasattr(model, 'description') else ""
49
- strength_match = re.search(r'strength:?\s*([\d.]+)', description, re.IGNORECASE)
50
  if strength_match:
51
  strength = float(strength_match.group(1))
52
  else:
@@ -66,9 +75,13 @@ def download_and_upload(url, repo_type):
66
  if not model_id:
67
  return "URLからモデルIDを抽出できませんでした。URL形式を確認してください。"
68
 
 
 
 
 
69
  try:
70
  # ファイルのダウンロード
71
- response = requests.get(url)
72
  response.raise_for_status()
73
  except Exception as e:
74
  return f"ファイルのダウンロードに失敗しました: {e}"
@@ -92,7 +105,7 @@ def download_and_upload(url, repo_type):
92
  return f"ファイルの保存に失敗しました: {e}"
93
 
94
  # メタデータの取得
95
- strength, trigger_words = get_metadata_civitai(model_id)
96
 
97
  # メタデータのダウンロード
98
  metadata_filename = "metadata.json"
 
4
  import requests
5
  import gradio as gr
6
  from huggingface_hub import HfApi
 
7
 
8
  # 環境変数から取得
9
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
 
14
  # Hugging Face APIの初期化
15
  api = HfApi()
16
 
 
 
 
17
  def extract_model_id(url):
18
  """
19
  CivitaiのダウンロードURLからモデルIDを抽出する関数。
 
26
  else:
27
  return None
28
 
29
+ def get_metadata(model_id):
30
  """
31
+ Civitai APIからモデルのトリガーワードと推奨強度を取得する関数。
32
  """
33
  if not model_id:
34
  return "N/A", "N/A"
35
+
36
+ api_url = f"https://civitai.com/api/v1/models/{model_id}"
37
+ headers = {}
38
+ if CIVITAI_TOKEN:
39
+ headers["Authorization"] = f"Bearer {CIVITAI_TOKEN}"
40
 
41
  try:
42
+ response = requests.get(api_url, headers=headers)
43
+ response.raise_for_status()
44
+ data = response.json()
45
+
46
+ # デバッグ: レスポンス全体を確認
47
+ print("Civitai API Response:")
48
+ print(json.dumps(data, indent=4, ensure_ascii=False))
49
 
50
  # トリガーワードの取得
51
+ # APIレスポンスのフィールド名を確認
52
+ # ここでは 'trainedWords' と仮定していますが、実際のレスポンスに合わせて修正してください
53
+ trigger_words = data.get('trainedWords', 'N/A')
54
 
55
  # 推奨強度の取得
56
+ description = data.get('description', '')
57
+ # 正規表現を柔軟にして、様々な記述形式に対応
58
+ strength_match = re.search(r'strength[:=]?\s*([\d.]+)', description, re.IGNORECASE)
59
  if strength_match:
60
  strength = float(strength_match.group(1))
61
  else:
 
75
  if not model_id:
76
  return "URLからモデルIDを抽出できませんでした。URL形式を確認してください。"
77
 
78
+ headers = {}
79
+ if CIVITAI_TOKEN:
80
+ headers["Authorization"] = f"Bearer {CIVITAI_TOKEN}"
81
+
82
  try:
83
  # ファイルのダウンロード
84
+ response = requests.get(url, headers=headers)
85
  response.raise_for_status()
86
  except Exception as e:
87
  return f"ファイルのダウンロードに失敗しました: {e}"
 
105
  return f"ファイルの保存に失敗しました: {e}"
106
 
107
  # メタデータの取得
108
+ strength, trigger_words = get_metadata(model_id)
109
 
110
  # メタデータのダウンロード
111
  metadata_filename = "metadata.json"