Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for optional annotation of AMR linking #857

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions skema/rest/integrated_text_reading_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def integrated_extractions(
)
async def integrated_text_extractions(
response: Response,
texts: TextReadingInputDocuments,
inputs: TextReadingInputDocuments,
annotate_skema: bool = True,
annotate_mit: bool = True,
) -> TextReadingAnnotationsOutput:
Expand All @@ -428,10 +428,12 @@ async def integrated_text_extractions(
```
"""
# Get the input plain texts
texts = texts.texts
texts = inputs.texts

amrs = inputs.amrs

# Run the text extractors
return integrated_extractions(
extractions = integrated_extractions(
response,
annotate_text_with_skema,
texts,
Expand All @@ -440,6 +442,31 @@ async def integrated_text_extractions(
annotate_mit
)

# Do the alignment
aligned_amrs = list()
if len(amrs) > 0:
# Build an UploadFile instance from the extractions
json_extractions = extractions.model_dump_json()
extractions_ufile = UploadFile(file=io.BytesIO(json_extractions.encode('utf-8')))
for amr in amrs:
# amr = json.loads(amr)
amr_ufile = UploadFile(file=io.BytesIO(amr.encode('utf-8')))
try:
aligned_amr = metal_proxy.link_amr(
amr_file=amr_ufile,
text_extractions_file=extractions_ufile)
aligned_amrs.append(aligned_amr)
except Exception as e:
error = TextReadingError(pipeline="AMR Linker", message=f"Error annotating {amr.filename}: {e}")
if extractions.generalized_errors is None:
extractions.generalized_errors = [error]
else:
extractions.generalized_errors.append(error)

extractions.aligned_amrs = aligned_amrs

return extractions


@router.post(
"/integrated-pdf-extractions",
Expand Down
5 changes: 5 additions & 0 deletions skema/rest/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import List, Optional, Dict, Any

from askem_extractions.data_model import AttributeCollection
from fastapi import UploadFile
from pydantic import BaseModel, Field

# see https://github.com/pydantic/pydantic/issues/5821#issuecomment-1559196859
Expand Down Expand Up @@ -168,6 +169,10 @@ class TextReadingInputDocuments(BaseModel):
description="List of input plain texts to be annotated by the text reading pipelines",
examples=[["x = 0", "y = 1", "I: Infected population"]],
)
amrs: List[str] = Field(
description="List of optional AMR files to align with the extractions",
examples=[[]]
)


class TextReadingError(BaseModel):
Expand Down
3 changes: 2 additions & 1 deletion skema/rest/tests/test_integrated_text_reading_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def test_text_integrated_extractions():
"x = 0",
"y = 1",
"I: Infected population"
]
],
"amrs": []
}

response = client.post(f"/integrated-text-extractions", params=params, json=payload)
Expand Down
Loading