Hello all! This is a little example of using 🤗 huggingface transformers and Flask-RESTful to create a question answering API.
The only real requirement is a Linux environment. If you are using Windows I recommend installing Ubuntu for Windows. To install the needed software dependencies run:
cd /path/to/autolector
bash install_dependencies.sh # This may take a while.
bash fetch_model.sh # So could this, depending on your connection speed.
-
Open a terminal window and enter:
cd /path/to/autolector python3 autolector_server.py
and let it run while you're playing around with it. To kill the server press
Ctrl-C
while this terminal window is selected and it will close the process. -
In a new terminal (yes you need two open at the same time!), run the commands:
cd /path/to/autolector python3 autolector_client.py
You should see something like this.
Context:
The Amazon rainforest (Portuguese: Floresta Amazônica or
Amazônia; Spanish: Selva Amazónica, Amazonía or usually
Amazonia; French: Forêt amazonienne; Dutch:
Amazoneregenwoud), also known in English as Amazonia or
the Amazon Jungle, is a moist broadleaf forest that
covers most of the Amazon basin of South America. This
basin encompasses 7,000,000 square kilometres (2,700,000
sq mi), of which 5,500,000 square kilometres (2,100,000
sq mi) are covered by the rainforest. This region
includes territory belonging to nine nations. The
majority of the forest is contained within Brazil, with
60% of the rainforest, followed by Peru with 13%,
Colombia with 10%, and with minor amounts in Venezuela,
Ecuador, Bolivia, Guyana, Suriname and French Guiana.
States or departments in four nations contain "Amazonas"
in their names. The Amazon represents over half of the
"planets remaining rainforests, and comprises the "
largest and most biodiverse tract of tropical rainforest
in the world, with an estimated 390 billion individual
trees divided into 16,000 species.
Question:
Which name is also used to describe the Amazon rainforest in English?
Answer:
Amazonia.
To run the API inside a container you need to take the following steps:
Follow the instructions here to install docker on your system.
To download the models from hugging face open a terminal, navigate to the repository, and run
bash fetch_model.sh
Open a terminal and run:
cd /path/to/autolector # Optional if you're in repo root already.
# Build container and name image qa-api with version tag v1.
docker build -t autolector:v1 . # <-- DON'T FORGET THIS PERIOD!!!
In the same terminal, type in:
docker run \
-p 5000:5000 \ # binds port host:container
-v /path/to/autolector/models:/app/models \ # Mounts host:container
autolector:v1
In a new terminal window (just like before, we need two open), run the following:
# Make sure you're in repo root!
python3 autolector_client.py
And that's it! If you want to host your API in the cloud now it's almost as easy as saying docker push
.
Alternately, you can open up a browser navigate to ./index.html
or open a terminal and enter:
# Of course...
cd /path/to/autolector
# If you've installed locally.
python3 autolector_server.py & # This will run process in the background
##########
## OR ##
##########
# If you've installed with docker.
docker run \
-p 5000:5000 \ # binds port host:container
-v /path/to/autolector/models:/app/models \ # Mounts host:container
autolector:v1 & # Not sure if this is necessary, but whatever.
# You've got to change /path/to -> <the actual path>
firefox file:///path/to/autolector/index.html
after getting the API running locally as show above. I recommend this way, as it shows the real value of putting a service behind an API rather than creating a question answering agent on the command line. It should look something like this.
So now you've got a good idea of how to design a website from front to back. You can host your static code on github, put your Flask APIs somewhere in the cloud (that's what docker is good for), and you're in business. Good luck!