Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -26,6 +26,23 @@ import PIL.Image
|
|
26 |
from PIL import Image
|
27 |
from io import BytesIO
|
28 |
import torchvision.transforms as T
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
app = FastAPI()
|
31 |
|
@@ -64,32 +81,6 @@ def add_margin(pil_img, top, right, bottom, left, color):
|
|
64 |
result.paste(pil_img, (left, top))
|
65 |
return result
|
66 |
|
67 |
-
from fastapi import HTTPException
|
68 |
-
from httpx import AsyncClient
|
69 |
-
|
70 |
-
MODEL_URL = "https://www.dropbox.com/s/04suaimdpru76h3/ArtLine_920.pkl?dl=1"
|
71 |
-
|
72 |
-
async def download_model(url: str, filename: str):
|
73 |
-
async with AsyncClient() as client:
|
74 |
-
response = await client.get(url)
|
75 |
-
if response.status_code == 200:
|
76 |
-
with open(filename, "wb") as f:
|
77 |
-
f.write(response.content)
|
78 |
-
else:
|
79 |
-
raise HTTPException(status_code=response.status_code, detail="Failed to download model")
|
80 |
-
|
81 |
-
# Define the function to download the model asynchronously
|
82 |
-
async def setup_model():
|
83 |
-
await download_model(MODEL_URL, "ArtLine_920.pkl")
|
84 |
-
|
85 |
-
# Run the setup function to download the model before the FastAPI app starts
|
86 |
-
import asyncio
|
87 |
-
loop = asyncio.get_event_loop()
|
88 |
-
loop.run_until_complete(setup_model())
|
89 |
-
|
90 |
-
# Now load the learner once the model is downloaded
|
91 |
-
path = Path(".")
|
92 |
-
learn = load_learner(path, 'ArtLine_920.pkl')
|
93 |
|
94 |
|
95 |
|
|
|
26 |
from PIL import Image
|
27 |
from io import BytesIO
|
28 |
import torchvision.transforms as T
|
29 |
+
import requests
|
30 |
+
from pathlib import Path
|
31 |
+
|
32 |
+
MODEL_URL = "https://www.dropbox.com/s/04suaimdpru76h3/ArtLine_920.pkl?dl=1"
|
33 |
+
MODEL_PATH = "ArtLine_920.pkl"
|
34 |
+
|
35 |
+
# Download the model file
|
36 |
+
response = requests.get(MODEL_URL)
|
37 |
+
if response.status_code == 200:
|
38 |
+
with open(MODEL_PATH, 'wb') as f:
|
39 |
+
f.write(response.content)
|
40 |
+
else:
|
41 |
+
print("Failed to download the model")
|
42 |
+
|
43 |
+
# Load the model using FastAI
|
44 |
+
path = Path(".")
|
45 |
+
learn = load_learner(path, MODEL_PATH)
|
46 |
|
47 |
app = FastAPI()
|
48 |
|
|
|
81 |
result.paste(pil_img, (left, top))
|
82 |
return result
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
|
86 |
|