In this task we are going to deploy the logo-detection model-server, recently published to the Wikimedia docker registry (T362598), to LiftWing staging for user acceptance testing by the Structured Content team. This will involve the team testing functionality using Upload Stash URLs, identifying and reporting edge cases encountered, and collaborating with the ML team to resolve these issues to ensure the logo-detection inference service readiness for production use.
Description
Details
| Status | Subtype | Assigned | Task | ||
|---|---|---|---|---|---|
| Open | None | T349641 [EPIC] MVP Logo machine detection on Commons | |||
| Resolved | kevinbazira | T358676 Host a logo detection model for Commons images | |||
| Resolved | kevinbazira | T362749 Deploy logo-detection model-server to LiftWing staging | |||
| Resolved | kevinbazira | T363449 Configure the logo-detection model-server hosted on LiftWing to process images from Wikimedia Commons |
Event Timeline
Change #1020706 had a related patch set uploaded (by Kevin Bazira; author: Kevin Bazira):
[operations/deployment-charts@master] ml-services: add logo-detection isvc to experimental namespace
Change #1020710 had a related patch set uploaded (by Kevin Bazira; author: Kevin Bazira):
[machinelearning/liftwing/inference-services@main] logo-detection: specify model name
Change #1020710 merged by jenkins-bot:
[machinelearning/liftwing/inference-services@main] logo-detection: specify model name
Change #1020706 merged by jenkins-bot:
[operations/deployment-charts@master] ml-services: add logo-detection isvc to experimental namespace
I have deployed the logo-detection model-server in the experimental namespace on LiftWing staging. On checking the pod, I noticed it was not starting successfully due to a CrashLoopBackOff error:
$ kubectl get pods . . . NAME READY STATUS RESTARTS AGE logo-detection-predictor-00001-deployment-7f98bb54f7-hpnf4 1/3 CrashLoopBackOff 5 (36s ago) 4m31s . . .
I have reviewed the logs and found that the CrashLoopBackOff error is occurring because the model-server lacks the necessary permissions to load the logo-detection model:
$ kubectl describe pod logo-detection-predictor-00001-deployment-7f98bb54f7-hpnf4 . . . Traceback (most recent call last): File "/srv/logo_detection/model_server/model.py", line 210, in <module> model = LogoDetectionModel(model_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/srv/logo_detection/model_server/model.py", line 33, in __init__ self.model = self.load() ^^^^^^^^^^^ File "/srv/logo_detection/model_server/model.py", line 36, in load model = keras.models.load_model(self.model_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/lib/python/site-packages/keras/src/saving/saving_api.py", line 176, in load_model return saving_lib.load_model( ^^^^^^^^^^^^^^^^^^^^^^ File "/opt/lib/python/site-packages/keras/src/saving/saving_lib.py", line 139, in load_model with file_utils.File(filepath, mode="r+b") as gfile_handle, zipfile.ZipFile( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/lib/python/site-packages/keras/src/utils/file_utils.py", line 436, in File return open(path, mode=mode) ^^^^^^^^^^^^^^^^^^^^^ PermissionError: [Errno 13] Permission denied: '/mnt/models/logo_max_all.keras' . . .
I am going to reach out to SREs to assist in resolving this permission issue.
I've fetched the image locally and run a shell in it like so:
docker run -it --entrypoint /bin/bash docker-registry.wikimedia.org/wikimedia/machinelearning-liftwing-inference-services-logo-detection:stable
I wanted to check the permissions on /mnt/models/, and it turns out, the directory doesn't exist (/mnt does). So my guess is that the Blubberfile needs to be adjusted to create that directory (with the right permissions).
Change #1021397 had a related patch set uploaded (by Kevin Bazira; author: Kevin Bazira):
[machinelearning/liftwing/inference-services@main] logo-detection: downgrade bookworm to bullseye
Change #1021397 merged by jenkins-bot:
[machinelearning/liftwing/inference-services@main] logo-detection: downgrade bookworm to bullseye
Change #1021398 had a related patch set uploaded (by Kevin Bazira; author: Kevin Bazira):
[operations/deployment-charts@master] ml-services: add logo-detection isvc to experimental ns
Change #1021398 merged by jenkins-bot:
[operations/deployment-charts@master] ml-services: add logo-detection isvc to experimental ns
The directory is being created by the storage-initializer container when the pod initializes.
I attached a shell to the running pod and checked the permissions on the file and they seem ok
-rw-r--r-- 1 nobody daemon 71333909 Apr 19 09:10 logo_max_all.keras
For comparison these are the permissions from revertrisk-wikidata model
-rw-r--r-- 1 nobody daemon 1019107337 Apr 12 15:54 model.pkl
Same stands for the directories
when I execute
ls -ld /mnt /mnt/models
drwxr-xr-x 1 root root 4096 Apr 12 15:54 /mnt drwxrwsrwx 2 root daemon 4096 Apr 12 15:54 /mnt/models
drwxr-xr-x 1 root root 4096 Apr 19 09:10 /mnt drwxrwsrwx 2 root daemon 4096 Apr 19 09:10 /mnt/models
This is a bug in keras: it tries to open the file with mode r+b (read, append, binary), but since the file is owned by another user (nobody vs. somebody), the call fails. Why keras would need to be able to append to the file, I don't know.
Change #1021883 had a related patch set uploaded (by Ilias Sarantopoulos; author: Ilias Sarantopoulos):
[machinelearning/liftwing/inference-services@main] logo-detection: bump keras to 3.2.1
Filed a patch to test latest keras version (3.2.1) which opens the file in "rb" mode
https://github.com/keras-team/keras/blob/master/keras/src/saving/saving_lib.py#L151
keras 3.2.1
with open(filepath, "rb") as f:
return _load_model_from_fileobj(
f, custom_objects, compile, safe_mode
)vs keras 3.0.4 (current version) https://github.com/keras-team/keras/blob/v3.0.4/keras/saving/saving_lib.py#L139
with file_utils.File(filepath, mode="r+b") as gfile_handle, zipfile.ZipFile(
gfile_handle, "r"
) as zf:
with zf.open(_CONFIG_FILENAME, "r") as f:
config_json = f.read()Change #1021883 merged by jenkins-bot:
[machinelearning/liftwing/inference-services@main] logo-detection: bump keras to 3.2.1
Upgrading to keras==3.2.1 resolved the above issue. Nice catch @klausman!
Now in order for requests to work we'd need to give the model server connectivity to the commons upload stash so that the model server can download the images.
Thank you for your help in troubleshooting and resolving the permissions issue, @klausman and @isarantopoulos!
@mfossati, when a model-server is deployed within the WMF k8s infrastructure it has to be configured to enable it to access external resources like wikimedia, wikipedia, and wikidata (see details here). Is it possible for the Structured content team to provide sample URLs from the commons upload stash? This will enable us to configure the logo-detection model-server to access them from LiftWing. Thanks in advance.
Change #1021908 had a related patch set uploaded (by Ilias Sarantopoulos; author: Ilias Sarantopoulos):
[operations/deployment-charts@master] ml-services: update keras version in logo detection
Change #1021399 had a related patch set uploaded (by Kevin Bazira; author: Kevin Bazira):
[machinelearning/liftwing/inference-services@main] logo-detection: upgrade bullseye to bookworm
Change #1021399 merged by jenkins-bot:
[machinelearning/liftwing/inference-services@main] logo-detection: upgrade bullseye to bookworm
Change #1021401 had a related patch set uploaded (by Kevin Bazira; author: Kevin Bazira):
[operations/deployment-charts@master] ml-services: upgrade OS in logo-detection
Change #1021402 had a related patch set uploaded (by Kevin Bazira; author: Kevin Bazira):
[operations/deployment-charts@master] ml-services: upgrade OS in logo-detection
Change #1021908 merged by Ilias Sarantopoulos:
[operations/deployment-charts@master] ml-services: update keras version in logo detection
Change #1021401 merged by jenkins-bot:
[operations/deployment-charts@master] ml-services: upgrade OS in logo-detection
Hey @kevinbazira , here's how a public stash URL would look like: https://commons.wikimedia.org/wiki/Special:UploadStash/file/1avpfxdmdb4c.deuia.10893556.png. The only variable would be the file key, i.e., 1avpfxdmdb4c.deuia.10893556.png.
Not 100% sure, but I guess that you can go for http://localhost:6500/wiki/Special:UploadStash/file/1avpfxdmdb4c.deuia.10893556.png, with commons.wikimedia.org as the host header.
As we work to enable the logo-detection model-server to access images from the upload stash using a k8s endpoint, @achou pointed out that files might not be accessible since the upload stash docs state: files not be public, and only writable/accessible by the uploader.
I was able to reproduce this issue using the steps below:
- visit the Commons UploadWizard: https://commons.wikimedia.org/wiki/Special:UploadWizard
- use the UploadWizard to upload an image but don't publish it
- visit Commons UploadStash: https://commons.wikimedia.org/wiki/Special:UploadStash
- copy URL of stash key and try to access it in incognito. it's not publicly accessible.
@mfossati, if images uploaded to the stash are private to the user, how will the tool you build to do logo-detection be able to access these image URLs or serialized image objects and send them to the LiftWing API to get a prediction?
Great catch, I totally missed this!
I've just scratched the surface: it seems that the stash URL request should contain some logged-in user session ID to enable access, which is stored in a cookie. We'll have to dig into the Upload stash code base to fully understand the mechanism. For now I can see cookies like commonswikiSession that ring a bell.
That said, what if we T363506: Pass image objects to the logo detection service instead? Would that not require a logged-in user? Definitely an open question.
@mfossati and I were able to download images that we uploaded to the commons stash by using the stash file URL and a cookie as shown in the function below:
import requests
def download_image_from_stash(url, cookie, save_path="downloaded_image.jpg"):
"""
Download an image from the Wikimedia Commons UploadStash using the provided URL and cookie.
"""
headers = {"cookie": cookie}
response = requests.get(url, headers=headers)
if response.status_code == 200:
with open(save_path, 'wb') as f:
f.write(response.content)
print("Image downloaded successfully from Commons UploadStash.")
else:
print("Failed to download image from Commons UploadStash. Status code:", response.status_code)
# Example usage
url_kevin = "https://commons.wikimedia.org/wiki/Special:UploadStash/file/1awnoke3jq8k.fndsu8.7972071.jpg"
url_marco = "https://commons.wikimedia.org/wiki/Special:UploadStash/file/1awnvkvdmzw0.bqey2h.10893556.png"
cookie_kevin = "<redacted>"
cookie_marco = "<redacted>"
download_image_from_stash(url_kevin, cookie_kevin)
# download_image_from_stash(url_marco, cookie_marco)@mfossati is there any other way to access the images in the upload stash other than using a cookie. Using a user cookie to access an API doesn't seem like the right way for a production application both from a design as well as a security point of view. An API key/token would seem more appropriate (if there is such an option available).
Another if there is the possibility to allow Lift Wing IPs direct access to the stash in some way, but then again I'm unaware if that is an option.
I agree and have dug deeper in the current request being made to the Upload API: maybe the CSRF token is what we're looking for. See upload_file_in_chunks in the example request code. I can confirm that the Upload Wizard is sending a token parameter in the request.
Another if there is the possibility to allow Lift Wing IPs direct access to the stash in some way, but then again I'm unaware if that is an option.
I don't know. Perhaps this is a question for folks that know how the object storage work, CC @jcrespo @MatthewVernon.
The short answer is: no, I don't think giving privileged swift access to Lift Wing (or anything else) based on IP is a possible (or really appropriate) solution.
If I understand correctly, this is an admin tool that is able to look at what would otherwise be private images (i.e. they're still in a user's stash)? That seems like the sort of thing that should have its access controls Done Properly.
Yes, Upload stash shouldn't be accessed directly or indirectly. It is internal to mediawiki and private. You can do it post-upload and add a comment or something (similar to what Automoderator is planning to do with edits or what ores did)
Having it private makes total sense from a user privacy point of view. This would also mean that sending image thumbnails from the stash to Lift Wing is out of the question.
@mfossati @AUgolnikova-WMF Would the post-upload be an option?
Product wise, it would not be a reasonable option for us and would defeat the goal of preventing bad uploads from coming in during the upload which is what we are focused on. We want to show the warning during the upload flow after the user uploaded the image and is adding information about licences to prevent them adding a wrong license which leads to deletion requests. There are already some image checks that are happening on the first step of the upload, right?
It's not an impossible problem to fix but we should have been informed about this way sooner to be able to come up with a solution that doesn't compromise privacy, reliability or security.
Yes, please refer to the requirements in the initial ticket discussed in March https://phabricator.wikimedia.org/T358676#9637065
Unless I'm missing something, no-one from the Data Persistence team was copied into that ticket? I think the first we knew of this when a couple of us were tagged on this ticket on the 10th of May.
Depends on what you mean by "able to look at". A user has uploaded an image to our infrastructure - we're running it through a model and getting back a number. It's not really much different from the hash comparisons we already do.
... but if we don't want to give LiftWing access to swift then we'll see if we can do it another way
It's not just the privacy. For example, we want to eventually implement uploading from URLs via chunks which means a file being uploaded can have many stashed files instead of one. So this is about the encapsulation of how mw works.
We should also have discussions regarding performance and reliability. e.g. graceful degradation, reasonable timeouts, etc. We should also get people from mw team to comment on the design. For example, you need to make sure you never call liftwing during upload for bots or people with > X number of edits or files that are larger than Y (e.g. ignore videos) . Once that is established, you can just send over the file to liftwing maybe? (we should consider alternative designs and so on).
I think that the logo detection service can be exposed through an internal endpoint, so it will be inside WMF’s infrastructure.
Moreover, when an image is sent to the upload stash, there’s a set of already implemented checks including existing duplicates and previously deleted duplicates.
That said, I think that directly passing image objects to the service would avoid these issues.
Change #1021402 merged by jenkins-bot:
[operations/deployment-charts@master] ml-services: update logo-detection image