miyataken999
Prepare FastAPI app for Hugging Face Spaces
1e13199
#!/usr/bin/env python3
"""
FastAPI + Django 統合アプリケーション for Hugging Face Spaces
"""
import os
import sys
from pathlib import Path
from dotenv import load_dotenv
import uvicorn
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
# 環境変数読み込み
load_dotenv()
# プロジェクトルートをパスに追加
project_root = Path(__file__).parent
sys.path.append(str(project_root))
# FastAPIアプリケーションの作成
app = FastAPI(
title="FastAPI Django Main Live",
description="高性能なFastAPI + Django統合アプリケーション",
version="1.0.0",
)
# CORS設定
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ルートエンドポイント
@app.get("/", response_class=HTMLResponse)
async def read_root():
return """
<html>
<head>
<title>FastAPI Django Main Live</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.container {
max-width: 800px;
margin: 0 auto;
text-align: center;
}
.card {
background: rgba(255, 255, 255, 0.1);
border-radius: 10px;
padding: 20px;
margin: 20px 0;
backdrop-filter: blur(10px);
}
.button {
display: inline-block;
padding: 10px 20px;
margin: 10px;
background: #4CAF50;
color: white;
text-decoration: none;
border-radius: 5px;
transition: background 0.3s;
}
.button:hover {
background: #45a049;
}
.api-button {
background: #2196F3;
}
.api-button:hover {
background: #1976D2;
}
</style>
</head>
<body>
<div class="container">
<h1>🚀 FastAPI Django Main Live</h1>
<div class="card">
<h2>高性能なWeb アプリケーション</h2>
<p>FastAPI + Django統合アプリケーションがHugging Face Spacesで稼働中です!</p>
</div>
<div class="card">
<h3>🔗 利用可能なエンドポイント</h3>
<a href="/docs" class="button api-button">📚 API ドキュメント</a>
<a href="/health" class="button">💚 ヘルスチェック</a>
<a href="/status" class="button">📊 ステータス</a>
</div>
<div class="card">
<h3>🛠️ 技術スタック</h3>
<ul style="list-style: none; padding: 0;">
<li>🐍 Python 3.9+</li>
<li>⚡ FastAPI</li>
<li>🎯 Django</li>
<li>🐳 Docker</li>
<li>☁️ Hugging Face Spaces</li>
</ul>
</div>
</div>
</body>
</html>
"""
@app.get("/health")
async def health_check():
"""ヘルスチェックエンドポイント"""
return {
"status": "healthy",
"service": "FastAPI Django Main Live",
"platform": "Hugging Face Spaces",
"docker": True,
"python_version": sys.version,
"environment": os.environ.get("SPACE_ID", "local")
}
@app.get("/status")
async def get_status():
"""アプリケーションステータス"""
return {
"application": "FastAPI Django Main Live",
"version": "1.0.0",
"framework": "FastAPI",
"platform": "Hugging Face Spaces",
"features": [
"REST API",
"Auto Documentation",
"CORS Support",
"Health Monitoring"
],
"endpoints": {
"root": "/",
"health": "/health",
"status": "/status",
"docs": "/docs",
"redoc": "/redoc"
}
}
@app.get("/api/hello")
async def hello_api():
"""シンプルなAPI例"""
return {"message": "Hello from FastAPI!", "success": True}
@app.get("/api/info")
async def app_info():
"""アプリケーション情報"""
return {
"name": "FastAPI Django Main Live",
"description": "高性能なFastAPI + Django統合アプリケーション",
"author": "kenken999",
"deployment": "Hugging Face Spaces",
"container": "Docker",
"python_version": sys.version.split()[0],
"working_directory": str(project_root),
"pid": os.getpid()
}
# サーバー起動
if __name__ == "__main__":
# Hugging Face Spacesの場合はポート7860を使用
port = int(os.environ.get("PORT", 7860))
host = os.environ.get("HOST", "0.0.0.0")
print(f"🚀 FastAPI Django Main Live 起動中...")
print(f"📡 URL: http://{host}:{port}")
print(f"📚 API Docs: http://{host}:{port}/docs")
print(f"💚 Health Check: http://{host}:{port}/health")
uvicorn.run(
app,
host=host,
port=port,
log_level="info",
access_log=True
)