Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +19 -0
- app.py +51 -0
- generator_final.h5 +3 -0
- requirements.txt +11 -0
Dockerfile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
USER user
|
| 5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 6 |
+
ENV MODEL_PATH=/app/generator_final.h5
|
| 7 |
+
|
| 8 |
+
WORKDIR /app
|
| 9 |
+
|
| 10 |
+
COPY --chown=user requirements.txt .
|
| 11 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Copy model and app files
|
| 14 |
+
COPY --chown=user generator_final.h5 .
|
| 15 |
+
COPY --chown=user app.py .
|
| 16 |
+
|
| 17 |
+
EXPOSE 7860
|
| 18 |
+
|
| 19 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.responses import JSONResponse, StreamingResponse
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
app = FastAPI(title="GAN Image Generator API")
|
| 10 |
+
|
| 11 |
+
# Load model
|
| 12 |
+
model_path = os.getenv("MODEL_PATH", "generator_final.h5")
|
| 13 |
+
generator = load_model(model_path)
|
| 14 |
+
|
| 15 |
+
def generate_image(noise_dim=100):
|
| 16 |
+
"""Generate image from random noise"""
|
| 17 |
+
z = np.random.randn(1, noise_dim, 1, 1).astype(np.float32)
|
| 18 |
+
fake_image = generator.predict(z)
|
| 19 |
+
fake_image = (fake_image.squeeze() * 255).astype(np.uint8)
|
| 20 |
+
return fake_image[..., :3]
|
| 21 |
+
|
| 22 |
+
@app.get("/")
|
| 23 |
+
def read_root():
|
| 24 |
+
return {"message": "GAN Image Generator API"}
|
| 25 |
+
|
| 26 |
+
@app.get("/generate-random")
|
| 27 |
+
async def generate_random_image():
|
| 28 |
+
"""Endpoint to generate random image"""
|
| 29 |
+
image_array = generate_image()
|
| 30 |
+
img = Image.fromarray(image_array)
|
| 31 |
+
|
| 32 |
+
# Convert to bytes
|
| 33 |
+
img_byte_arr = io.BytesIO()
|
| 34 |
+
img.save(img_byte_arr, format='PNG')
|
| 35 |
+
img_byte_arr.seek(0)
|
| 36 |
+
|
| 37 |
+
return StreamingResponse(img_byte_arr, media_type="image/png")
|
| 38 |
+
|
| 39 |
+
@app.post("/generate-from-sketch")
|
| 40 |
+
async def generate_from_sketch(file: UploadFile = File(...)):
|
| 41 |
+
"""Endpoint to generate from sketch"""
|
| 42 |
+
# Process your sketch here (add your sketch processing logic)
|
| 43 |
+
# For now just returns a random image
|
| 44 |
+
image_array = generate_image()
|
| 45 |
+
img = Image.fromarray(image_array)
|
| 46 |
+
|
| 47 |
+
img_byte_arr = io.BytesIO()
|
| 48 |
+
img.save(img_byte_arr, format='PNG')
|
| 49 |
+
img_byte_arr.seek(0)
|
| 50 |
+
|
| 51 |
+
return StreamingResponse(img_byte_arr, media_type="image/png")
|
generator_final.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3536d9341014426b6488e885ac4574b96049663ed949b63298c46b7a7cce87b8
|
| 3 |
+
size 217820864
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.0.0
|
| 2 |
+
torchvision>=0.15.0
|
| 3 |
+
gradio>=4.0.0
|
| 4 |
+
numpy>=1.24.0
|
| 5 |
+
tensorflow>=2.10
|
| 6 |
+
fastapi
|
| 7 |
+
uvicorn[standard]
|
| 8 |
+
tensorflow
|
| 9 |
+
pillow
|
| 10 |
+
numpy
|
| 11 |
+
python-multipart
|