allenai / s2_fos

Apache License 2.0
32 stars 2 forks source link

Implement predictor #2

Closed regan-huff closed 2 years ago

regan-huff commented 3 years ago

Addresses https://github.com/allenai/scholar/issues/29237

This PR adapts code from https://github.com/allenai/s2-fos-data/pull/7 to enable the model generated there to be deployed from the s2-fos repo. A successful merge of this PR will allow "make serve" to launch an http endpoint that can return a list of foses given a title/abstract instance.

Follow up work represented in https://github.com/allenai/scholar/issues/29012 will include updating training and evaluation functions in s2-fos.

This code has been deployed following the notes given in the s2agemaker demo

S2AGEMAKER_PROJECT_PATH=~/src/s2-fos \
  ./deploy_adhoc_model.sh \
  --model-name s2-fos \
  --model-version 0 \
  --contact reganh \
  --artifacts-s3-path s3://ai2-s2-research/fos/packaged_artifacts/artifacts.tar.gz

endpoint may be invoked with a python script such as:

import json
import boto3

instances = [{"title": "Atomistic/Continuum Blending with Ghost Force Correction", "abstract": "We combine the ideas of atomistic/continuum energy blending and ghost force correction to obtain an energy-based atomistic/continuum coupling scheme which has, for a range of benchmark problems, the same convergence rates as optimal force-based coupling schemes. We present the construction of this new scheme, numerical results exploring its accuracy in comparison with established schemes, as well as a rigorous error analysis for an instructive special case."}, {"title": "Mapping tropospheric ozone profiles from an airborne ultraviolet-visible spectrometer", "abstract": "We present a novel technique for retrieving ozone (O3) profiles and especially tropospheric O3 from airborne UV/visible spectrometer measurements. This technique utilizes radiance spectra from one down-looking and two up-looking (85 degrees and 75 degrees) directions, taking advantage of the O3 absorption structure in the Huggins (300-340-nm) and Chappuis (530-650-nm) bands. This technique is especially sensitive to tropospheric O3 below and < or =8 km above the aircraft with a vertical resolution of 2-6 km and is sensitive to lower and middle stratospheric O3 with a vertical resolution of 8-15 km. It can measure tropospheric O3 at spatial resolutions of 2 km x 2 km or higher and is therefore well suited for regional air-quality studies and validation of satellite measurements."}]
sm = boto3.client("runtime.sagemaker")
resp = sm.invoke_endpoint(
  EndpointName="s2-fos-0-adhoc",
  ContentType="application/json",
  Body=json.dumps({"instances": instances})
)

resp_body = json.loads(resp["Body"].read().decode())

print(resp_body)

and will return a response such as:

{'predictions': [{'foses': ['Physics']}, {'foses': ['Environmental science']}]}