Add image proxy to fix CORS and mixed content issues
Browse files- Dockerfile +23 -2
- ui/index.html +1 -1
Dockerfile
CHANGED
|
@@ -5,7 +5,7 @@ WORKDIR /app
|
|
| 5 |
# Copy requirements
|
| 6 |
COPY requirements.txt .
|
| 7 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
-
RUN pip install fastapi uvicorn python-multipart
|
| 9 |
|
| 10 |
# Copy everything
|
| 11 |
COPY . .
|
|
@@ -24,7 +24,7 @@ from pathlib import Path\n\
|
|
| 24 |
sys.path.append(str(Path(__file__).parent.parent))\n\
|
| 25 |
\n\
|
| 26 |
from fastapi import FastAPI, File, UploadFile, HTTPException\n\
|
| 27 |
-
from fastapi.responses import JSONResponse, FileResponse\n\
|
| 28 |
from fastapi.middleware.cors import CORSMiddleware\n\
|
| 29 |
from PIL import Image\n\
|
| 30 |
import io\n\
|
|
@@ -69,6 +69,27 @@ async def get_training_data():\n\
|
|
| 69 |
data = json.load(f)\n\
|
| 70 |
return {"status": "success", "data": data}\n\
|
| 71 |
\n\
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
@app.post("/predict")\n\
|
| 73 |
async def predict(file: UploadFile = File(...)):\n\
|
| 74 |
contents = await file.read()\n\
|
|
|
|
| 5 |
# Copy requirements
|
| 6 |
COPY requirements.txt .
|
| 7 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
RUN pip install fastapi uvicorn python-multipart requests
|
| 9 |
|
| 10 |
# Copy everything
|
| 11 |
COPY . .
|
|
|
|
| 24 |
sys.path.append(str(Path(__file__).parent.parent))\n\
|
| 25 |
\n\
|
| 26 |
from fastapi import FastAPI, File, UploadFile, HTTPException\n\
|
| 27 |
+
from fastapi.responses import JSONResponse, FileResponse, Response\n\
|
| 28 |
from fastapi.middleware.cors import CORSMiddleware\n\
|
| 29 |
from PIL import Image\n\
|
| 30 |
import io\n\
|
|
|
|
| 69 |
data = json.load(f)\n\
|
| 70 |
return {"status": "success", "data": data}\n\
|
| 71 |
\n\
|
| 72 |
+
@app.get("/proxy_image")\n\
|
| 73 |
+
async def proxy_image(url: str):\n\
|
| 74 |
+
"""Proxy images to avoid CORS and mixed content issues"""\n\
|
| 75 |
+
import requests\n\
|
| 76 |
+
try:\n\
|
| 77 |
+
# Force HTTPS if possible\n\
|
| 78 |
+
if url.startswith("http://"):\n\
|
| 79 |
+
https_url = url.replace("http://", "https://")\n\
|
| 80 |
+
try:\n\
|
| 81 |
+
resp = requests.get(https_url, timeout=5)\n\
|
| 82 |
+
if resp.status_code == 200:\n\
|
| 83 |
+
return Response(content=resp.content, media_type="image/jpeg")\n\
|
| 84 |
+
except:\n\
|
| 85 |
+
pass\n\
|
| 86 |
+
# Fallback to original URL\n\
|
| 87 |
+
resp = requests.get(url, timeout=5)\n\
|
| 88 |
+
return Response(content=resp.content, media_type="image/jpeg")\n\
|
| 89 |
+
except:\n\
|
| 90 |
+
# Return placeholder if image fails\n\
|
| 91 |
+
return Response(status_code=404)\n\
|
| 92 |
+
\n\
|
| 93 |
@app.post("/predict")\n\
|
| 94 |
async def predict(file: UploadFile = File(...)):\n\
|
| 95 |
contents = await file.read()\n\
|
ui/index.html
CHANGED
|
@@ -551,7 +551,7 @@
|
|
| 551 |
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;">
|
| 552 |
${similarImages.map(img => `
|
| 553 |
<div style="text-align: center;">
|
| 554 |
-
<img src="${img.url}"
|
| 555 |
style="width: 100%; max-height: 150px; object-fit: cover; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);"
|
| 556 |
onerror="this.style.display='none'; this.nextElementSibling.style.display='block';"
|
| 557 |
alt="${img.name || 'Training image'}">
|
|
|
|
| 551 |
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;">
|
| 552 |
${similarImages.map(img => `
|
| 553 |
<div style="text-align: center;">
|
| 554 |
+
<img src="${API_URL}/proxy_image?url=${encodeURIComponent(img.url)}"
|
| 555 |
style="width: 100%; max-height: 150px; object-fit: cover; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);"
|
| 556 |
onerror="this.style.display='none'; this.nextElementSibling.style.display='block';"
|
| 557 |
alt="${img.name || 'Training image'}">
|