Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Commit
·
37e8687
1
Parent(s):
0754334
try new version of API
Browse files
main.py
CHANGED
@@ -14,25 +14,7 @@ import torch
|
|
14 |
import logging
|
15 |
from typing import List
|
16 |
import httpx
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
def process_single_image(image_url, model):
|
21 |
-
try:
|
22 |
-
response = requests.get(image_url)
|
23 |
-
image = Image.open(BytesIO(response.content))
|
24 |
-
processed_image = process_image(image, size=image_size)
|
25 |
-
image_tensor = transforms.ToTensor()(processed_image).unsqueeze(0)
|
26 |
-
|
27 |
-
with torch.no_grad():
|
28 |
-
outputs = model(image_tensor)
|
29 |
-
probabilities = torch.nn.functional.softmax(outputs, dim=1)
|
30 |
-
predicted_probabilities = probabilities.numpy().tolist()
|
31 |
-
confidence = round(predicted_probabilities[0][1], 2)
|
32 |
-
return {"imageUrl": image_url, "confidence": confidence}
|
33 |
-
except Exception as e:
|
34 |
-
return {"imageUrl": image_url, "error": str(e)}
|
35 |
-
|
36 |
|
37 |
app = FastAPI()
|
38 |
|
@@ -72,6 +54,8 @@ class PredictRequest(BaseModel):
|
|
72 |
modelName: str
|
73 |
|
74 |
|
|
|
|
|
75 |
# Dictionnaire pour stocker les pipelines de modèles
|
76 |
model_pipelines = {}
|
77 |
|
@@ -144,61 +128,86 @@ class BatchPredictRequest(BaseModel):
|
|
144 |
modelName: str
|
145 |
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
|
152 |
-
#
|
153 |
-
|
154 |
-
|
155 |
|
156 |
-
|
157 |
|
158 |
-
#
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
|
168 |
-
#
|
169 |
-
|
170 |
|
171 |
-
#
|
172 |
-
|
173 |
|
174 |
-
#
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
|
181 |
-
|
182 |
|
183 |
-
#
|
184 |
-
|
185 |
|
186 |
|
187 |
@app.post("/batch_predict")
|
188 |
async def batch_predict(request: BatchPredictRequest):
|
189 |
model_name = request.modelName
|
|
|
|
|
190 |
if model_name not in model_pipelines:
|
191 |
raise HTTPException(status_code=404, detail="Model not found")
|
192 |
|
193 |
model = model_pipelines[model_name]
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
return JSONResponse(content={"results": results})
|
|
|
14 |
import logging
|
15 |
from typing import List
|
16 |
import httpx
|
17 |
+
import asyncio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
app = FastAPI()
|
20 |
|
|
|
54 |
modelName: str
|
55 |
|
56 |
|
57 |
+
torch.set_num_threads(6)
|
58 |
+
|
59 |
# Dictionnaire pour stocker les pipelines de modèles
|
60 |
model_pipelines = {}
|
61 |
|
|
|
128 |
modelName: str
|
129 |
|
130 |
|
131 |
+
@app.post("/batch_predict")
|
132 |
+
async def batch_predict(request: BatchPredictRequest):
|
133 |
+
model_name = request.modelName
|
134 |
+
results = []
|
135 |
|
136 |
+
# Verify if the model is loaded
|
137 |
+
if model_name not in model_pipelines:
|
138 |
+
raise HTTPException(status_code=404, detail="Model not found")
|
139 |
|
140 |
+
model = model_pipelines[model_name]
|
141 |
|
142 |
+
# Asynchronously process each image
|
143 |
+
async with httpx.AsyncClient() as client:
|
144 |
+
for image_url in request.imageUrls:
|
145 |
+
try:
|
146 |
+
response = await client.get(image_url)
|
147 |
+
image = Image.open(BytesIO(response.content))
|
148 |
+
except Exception as e:
|
149 |
+
results.append({"imageUrl": image_url, "error": "Invalid image URL"})
|
150 |
+
continue
|
151 |
|
152 |
+
# Preprocess the image
|
153 |
+
processed_image = process_image(image, size=image_size)
|
154 |
|
155 |
+
# Convert to tensor
|
156 |
+
image_tensor = transforms.ToTensor()(processed_image).unsqueeze(0)
|
157 |
|
158 |
+
# Perform inference
|
159 |
+
with torch.no_grad():
|
160 |
+
outputs = model(image_tensor)
|
161 |
+
probabilities = torch.nn.functional.softmax(outputs, dim=1)
|
162 |
+
predicted_probabilities = probabilities.numpy().tolist()
|
163 |
+
confidence = round(predicted_probabilities[0][1], 2)
|
164 |
|
165 |
+
results.append({"imageUrl": image_url, "confidence": confidence})
|
166 |
|
167 |
+
# Return the results as JSON
|
168 |
+
return JSONResponse(content={"results": results})
|
169 |
|
170 |
|
171 |
@app.post("/batch_predict")
|
172 |
async def batch_predict(request: BatchPredictRequest):
|
173 |
model_name = request.modelName
|
174 |
+
|
175 |
+
# Verify if the model is loaded
|
176 |
if model_name not in model_pipelines:
|
177 |
raise HTTPException(status_code=404, detail="Model not found")
|
178 |
|
179 |
model = model_pipelines[model_name]
|
180 |
+
semaphore = asyncio.Semaphore(
|
181 |
+
6
|
182 |
+
) # Limiter à 8 tâches simultanées pour éviter de surcharger la machine
|
183 |
+
|
184 |
+
async def process_single_image(image_url):
|
185 |
+
async with semaphore:
|
186 |
+
try:
|
187 |
+
async with httpx.AsyncClient() as client:
|
188 |
+
response = await client.get(image_url)
|
189 |
+
image = Image.open(BytesIO(response.content))
|
190 |
+
except Exception:
|
191 |
+
return {"imageUrl": image_url, "error": "Invalid image URL"}
|
192 |
+
|
193 |
+
# Preprocess the image
|
194 |
+
processed_image = process_image(image, size=image_size)
|
195 |
+
|
196 |
+
# Convert to tensor
|
197 |
+
image_tensor = transforms.ToTensor()(processed_image).unsqueeze(0)
|
198 |
+
|
199 |
+
# Perform inference
|
200 |
+
with torch.no_grad():
|
201 |
+
outputs = model(image_tensor)
|
202 |
+
probabilities = torch.nn.functional.softmax(outputs, dim=1)
|
203 |
+
predicted_probabilities = probabilities.numpy().tolist()
|
204 |
+
confidence = round(predicted_probabilities[0][1], 2)
|
205 |
+
|
206 |
+
return {"imageUrl": image_url, "confidence": confidence}
|
207 |
+
|
208 |
+
# Launch tasks in parallel
|
209 |
+
tasks = [process_single_image(url) for url in request.imageUrls]
|
210 |
+
results = await asyncio.gather(*tasks)
|
211 |
+
|
212 |
+
# Return the results as JSON
|
213 |
return JSONResponse(content={"results": results})
|