GitHub Copilot can help you write code faster in many
different programming languages. In this video we will be covering
how to use Copilot with Python, and we'll have a
Python command line tool that we will be making some
changes with Copilot to help make it a Python HTTP
API.
If you're new to Copilot or you don't have it
installed yet, make sure to watch this video right here.
The project that we're going to be working with is
a command line tool, summarization tool, and I'm going to
show you how that works here in the terminal. You
can see summarize and I'm going to pass a URL
and I'm going to hit return and that is going
to take a quick second there to summarize the text
and passing a Wikipedia page that describes
Microsoft Azure. Now we're going to get a lot of
warnings here, and that's fine. That's expected. And it will
take a second, because loading this very big model on
my machine will take quite a lot of computing power.
Now let's wait a second until that completes and see
what the result is.
All right. So that summarization process completed and you can
see that at the bottom, it's Microsoft Azure. It's a
cloud computing platform operated by Microsoft. Perfect. So that is
what the tool does. That's the functionality. And what we're
going to be looking at today is trying to convert
this command line tool here. I'm going
to close the terminal now and what we're going to
do is going to convert these to an HTTP API
using FastAPI and Uvicorn.
Which are two tools that I'm going to use to
actually run the API and make the HTTP framework beats
for it. Now let's walk very quickly through some of
the components here. We're going to have a few functions
that we're going to be working with. This is extract
from URL. So this accepts the URL as input and
then it sends the request and parses all of the
paragraphs and tries to produce the text that gets returned
at the very end. The 'process' function will actually do
the actual summarization process and will try to get all
of the text captured from the URL and summarize it
properly. And finally we have the main which will use
the command line pieces to try to parse. We can
even have the option of doing from a file and
summarization from a file, which sounds correct. Alright, so
first thing that we want to do
is we want to take a look at the requirements,
what are the libraries that we're going to be working
with? Transformers is fine and Tensorflow is fine as well.
The 'click' is the command line tool framework. So because
we're porting this over to an HTTP service, we're going
to not need that anymore and we're going to start
adding a little bit of the things that we're going
to be working with.
We are going to actually use FAST API. Fast API
has a good version here that I want to use,
which is .78.0. I'm going to accept there
the Copilot suggestion for that zero. And then I'm going
to say Uvicorn. And one thing that I want to
do with Uvicorn, actually I'm going to do the standard
and I'm going to accept the standard one and I'm
not going to pin it for now. Pydantic
is part of.
FastAPI, so I'm not going to pin that version
right a second. Alright, so FastAPI and Uvicorn
are two things that I want to use. Let's start
porting over main dot pie with the help of Copilot
to try to get these to stop being a command
line tool and start looking more like an HTTP API.
So first thing I want to do is we're going
to remove a click from the very top.
And here at the bottom of these imports, we're going
to start importing what we need for the API. So
from fast API, we're going to import not only fast
API but we'll also do response. I think response is
something that we want to use for some HTML things
that we're going to respond for the root. And then
from pydantic. Again I didn't install that by default because
it comes with API and...
yes, in fact we're going to use base model. I'm
going to accept there the suggestion from compiler. Next, what
I'm going to be doing is create the application for
FastAPI and this is exactly what I wanted. On
line number seven I'm going to define what my application
is going to be and now what I need to
do is create
A definition of what my input is going to be.
I'm going to be using JSON as the input for
this HTTP API endpoint. So what I need to do
is create a class that's going to inherit from base
model. You can see here the copilot is actually trying
to guess that this is exactly what I want. And
yes, in fact, yes, this is exactly what I want.
It's going to be the summarized request. I actually want
to accept text and string.
So that looks great. So let's take a quick look
at what does this mean. The text here is the
key that the value is going to be of a
string value. So behind the scenes FastAPI is able
to use these to validate the request. Is exactly what
I need.
So this looks very good so far. Next, I am
going to scroll quickly here and remove all of the
click options for the main. We are not going to
yet do any app decorators yet. We're going to define
a root for serving our application. So I'm going to
say get the slash means root. I don't want to
call it read root, but I'm going to call it
root.
And that means the top level of our API. I
don't want to do return that, but I want to
return a response with some basic HTML. Here I'm going
to say H1. And yes, and you can see here
as I'm typing, I'm kind of like guiding Copilot to
keep me more what I want to do and kind
of like showing the path where I want to go
so that the suggestions can be better from Copilot. So
I'm going to accept that.
And then the next thing that I want to do
is change this main function. So I'm going to take
this out of here and I'm going to leave the
root at the very bottom. I'm going to paste the
main over there. I'm going to rename this main. I'm
going to rename it with the name of the endpoint
that I want to do. So I'm going to say
I'm going to say this going to be 'summarize'.
And I'm going to make sure that that function name
is not repeated. It isn't, so that's very good. You
have to be careful renaming function names to avoid clashing.
So I open up a and yes again, Copilot properly figure
out that I want to expose these with a POST
HTTP, and that will allow me to do that. That's
fine. Now I need to change the arguments because remember I
created a class for accepting that request.
And in this case again, it figures out that I
want to process in the request. The request is going
to come in and it's going to be a summarize
request. Now the body of the function now is incorrect
and I need to make sure that I'm now using
a request over here to try to parse what I'm
going to be working with.
So if you remember what we were going to be
using the key for summarize request, that class that we
created before the text is the actual key that we
want. So we want to process the text from that.
That text is going to be the URL that we're
going to pass. So instead of returning as copilot does
here, we're going to say URL is going to be
request.text, and that's fine. This is a bit
of a misnomer and we can actually fix that later
because it would be preferable to call it URL, but
we're going to leave it like that now. We're no
longer going to be doing anything with a file, so
when I remove that, that's fine. We're going to define
that text. It's going to come from extract_from_url.
Remember, this is going to also.
Retrieve the contents of that remote URL and then instead
of using click, which is the command line tool framework,
what we're going to be doing is going to return
some JSON for what we want to do. So return.
We can actually even return the plain text, so it
is something that we can definitely try to do that.
So why not try to leave it simple and not
return JSON for now?
But one thing that I want to fix, and Copilot that
is going to help us try to make sure that
we're doing this right. We're going to make sure that
summarize request uses a URL as the key. So let's
go all the way up to line 10. We're going
to change this to a URL. I'm going to save
that. I'm going to Scroll down back again and now
this is no longer text. This is going to be.
URL over here. So I'm going to save that and
then I'm going to make sure to look at all
of the functions and make sure that the command line
tool, framework bits and pieces are no longer used. In
this case 38 and 39 will go away. So when
install the new libraries and make sure that we have
everything we need to run these HTTP APIs so very
quickly everything is in order. Perfect, we were able to
complete that.
Next we need to run this project. Now in order
to do that we're going to use Uvicorn. Remember we
added that two requirements that text Uvicorn is a Python
web server that is going to allow us to run
this HTTP service or HTTP API rather. So I'm going
to clear these to clean this up all of that
output.
So I'm going to change to summarize and then that's
where our main.py module is and I'm going
to run Uvicorn and run this so that we can
see this. I'm going to say host is going to
be 0.0.0.0. That means that it's
going to bind to any address and that's fine. And
our application is in the main module and ':'
means that inside that main module we're going to have
our application defined as app and we want to run
these and we're going to run these and see. Hopefully
it runs without a problem or otherwise we'll need to
make some fixes. So this is our summarization API. Remember
I had that sloppy HTML that we created and the
address is 0.0.0.0: port 8000 locally on my
computer.
One of the things that we have with FastAPI
is that we have docs. So let's see if that
works so perfect. You can see that we have these
automatically generated documentation with FAST API. So what we're going
to do is take a quick look at what we
have. We have the post for summarize and we're going
to expand that and we have several different options, the
URL, string that looks perfect. So let's go ahead and
try it out. I'm going to click this button here
to try it out.
And this is the request body. But of course we
don't want string. This is just an example. We're going
to pass here the actual URL. That's the Microsoft Azure
Wikipedia entry. That looks correct. I'm going to click execute
and we'll see what is going on. All right, So
unrecognized response type, displaying content as text. That's fine. It
did actually quite a very good job to try to
do a little bit of summarization almost right away.
So that's it. We were able to complete this porting
over of these Python command line tool into this HTTP
API with Copilot, and we were able to go pretty
fast. If you found this video useful, make sure you
like and subscribe. If you want to catch up on
the rest of the Copilot series, make sure you watch
these videos right here.