SongGeneration / app.py
angeldove's picture
🎨 Redesign from AnyCoder
66d27eb verified
raw
history blame
6.36 kB
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
import uvicorn
import os
from datetime import datetime
import json
from typing import Dict, List
app = FastAPI(title="Docker Demo App", version="1.0.0")
# Sample data store (in-memory)
data_store: Dict[str, Dict] = {}
@app.get("/", response_class=HTMLResponse)
async def root():
"""Main page with the anycoder attribution"""
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>Docker App on HuggingFace</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
min-height: 100vh;
}
.container {
background: rgba(255, 255, 255, 0.1);
padding: 30px;
border-radius: 10px;
backdrop-filter: blur(10px);
}
h1 {
text-align: center;
margin-bottom: 30px;
}
.endpoint {
background: rgba(255, 255, 255, 0.2);
padding: 15px;
margin: 10px 0;
border-radius: 5px;
}
.method {
display: inline-block;
padding: 3px 8px;
border-radius: 3px;
font-weight: bold;
margin-right: 10px;
}
.get { background: #61affe; }
.post { background: #49cc90; }
.delete { background: #f93e3e; }
a {
color: #ffd700;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.footer {
margin-top: 40px;
text-align: center;
font-size: 14px;
}
</style>
</head>
<body>
<div class="container">
<h1>🐳 Docker Application on HuggingFace Spaces</h1>
<p>Welcome! This is a Docker-based FastAPI application running on HuggingFace Spaces.</p>
<h2>Available Endpoints:</h2>
<div class="endpoint">
<span class="method get">GET</span>
<strong>/health</strong> - Health check endpoint
</div>
<div class="endpoint">
<span class="method get">GET</span>
<strong>/items</strong> - List all items
</div>
<div class="endpoint">
<span class="method post">POST</span>
<strong>/items</strong> - Create a new item
</div>
<div class="endpoint">
<span class="method get">GET</span>
<strong>/items/{item_id}</strong> - Get a specific item
</div>
<div class="endpoint">
<span class="method delete">DELETE</span>
<strong>/items/{item_id}</strong> - Delete an item
</div>
<div class="endpoint">
<span class="method get">GET</span>
<strong>/info</strong> - System information
</div>
<div class="endpoint">
<span class="method get">GET</span>
<strong>/docs</strong> - Interactive API documentation
</div>
<div class="footer">
<p>Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">anycoder</a></p>
<p>Deployed with Docker on HuggingFace Spaces</p>
</div>
</div>
</body>
</html>
"""
return html_content
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {
"status": "healthy",
"timestamp": datetime.utcnow().isoformat(),
"version": "1.0.0"
}
@app.get("/items", response_model=List[Dict])
async def list_items():
"""List all items"""
return list(data_store.values())
@app.post("/items")
async def create_item(item: Dict):
"""Create a new item"""
if "id" not in item:
raise HTTPException(status_code=400, detail="Item must have an 'id' field")
item_id = str(item["id"])
if item_id in data_store:
raise HTTPException(status_code=409, detail="Item with this ID already exists")
item["created_at"] = datetime.utcnow().isoformat()
data_store[item_id] = item
return {"message": "Item created successfully", "item": item}
@app.get("/items/{item_id}")
async def get_item(item_id: str):
"""Get a specific item"""
if item_id not in data_store:
raise HTTPException(status_code=404, detail="Item not found")
return data_store[item_id]
@app.delete("/items/{item_id}")
async def delete_item(item_id: str):
"""Delete an item"""
if item_id not in data_store:
raise HTTPException(status_code=404, detail="Item not found")
deleted_item = data_store.pop(item_id)
return {"message": "Item deleted successfully", "item": deleted_item}
@app.get("/info")
async def system_info():
"""Get system information"""
return {
"app_name": "Docker Demo App",
"environment": os.getenv("ENVIRONMENT", "development"),
"python_version": os.sys.version,
"docker": True,
"platform": os.name,
"total_items": len(data_store)
}
# Add some sample data on startup
@app.on_event("startup")
async def startup_event():
"""Initialize with sample data"""
sample_items = [
{"id": 1, "name": "Sample Item 1", "description": "This is a sample item"},
{"id": 2, "name": "Sample Item 2", "description": "Another sample item"},
{"id": 3, "name": "Docker Demo", "description": "Running in Docker container"}
]
for item in sample_items:
item_id = str(item["id"])
item["created_at"] = datetime.utcnow().isoformat()
data_store[item_id] = item
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)