File size: 4,615 Bytes
1e13199 |
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 |
#!/usr/bin/env python3
"""
Hugging Face Spaces Docker Space Creator
DockerスペースをHugging Face Spacesに作成するスクリプト
"""
import os
from huggingface_hub import HfApi, Repository
import subprocess
import sys
def create_docker_space(space_name, token, description="Docker Space for FastAPI Django App"):
"""
Hugging Face SpacesでDockerスペースを作成
Args:
space_name (str): スペース名
token (str): Hugging Face アクセストークン
description (str): スペースの説明
"""
print(f"🚀 Creating Docker Space: {space_name}")
# HfApi インスタンスを作成
api = HfApi(token=token)
try:
# スペースを作成
space_url = api.create_repo(
repo_id=space_name,
token=token,
repo_type="space",
space_sdk="docker",
private=False
)
print(f"✅ Space created successfully: {space_url}")
# ローカルリポジトリをクローン
repo = Repository(
local_dir=f"./{space_name}",
clone_from=space_url,
token=token
)
print(f"✅ Repository cloned to: ./{space_name}")
# 基本的なDockerfileとREADMEを作成
create_basic_files(f"./{space_name}", space_name, description)
return space_url
except Exception as e:
print(f"❌ Error creating space: {e}")
return None
def create_basic_files(space_dir, space_name, description):
"""
基本的なファイルを作成
"""
# Dockerfile
dockerfile_content = """FROM python:3.9-slim
WORKDIR /app
# 必要な依存関係をインストール
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# アプリケーションファイルをコピー
COPY . .
# ポートを公開
EXPOSE 7860
# アプリケーションを起動
CMD ["python", "app.py"]
"""
with open(f"{space_dir}/Dockerfile", "w", encoding="utf-8") as f:
f.write(dockerfile_content)
# README.md
readme_content = f"""---
title: {space_name}
emoji: 🐳
colorFrom: blue
colorTo: red
sdk: docker
pinned: false
license: mit
---
# {space_name}
{description}
## 概要
このスペースは、FastAPI + Django統合アプリケーションをDockerで実行するためのスペースです。
## 機能
- FastAPI バックエンド
- Django 統合
- Docker コンテナ対応
- 自動デプロイ
## 使用方法
1. 必要な依存関係を`requirements.txt`に追加
2. `app.py`にメインアプリケーションを実装
3. コミット & プッシュで自動デプロイ
## 開発者
Created with ❤️ by kenken999
"""
with open(f"{space_dir}/README.md", "w", encoding="utf-8") as f:
f.write(readme_content)
# requirements.txt
requirements_content = """fastapi==0.104.1
uvicorn==0.24.0
django==4.2.7
pydantic==2.5.0
requests==2.31.0
python-multipart==0.0.6
"""
with open(f"{space_dir}/requirements.txt", "w", encoding="utf-8") as f:
f.write(requirements_content)
# app.py (基本的なFastAPIアプリ)
app_content = """#!/usr/bin/env python3
import os
from fastapi import FastAPI
import uvicorn
app = FastAPI(title="Docker Space App", version="1.0.0")
@app.get("/")
def read_root():
return {"Hello": "World", "message": "Docker Space is running!"}
@app.get("/health")
def health_check():
return {"status": "healthy", "docker": True}
if __name__ == "__main__":
port = int(os.environ.get("PORT", 7860))
uvicorn.run(app, host="0.0.0.0", port=port)
"""
with open(f"{space_dir}/app.py", "w", encoding="utf-8") as f:
f.write(app_content)
print("✅ Basic files created successfully")
def main():
"""メイン関数"""
# 設定
space_name = "fastapi-django-docker-space"
token = input("Hugging Face Access Token を入力してください: ")
if not token:
print("❌ トークンが必要です")
return
# スペースを作成
space_url = create_docker_space(space_name, token)
if space_url:
print(f"""
🎉 Docker Space が正常に作成されました!
📋 次の手順:
1. cd {space_name}
2. 必要に応じてファイルを編集
3. git add .
4. git commit -m "Initial commit"
5. git push
🌐 スペースURL: {space_url}
""")
else:
print("❌ スペースの作成に失敗しました")
if __name__ == "__main__":
main()
|