Ashrafb commited on
Commit
1e19492
1 Parent(s): d56b8e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -4
app.py CHANGED
@@ -64,11 +64,33 @@ def add_margin(pil_img, top, right, bottom, left, color):
64
  result.paste(pil_img, (left, top))
65
  return result
66
 
67
- MODEL_URL = "https://www.dropbox.com/s/04suaimdpru76h3/ArtLine_920.pkl?dl=1 "
68
- urllib.request.urlretrieve(MODEL_URL, "ArtLine_920.pkl")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  path = Path(".")
70
- print(os.listdir('.'))
71
- learn=load_learner(path, 'ArtLine_920.pkl')
72
 
73
 
74
 
 
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
 
96