|
|
|
""" |
|
現在のプロジェクトをHugging Face Docker Spaceに準備するスクリプト |
|
""" |
|
|
|
import os |
|
import shutil |
|
import subprocess |
|
|
|
def create_dockerfile_for_current_project(): |
|
""" |
|
現在のプロジェクト用のDockerfileを作成 |
|
""" |
|
|
|
dockerfile_content = """FROM python:3.9-slim |
|
|
|
WORKDIR /app |
|
|
|
# システムの依存関係をインストール |
|
RUN apt-get update && apt-get install -y \\ |
|
gcc \\ |
|
&& rm -rf /var/lib/apt/lists/* |
|
|
|
# 要件をコピーしてインストール |
|
COPY requirements.txt . |
|
RUN pip install --no-cache-dir -r requirements.txt |
|
|
|
# アプリケーションファイルをコピー |
|
COPY . . |
|
|
|
# ポートを公開 (Hugging Face Spacesは7860を使用) |
|
EXPOSE 7860 |
|
|
|
# 環境変数を設定 |
|
ENV PORT=7860 |
|
|
|
# アプリケーションを起動 |
|
CMD ["python", "app.py"] |
|
""" |
|
|
|
with open("Dockerfile", "w", encoding="utf-8") as f: |
|
f.write(dockerfile_content) |
|
|
|
print("✅ Dockerfile created") |
|
|
|
def create_requirements_txt(): |
|
""" |
|
requirements.txtを作成または更新 |
|
""" |
|
|
|
requirements = [ |
|
"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", |
|
"sqlalchemy==2.0.23", |
|
"python-jose[cryptography]==3.3.0", |
|
"passlib[bcrypt]==1.7.4", |
|
"python-dotenv==1.0.0", |
|
"jinja2==3.1.2", |
|
"aiofiles==23.2.1" |
|
] |
|
|
|
with open("requirements.txt", "w", encoding="utf-8") as f: |
|
for req in requirements: |
|
f.write(f"{req}\\n") |
|
|
|
print("✅ requirements.txt created") |
|
|
|
def create_hf_readme(): |
|
""" |
|
Hugging Face Spaces用のREADME.mdを作成 |
|
""" |
|
|
|
readme_content = """--- |
|
title: FastAPI Django Main Live |
|
emoji: 🚀 |
|
colorFrom: blue |
|
colorTo: red |
|
sdk: docker |
|
pinned: false |
|
license: mit |
|
--- |
|
|
|
# FastAPI Django Main Live |
|
|
|
高性能なFastAPI + Django統合アプリケーション |
|
|
|
## 🚀 概要 |
|
|
|
このスペースは、FastAPIとDjangoを統合したライブアプリケーションです。 |
|
|
|
## ✨ 機能 |
|
|
|
- **FastAPI** - 高性能なPython Webフレームワーク |
|
- **Django** - 成熟したWebアプリケーションフレームワーク |
|
- **Docker** - コンテナ化されたデプロイメント |
|
- **自動デプロイ** - Hugging Face Spacesでの自動デプロイ |
|
|
|
## 🛠️ 使用技術 |
|
|
|
- Python 3.9+ |
|
- FastAPI |
|
- Django |
|
- Docker |
|
- Uvicorn |
|
|
|
## 📋 API エンドポイント |
|
|
|
- `GET /` - ヘルスチェック |
|
- `GET /health` - システムステータス |
|
- `GET /docs` - API ドキュメント (Swagger UI) |
|
|
|
## 🔧 ローカル開発 |
|
|
|
```bash |
|
# 依存関係のインストール |
|
pip install -r requirements.txt |
|
|
|
# アプリケーションの起動 |
|
python app.py |
|
``` |
|
|
|
## 🐳 Docker |
|
|
|
```bash |
|
# イメージのビルド |
|
docker build -t fastapi-django-app . |
|
|
|
# コンテナの実行 |
|
docker run -p 7860:7860 fastapi-django-app |
|
``` |
|
|
|
## 📝 ライセンス |
|
|
|
MIT License |
|
|
|
## 👤 作成者 |
|
|
|
kenken999 |
|
""" |
|
|
|
with open("README.md", "w", encoding="utf-8") as f: |
|
f.write(readme_content) |
|
|
|
print("✅ README.md created") |
|
|
|
def create_gitignore(): |
|
""" |
|
.gitignoreファイルを作成 |
|
""" |
|
|
|
gitignore_content = """# Python |
|
__pycache__/ |
|
*.py[cod] |
|
*$py.class |
|
*.so |
|
.Python |
|
build/ |
|
develop-eggs/ |
|
dist/ |
|
downloads/ |
|
eggs/ |
|
.eggs/ |
|
lib/ |
|
lib64/ |
|
parts/ |
|
sdist/ |
|
var/ |
|
wheels/ |
|
*.egg-info/ |
|
.installed.cfg |
|
*.egg |
|
|
|
# Django |
|
*.log |
|
local_settings.py |
|
db.sqlite3 |
|
media/ |
|
|
|
# Environment |
|
.env |
|
.venv |
|
env/ |
|
venv/ |
|
ENV/ |
|
env.bak/ |
|
venv.bak/ |
|
|
|
# IDE |
|
.vscode/ |
|
.idea/ |
|
*.swp |
|
*.swo |
|
*~ |
|
|
|
# OS |
|
.DS_Store |
|
Thumbs.db |
|
|
|
# Docker |
|
.dockerignore |
|
""" |
|
|
|
with open(".gitignore", "w", encoding="utf-8") as f: |
|
f.write(gitignore_content) |
|
|
|
print("✅ .gitignore created") |
|
|
|
def main(): |
|
"""メイン関数""" |
|
|
|
print("🚀 Hugging Face Docker Space用のファイルを準備中...") |
|
|
|
|
|
create_dockerfile_for_current_project() |
|
create_requirements_txt() |
|
create_hf_readme() |
|
create_gitignore() |
|
|
|
print(""" |
|
🎉 プロジェクトの準備完了! |
|
|
|
📋 次の手順: |
|
1. app.py を確認・編集 |
|
2. git add . |
|
3. git commit -m "Prepare for Hugging Face Docker Space" |
|
4. git push |
|
|
|
🌐 プッシュ後、Hugging Face Spacesで自動的にビルドされます。 |
|
""") |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|