File size: 8,416 Bytes
7b7bdab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
#!/usr/bin/env python3
"""
Hugging Face リポジトリ内容取得クライアント
"""
import os
import requests
from huggingface_hub import HfApi, Repository, list_repo_files
from huggingface_hub import hf_hub_download, snapshot_download
from typing import List, Dict, Optional
import json
from datetime import datetime
class HuggingFaceRepoClient:
"""Hugging Face リポジトリの内容を取得するクライアント"""
def __init__(self, token: Optional[str] = None):
"""
初期化
Args:
token: Hugging Face API トークン(環境変数 HF_TOKEN から取得)
"""
self.token = token or os.environ.get("HF_TOKEN")
self.api = HfApi(token=self.token)
def get_repo_info(self, repo_id: str, repo_type: str = "space") -> Dict:
"""
リポジトリの基本情報を取得
Args:
repo_id: リポジトリID (例: "kenken999/fastapi_django_main_live")
repo_type: リポジトリタイプ ("space", "model", "dataset")
Returns:
リポジトリ情報の辞書
"""
try:
if repo_type == "space":
repo_info = self.api.space_info(repo_id)
elif repo_type == "model":
repo_info = self.api.model_info(repo_id)
elif repo_type == "dataset":
repo_info = self.api.dataset_info(repo_id)
else:
raise ValueError("repo_type must be 'space', 'model', or 'dataset'")
return {
"id": repo_info.id,
"author": repo_info.author,
"sha": getattr(repo_info, 'sha', 'N/A'),
"created_at": str(repo_info.created_at) if hasattr(repo_info, 'created_at') else 'N/A',
"last_modified": str(repo_info.last_modified) if hasattr(repo_info, 'last_modified') else 'N/A',
"private": getattr(repo_info, 'private', False),
"tags": getattr(repo_info, 'tags', []),
"siblings": [f.rfilename for f in getattr(repo_info, 'siblings', [])],
"downloads": getattr(repo_info, 'downloads', 0),
"likes": getattr(repo_info, 'likes', 0),
}
except Exception as e:
return {"error": str(e)}
def list_files(self, repo_id: str, repo_type: str = "space") -> List[str]:
"""
リポジトリ内のファイル一覧を取得
Args:
repo_id: リポジトリID
repo_type: リポジトリタイプ
Returns:
ファイルパスのリスト
"""
try:
files = list_repo_files(repo_id, repo_type=repo_type, token=self.token)
return list(files)
except Exception as e:
print(f"ファイル一覧取得エラー: {e}")
return []
def download_file(self, repo_id: str, filename: str, repo_type: str = "space") -> Optional[str]:
"""
リポジトリから特定のファイルをダウンロード
Args:
repo_id: リポジトリID
filename: ダウンロードするファイル名
repo_type: リポジトリタイプ
Returns:
ダウンロードしたファイルのローカルパス
"""
try:
file_path = hf_hub_download(
repo_id=repo_id,
filename=filename,
repo_type=repo_type,
token=self.token
)
return file_path
except Exception as e:
print(f"ファイルダウンロードエラー: {e}")
return None
def read_file_content(self, repo_id: str, filename: str, repo_type: str = "space") -> Optional[str]:
"""
リポジトリから特定のファイルの内容を読み取り
Args:
repo_id: リポジトリID
filename: 読み取るファイル名
repo_type: リポジトリタイプ
Returns:
ファイルの内容(テキスト)
"""
try:
# ファイルをダウンロード
file_path = self.download_file(repo_id, filename, repo_type)
if file_path:
with open(file_path, 'r', encoding='utf-8') as f:
return f.read()
return None
except Exception as e:
print(f"ファイル内容読み取りエラー: {e}")
return None
def clone_repo(self, repo_id: str, local_dir: str, repo_type: str = "space") -> bool:
"""
リポジトリ全体をローカルにクローン
Args:
repo_id: リポジトリID
local_dir: ローカルディレクトリパス
repo_type: リポジトリタイプ
Returns:
成功したかどうか
"""
try:
snapshot_download(
repo_id=repo_id,
local_dir=local_dir,
repo_type=repo_type,
token=self.token
)
return True
except Exception as e:
print(f"リポジトリクローンエラー: {e}")
return False
def get_commit_history(self, repo_id: str, repo_type: str = "space") -> List[Dict]:
"""
リポジトリのコミット履歴を取得
Args:
repo_id: リポジトリID
repo_type: リポジトリタイプ
Returns:
コミット履歴のリスト
"""
try:
commits = self.api.list_repo_commits(repo_id, repo_type=repo_type)
return [
{
"commit_id": commit.commit_id,
"title": commit.title,
"message": getattr(commit, 'message', ''),
"date": str(commit.date) if hasattr(commit, 'date') else 'N/A',
"author": getattr(commit, 'author', 'Unknown'),
}
for commit in commits[:10] # 最新10件
]
except Exception as e:
print(f"コミット履歴取得エラー: {e}")
return []
def main():
"""メイン関数 - 使用例"""
# 現在のHugging Face Spacesリポジトリの情報を取得
client = HuggingFaceRepoClient()
repo_id = "kenken999/fastapi_django_main_live"
print("🚀 Hugging Face リポジトリ情報取得開始")
print(f"📂 対象リポジトリ: {repo_id}")
print("-" * 50)
# 1. リポジトリ基本情報
print("📋 基本情報:")
repo_info = client.get_repo_info(repo_id, "space")
if "error" not in repo_info:
for key, value in repo_info.items():
print(f" {key}: {value}")
else:
print(f" エラー: {repo_info['error']}")
print("\n" + "-" * 50)
# 2. ファイル一覧
print("📁 ファイル一覧:")
files = client.list_files(repo_id, "space")
if files:
for i, file in enumerate(files[:10]): # 最初の10ファイル
print(f" {i+1:2d}. {file}")
if len(files) > 10:
print(f" ... and {len(files) - 10} more files")
else:
print(" ファイルが見つかりません")
print("\n" + "-" * 50)
# 3. 特定ファイルの内容読み取り
print("📄 README.md の内容:")
readme_content = client.read_file_content(repo_id, "README.md", "space")
if readme_content:
# 最初の500文字を表示
print(readme_content[:500] + "..." if len(readme_content) > 500 else readme_content)
else:
print(" README.md が見つかりません")
print("\n" + "-" * 50)
# 4. コミット履歴
print("📜 最新コミット履歴:")
commits = client.get_commit_history(repo_id, "space")
if commits:
for i, commit in enumerate(commits[:5]): # 最新5件
print(f" {i+1}. {commit['title']} ({commit['date'][:10]})")
else:
print(" コミット履歴が取得できません")
print("\n" + "=" * 50)
print("✅ 取得完了")
if __name__ == "__main__":
main()
|