Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,71 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File
|
| 2 |
-
from fastapi.
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
import
|
| 7 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from fastapi.responses import StreamingResponse
|
| 4 |
+
import numpy as np
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import io
|
| 8 |
+
import os
|
| 9 |
+
import cv2
|
| 10 |
+
|
| 11 |
+
app = FastAPI(title="GAN Image Generator API")
|
| 12 |
+
|
| 13 |
+
# Add CORS middleware to allow React frontend to connect
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"], # Allows all origins - adjust for production!
|
| 17 |
+
allow_credentials=True,
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Load model
|
| 23 |
+
model_path = os.getenv("MODEL_PATH", "generator_final.h5")
|
| 24 |
+
generator = load_model(model_path)
|
| 25 |
+
|
| 26 |
+
def preprocess_sketch(image_bytes):
|
| 27 |
+
"""Process uploaded sketch image for the GAN"""
|
| 28 |
+
try:
|
| 29 |
+
# Convert bytes to PIL Image
|
| 30 |
+
img = Image.open(io.BytesIO(image_bytes)).convert('RGB')
|
| 31 |
+
img = np.array(img)
|
| 32 |
+
|
| 33 |
+
# Convert to grayscale
|
| 34 |
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 35 |
+
|
| 36 |
+
# Resize to model's expected input size (adjust dimensions as needed)
|
| 37 |
+
resized = cv2.resize(gray, (256, 256))
|
| 38 |
+
|
| 39 |
+
# Normalize pixel values to [-1, 1] range
|
| 40 |
+
normalized = (resized.astype(np.float32) / 127.5) - 1.0
|
| 41 |
+
|
| 42 |
+
# Add batch and channel dimensions
|
| 43 |
+
processed = np.expand_dims(normalized, axis=[0, -1])
|
| 44 |
+
|
| 45 |
+
return processed
|
| 46 |
+
except Exception as e:
|
| 47 |
+
raise ValueError(f"Image processing failed: {str(e)}")
|
| 48 |
+
|
| 49 |
+
@app.post("/generate-from-sketch")
|
| 50 |
+
async def generate_from_sketch(file: UploadFile = File(...)):
|
| 51 |
+
try:
|
| 52 |
+
# Read uploaded file
|
| 53 |
+
contents = await file.read()
|
| 54 |
+
|
| 55 |
+
# Process sketch
|
| 56 |
+
processed_sketch = preprocess_sketch(contents)
|
| 57 |
+
|
| 58 |
+
# Generate image (modify this to use your actual GAN prediction)
|
| 59 |
+
generated = generator.predict(processed_sketch)
|
| 60 |
+
generated = (generated.squeeze() * 255).astype(np.uint8)
|
| 61 |
+
|
| 62 |
+
# Convert to bytes
|
| 63 |
+
img = Image.fromarray(generated)
|
| 64 |
+
img_byte_arr = io.BytesIO()
|
| 65 |
+
img.save(img_byte_arr, format='PNG')
|
| 66 |
+
img_byte_arr.seek(0)
|
| 67 |
+
|
| 68 |
+
return StreamingResponse(img_byte_arr, media_type="image/png")
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
raise HTTPException(status_code=400, detail=str(e))
|