When I left you, we had just created this simple user interface for converting from Python to C plus
plus using different frontier models.
And I said, you know what?
Let's make this into a proper prototype UI and let's do that.
So if I come back here to this Jupyter notebook, we're going to do, we're going to write a few more
methods.
So first of all this one execute Python takes code is going to do the same slightly dangerous thing
here.
Execute that code.
So again this particular prototype is really just for you.
This is not one to share because if you share this with anybody, they will have a way to execute Python
code directly on your box.
And that would be a bad thing.
So share should always very much be false in in this particular Gradio app.
Uh, but it's, it's a great prototype for you to work with.
Um, so what this does is, is execute that code.
And the reason there's some stuff, some gubbins around it is because we want to capture the standard
out that comes from this Python code, and return it so that this becomes a function that takes code,
executes it, and returns the output because we want to display it in gradio.
So that's an execute Python.
And now some slightly heftier code to execute C plus plus um given some code.
So the first thing I do is I call that that that function we wrote a while back to actually write it
out to the file optimized.
Um, and then what I do is something which involves, uh, running the compile command.
I use something called subprocess dot run, which is a way that you can, uh, have spawn a subprocess
to, to carry something out.
Um, and that's what I do.
Now, you may notice this is slightly longer than before.
I mentioned a while back what we're going to do this this, this is a really using all of the flags
to make sure that the code is optimized for my computer.
It's actually barely scratching the surface for people that are that know all about this stuff.
There are a gazillion flags that you could add in, but I.
After a while I figured this is good enough, so we put in a bunch of flags to do this.
Um, and um, then we run the optimized program, and then we return the we, we capture the standard
out and return that.
Or if there's an error, we return standard error.
So that is the method to execute C plus plus code and return the response.
It's really compile and execute would be more accurate.
Um here I've just got a little CSS string because I want to do some uh, a little bit of coloring in,
um, as you will see.
And then this is the extent of the user interface, that's all.
We're not doing any more coding.
It's just this.
So let me quickly tell you, um, I've got again, I'm using the GR blocks, which is the the lower
level Gradio API that lets you have more flexibility on your user interface, not using one of radio's
off the shelf UIs.
Um, you can pass in your own CSS, which is what I'm doing here.
CSS equals this variable.
Um, and that lets you do a little bit more customization.
This dot markdown gives me a nice heading at the top.
And then I've got one, two, three, four, five rows of widgets.
Um you'll recognize Python code and C plus plus code.
That's exactly what I had before setting the the the default value to be the pi code.
Uh, then this is as before, the drop down to pick GPT or Claude.
Uh, the button to convert code.
And then I've got some more buttons.
Python run to run Python and C plus plus CP run to run C plus plus.
And then I've got a Python out with Python's output CP out for the C plus plus output.
Hopefully you're following, but if not, when you see the user interface, this is all going to be
utterly clear.
And then you can look back at this and you'll say, wow, that's that's so simple.
Um, so then we've got three buttons convert, Python run and C plus plus run.
So we have to say what happens when you click any of those three buttons?
If you press the convert button, it's just as before.
It should run the optimize method function, passing in the inputs, the Python code, and the model.
The output goes to the C plus plus code, so when you press convert it will take the Python code.
It will use the model to convert it and put that into C plus plus.
If you press the press the Python run button it's going to execute Python with the Python code to Python
out.
If you press C plus plus run, it's going to execute the C plus plus code with the C plus plus inputs.
C plus plus output.
That's all there is to it.
If that sounded, uh, like a like a lot of spaghetti stuff that it's, it's gonna nicely unravel and
be super clear when you see what you're about to see.
Uh, let's run this.
Here is the user interface.
Welcome to a more colorful user interface.
Uh, so there's Python code here, C plus plus code.
And then we get to select a model convert code and we can run Python run C plus plus.
So let's start.
We can press Run Python to run this code here.
Uh right from this user interface.
It's calling that exact method right now.
And we see the results here.
Uh, there it is.
Um, there is indeed Pi.
And it took 8.4 seconds.
Uh, and we will now flip to, uh, we will now convert that to C plus.
Plus.
Here it is.
And we can now run the C plus plus.
And there we have it the C plus plus.
You'll see that that's significantly faster than before when if I remember right it was about 0.2 seconds.
Uh, and that's because we've got these extra flags to really optimize this, uh, this code.
Um, and so you can see we are now more than 100 times faster between the two implementations.
And for sort of loop loopy mathy Math code, you would usually expect that a straight up compiled C
plus plus, uh, compiled natively like this can be, uh, at least 100 times faster.
So it's not not not too surprising, but it's a good result.
Um, and of course, the numbers are, uh, well, they're off by a teeny bit, which is probably which
is all down to floating point rounding stuff.
Let's see how Claude does and just confirm.
And let's run Claude's C plus plus code.
And it's very similar, of course.
Same number, similarly super blazingly fast.
Um, and it has generated, I'm sure, very similar uh, c plus plus code which is being compiled to
the same machine code I would expect.
All right.
One more, uh, flip to the next video, and then we will run for our hard problem through here.
Uh, and then we will call it a day, but it's been.
I hope you've enjoyed it as much as I have.
This is a great a great little product.
See you in a second.