|
|
|
""" |
|
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}") |
|
|
|
|
|
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}") |
|
|
|
|
|
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_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_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_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_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() |
|
|