Commit ·
8d3c364
1
Parent(s): fa8d2d9
initial commit
Browse files- .gitignore +2 -0
- Dockerfile +15 -0
- app.py +38 -0
- requirements.txt +3 -0
- static/index.html +26 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__
|
| 2 |
+
.env
|
Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
USER user
|
| 5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
COPY --chown=user requirements.txt .
|
| 10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
+
|
| 12 |
+
EXPOSE 7860
|
| 13 |
+
|
| 14 |
+
COPY --chown=user app.py .
|
| 15 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import base64
|
| 3 |
+
import io
|
| 4 |
+
# import torch
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from fastapi import FastAPI
|
| 7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
from fastapi.responses import JSONResponse
|
| 9 |
+
from fastapi.staticfiles import StaticFiles
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
app.add_middleware(
|
| 14 |
+
CORSMiddleware,
|
| 15 |
+
allow_origins=["*"],
|
| 16 |
+
allow_credentials=True,
|
| 17 |
+
allow_methods=["*"],
|
| 18 |
+
allow_headers=["*"],
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
app.mount('/static', StaticFiles(directory='static'), name='static')
|
| 22 |
+
|
| 23 |
+
# load model
|
| 24 |
+
# send to device
|
| 25 |
+
|
| 26 |
+
@app.get('/generate') # rate limit this
|
| 27 |
+
async def generate():
|
| 28 |
+
# create random latent vector
|
| 29 |
+
# forward latent vector to generator
|
| 30 |
+
# convert result tensor into image with ToPILImage()
|
| 31 |
+
# return image
|
| 32 |
+
arr = np.random.randint(0, 256, (256, 256, 3)).astype(np.uint8)
|
| 33 |
+
pil_img = Image.fromarray(arr)
|
| 34 |
+
buffer = io.BytesIO()
|
| 35 |
+
pil_img.save(buffer, format='PNG')
|
| 36 |
+
buffer.seek(0)
|
| 37 |
+
img_str = base64.b64encode(buffer.read()).decode('utf-8')
|
| 38 |
+
return JSONResponse({ 'image': img_str })
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
torch
|
static/index.html
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>kelompok cina visioner</title>
|
| 7 |
+
</head>
|
| 8 |
+
<body>
|
| 9 |
+
<div style="display: flex; flex-direction: column; justify-content: center; align-items: center; box-sizing: border-box; height: 100vh; margin: 0;">
|
| 10 |
+
<h1>kelompok cina visioner :sparkle:</h1>
|
| 11 |
+
<img id="preview" style="width: 256px; height: 256px; border: 1px solid; border-radius: 4px;">
|
| 12 |
+
<button id="generate" onclick="generate()">Generate</button>
|
| 13 |
+
<button id="save">Save</button>
|
| 14 |
+
</div>
|
| 15 |
+
|
| 16 |
+
<script>
|
| 17 |
+
async function generate() {
|
| 18 |
+
const response = await fetch("/generate", {
|
| 19 |
+
"method": "GET"
|
| 20 |
+
});
|
| 21 |
+
const data = await response.json();
|
| 22 |
+
document.getElementById("preview").src = 'data:image/png;base64,' + data.image;
|
| 23 |
+
}
|
| 24 |
+
</script>
|
| 25 |
+
</body>
|
| 26 |
+
</html>
|