Spaces:
Running
on
L40S
Running
on
L40S
File size: 6,360 Bytes
66d27eb 3ef9463 66d27eb 989bff9 66d27eb 989bff9 66d27eb 0a1e140 66d27eb 258fd02 66d27eb 258fd02 66d27eb 258fd02 d658154 66d27eb 258fd02 66d27eb d658154 258fd02 66d27eb |
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 195 196 197 |
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) |