Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Commit
·
7f89f2f
1
Parent(s):
08ec402
fix issues lambda
Browse files
main.py
CHANGED
@@ -16,6 +16,24 @@ from typing import List
|
|
16 |
import httpx
|
17 |
from concurrent.futures import ProcessPoolExecutor
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
app = FastAPI()
|
20 |
|
21 |
image_size = 256
|
@@ -61,23 +79,6 @@ model_pipelines = {}
|
|
61 |
base_model = ResNet("resnet152", num_output_neurons=2).to(device)
|
62 |
|
63 |
|
64 |
-
def process_single_image(image_url, model):
|
65 |
-
try:
|
66 |
-
response = requests.get(image_url)
|
67 |
-
image = Image.open(BytesIO(response.content))
|
68 |
-
processed_image = process_image(image, size=image_size)
|
69 |
-
image_tensor = transforms.ToTensor()(processed_image).unsqueeze(0)
|
70 |
-
|
71 |
-
with torch.no_grad():
|
72 |
-
outputs = model(image_tensor)
|
73 |
-
probabilities = torch.nn.functional.softmax(outputs, dim=1)
|
74 |
-
predicted_probabilities = probabilities.numpy().tolist()
|
75 |
-
confidence = round(predicted_probabilities[0][1], 2)
|
76 |
-
return {"imageUrl": image_url, "confidence": confidence}
|
77 |
-
except Exception as e:
|
78 |
-
return {"imageUrl": image_url, "error": str(e)}
|
79 |
-
|
80 |
-
|
81 |
@app.on_event("startup")
|
82 |
async def load_models():
|
83 |
# Charger les modèles au démarrage
|
@@ -194,7 +195,7 @@ async def batch_predict(request: BatchPredictRequest):
|
|
194 |
with ProcessPoolExecutor() as executor:
|
195 |
results = list(
|
196 |
executor.map(
|
197 |
-
|
198 |
)
|
199 |
)
|
200 |
|
|
|
16 |
import httpx
|
17 |
from concurrent.futures import ProcessPoolExecutor
|
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 |
|
39 |
image_size = 256
|
|
|
79 |
base_model = ResNet("resnet152", num_output_neurons=2).to(device)
|
80 |
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
@app.on_event("startup")
|
83 |
async def load_models():
|
84 |
# Charger les modèles au démarrage
|
|
|
195 |
with ProcessPoolExecutor() as executor:
|
196 |
results = list(
|
197 |
executor.map(
|
198 |
+
process_single_image, [(url, model) for url in request.imageUrls]
|
199 |
)
|
200 |
)
|
201 |
|