Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
Base Worker
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Workers
Base Worker
Commits
34d13098
Commit
34d13098
authored
4 years ago
by
Eva Bardou
Browse files
Options
Downloads
Patches
Plain Diff
Add logic to store Transcriptions in cache
parent
0a4bbe4b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
arkindex_worker/cache.py
+13
-0
13 additions, 0 deletions
arkindex_worker/cache.py
arkindex_worker/worker.py
+60
-3
60 additions, 3 deletions
arkindex_worker/worker.py
with
73 additions
and
3 deletions
arkindex_worker/cache.py
+
13
−
0
View file @
34d13098
...
...
@@ -13,6 +13,14 @@ SQL_ELEMENTS_TABLE_CREATION = """CREATE TABLE IF NOT EXISTS elements (
initial BOOLEAN DEFAULT 0 NOT NULL,
worker_version_id VARCHAR(32)
)
"""
SQL_TRANSCRIPTIONS_TABLE_CREATION
=
"""
CREATE TABLE IF NOT EXISTS transcriptions (
id VARCHAR(32) PRIMARY KEY,
element_id VARCHAR(32) NOT NULL,
text TEXT NOT NULL,
confidence REAL NOT NULL,
worker_version_id VARCHAR(32) NOT NULL,
FOREIGN KEY(element_id) REFERENCES elements(id)
)
"""
CachedElement
=
namedtuple
(
...
...
@@ -20,6 +28,10 @@ CachedElement = namedtuple(
[
"
id
"
,
"
name
"
,
"
type
"
,
"
polygon
"
,
"
worker_version_id
"
,
"
parent_id
"
,
"
initial
"
],
defaults
=
[
None
,
0
],
)
CachedTranscription
=
namedtuple
(
"
CachedTranscription
"
,
[
"
id
"
,
"
element_id
"
,
"
text
"
,
"
confidence
"
,
"
worker_version_id
"
],
)
class
LocalDB
(
object
):
...
...
@@ -31,6 +43,7 @@ class LocalDB(object):
def
create_tables
(
self
):
self
.
cursor
.
execute
(
SQL_ELEMENTS_TABLE_CREATION
)
self
.
cursor
.
execute
(
SQL_TRANSCRIPTIONS_TABLE_CREATION
)
def
insert
(
self
,
table
,
lines
):
if
not
lines
:
...
...
This diff is collapsed.
Click to expand it.
arkindex_worker/worker.py
+
60
−
3
View file @
34d13098
...
...
@@ -17,7 +17,7 @@ from apistar.exceptions import ErrorResponse
from
arkindex
import
ArkindexClient
,
options_from_env
from
arkindex_worker
import
logger
from
arkindex_worker.cache
import
CachedElement
,
LocalDB
from
arkindex_worker.cache
import
CachedElement
,
CachedTranscription
,
LocalDB
from
arkindex_worker.models
import
Element
from
arkindex_worker.reporting
import
Reporter
from
arkindex_worker.utils
import
convert_str_uuid_to_hex
...
...
@@ -524,7 +524,7 @@ class ElementsWorker(BaseWorker):
)
return
self
.
api_client
.
request
(
created
=
self
.
api_client
.
request
(
"
CreateTranscription
"
,
id
=
element
.
id
,
body
=
{
...
...
@@ -533,8 +533,24 @@ class ElementsWorker(BaseWorker):
"
score
"
:
score
,
},
)
self
.
report
.
add_transcription
(
element
.
id
)
# Store transcription in local cache
try
:
to_insert
=
[
CachedTranscription
(
id
=
convert_str_uuid_to_hex
(
created
[
"
id
"
]),
element_id
=
convert_str_uuid_to_hex
(
element
.
id
),
text
=
created
[
"
text
"
],
confidence
=
created
[
"
confidence
"
],
worker_version_id
=
convert_str_uuid_to_hex
(
self
.
worker_version_id
),
)
]
self
.
cache
.
insert
(
"
transcriptions
"
,
to_insert
)
except
sqlite3
.
IntegrityError
as
e
:
logger
.
warning
(
f
"
Couldn
'
t save created transcription in local cache:
{
e
}
"
)
def
create_classification
(
self
,
element
,
ml_class
,
confidence
,
high_confidence
=
False
):
...
...
@@ -703,14 +719,55 @@ class ElementsWorker(BaseWorker):
"
return_elements
"
:
True
,
},
)
for
annotation
in
annotations
:
created_ids
=
[]
elements_to_insert
=
[]
transcriptions_to_insert
=
[]
parent_id_hex
=
convert_str_uuid_to_hex
(
element
.
id
)
worker_version_id_hex
=
convert_str_uuid_to_hex
(
self
.
worker_version_id
)
for
index
,
annotation
in
enumerate
(
annotations
):
transcription
=
transcriptions
[
index
]
element_id_hex
=
convert_str_uuid_to_hex
(
annotation
[
"
id
"
])
if
annotation
[
"
created
"
]:
logger
.
debug
(
f
"
A sub_element of
{
element
.
id
}
with type
{
sub_element_type
}
was created during transcriptions bulk creation
"
)
self
.
report
.
add_element
(
element
.
id
,
sub_element_type
)
if
annotation
[
"
id
"
]
not
in
created_ids
:
# TODO: Retrieve real element_name through API
elements_to_insert
.
append
(
CachedElement
(
id
=
element_id_hex
,
parent_id
=
parent_id_hex
,
name
=
"
test
"
,
type
=
sub_element_type
,
polygon
=
json
.
dumps
(
transcription
[
"
polygon
"
]),
worker_version_id
=
worker_version_id_hex
,
)
)
created_ids
.
append
(
annotation
[
"
id
"
])
self
.
report
.
add_transcription
(
annotation
[
"
id
"
])
transcriptions_to_insert
.
append
(
CachedTranscription
(
# TODO: Retrieve real transcription_id through API
id
=
convert_str_uuid_to_hex
(
uuid
.
uuid4
()),
element_id
=
element_id_hex
,
text
=
transcription
[
"
text
"
],
confidence
=
transcription
[
"
score
"
],
worker_version_id
=
worker_version_id_hex
,
)
)
# Store transcriptions and their associated element (if created) in local cache
try
:
self
.
cache
.
insert
(
"
elements
"
,
elements_to_insert
)
self
.
cache
.
insert
(
"
transcriptions
"
,
transcriptions_to_insert
)
except
sqlite3
.
IntegrityError
as
e
:
logger
.
warning
(
f
"
Couldn
'
t save created transcriptions in local cache:
{
e
}
"
)
return
annotations
def
create_metadata
(
self
,
element
,
type
,
name
,
value
,
entity
=
None
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment