bug(medcat-trainer): Uploading exports with html issue#569
Open
mart-r wants to merge 4 commits into
Open
Conversation
tomolopolis
reviewed
Jun 26, 2026
tomolopolis
left a comment
Member
There was a problem hiding this comment.
not sure I understand what has happened here? do no uploads work anymore or only some fail if they have HTML in the text?
Looks like most of this change is refactor rather than fixing the import function right?
|
|
||
|
|
||
| def _prepare_state(proj: dict) -> tuple[set, dict, set, set, dict]: | ||
| # ensure current deployment has the neccessary - Entity, MetaTak, Relation, and warn on not present User objects. |
Collaborator
Author
There was a problem hiding this comment.
I can fix it, but was there before :)
Collaborator
Author
It was only an issue for text with HTML tags - sorry, should have made that clearer.
Yes - 98% of non-test code is just refactor. The only change was this: diff --git a/medcat-trainer/webapp/api/api/data_utils.py b/medcat-trainer/webapp/api/api/data_utils.py
index 56faf420..a4220f69 100644
--- a/medcat-trainer/webapp/api/api/data_utils.py
+++ b/medcat-trainer/webapp/api/api/data_utils.py
@@ -119,7 +119,7 @@ def _init_proj_ann_ents(
return p
-def _create_dataset(proj: dict, p: ProjectAnnotateEntities) -> Dataset:
+def _create_dataset(proj: dict, p: ProjectAnnotateEntities) -> tuple[Dataset, dict[str, str]]:
# escape - filename
ds_file_name = MEDIA_ROOT + '/' + re.sub('/|\.', '_', p.name + '_dataset') + '.csv'
names = [doc['name'] for doc in proj['documents']]
@@ -132,7 +132,13 @@ def _create_dataset(proj: dict, p: ProjectAnnotateEntities) -> Dataset:
ds_mod.original_file.name = ds_file_name
ds_mod.save()
p.dataset = ds_mod
- return ds_mod
+ # creating text 2 name mapping so we can find the doucments based on the name
+ # even if the text has been processed through pandas conversion and/or sanitisation
+ text2name: dict[str, str] = {
+ doc["text"]: name
+ for doc, name in zip(proj['documents'], names)
+ }
+ return ds_mod, text2name
def _upload_project(
@@ -152,7 +158,7 @@ def _upload_project(
ent_labels, meta_tasks, rels, unavailable_users, available_users = _prepare_state(proj)
- ds_mod = _create_dataset(proj, p)
+ ds_mod, text2name = _create_dataset(proj, p)
p.save()
@@ -200,11 +206,21 @@ def _upload_project(
for doc in proj['documents']:
- _process_doc(doc, p, ds_mod, available_users)
+ _process_doc(doc, p, ds_mod, available_users, text2name)
-def _process_doc(doc: dict, p: ProjectAnnotateEntities, ds_mod: Dataset, available_users: dict):
- doc_mod = Document.objects.filter(Q(dataset=ds_mod) & Q(text=doc['text'])).first()
+def _process_doc(
+ doc: dict,
+ p: ProjectAnnotateEntities,
+ ds_mod: Dataset,
+ available_users: dict,
+ text2name: dict[str, str],
+):
+ # NOTE: using text2name mapping here since the text in the database
+ # can be sanitised and changed during pandas serialisation (and deserialisation),
+ # however we've kpet track of per-text names (which are unique) so will use these
+ # instead here
+ doc_mod = Document.objects.filter(Q(dataset=ds_mod) & Q(name=text2name[doc['text']])).first()
annos = []
for anno in doc['annotations']:
a = AnnotatedEntity() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There's an issue with uploading certain trainer exports (or other things in that format) to trainer. Specifically, HTML tag-including ones. If these aren't present, there doesn't seem to be any issue.
There's 2 underlying issues:
api.data_utils.sanitise_input) to remove HTML tagspd.DataFrameand saved on diskDataset.save()call triggers theDocumentcreation from the fileThe combination of the above two means that the text that's getting queried no longer matches the text that's in the database. And just using
sanitise_inputwould also not fix the issue alone.For reference, the type of failure I'm talking about is this:
https://gist.github.com/mart-r/74b8b44e96f816068166bb772c8f4519
As we can see, the
document_idfor a specific entity isNone. And that's because the search for it returned nothing.So the solution I've come up with is to keep track of the (unique!) names of each document and use those for the lookup instead. That way they always match.
Along the way I've also refactored the
api.data_utils.upload_projects_exportfunction and split it into different parts.PS:
The current plan for this PR is to have the first workflow (that only adds the test) fail, and have the subsequent workflow (with the fix) to succeed.