Log in to GraphQL Editor
Python models in GraphQL with Rust backend in 1 line of code
DJ_Fresh

Dan Stein - DJ Fresh

1/23/2023

Python models in GraphQL with Rust backend in 1 line of code

As 2022 drew to a close, and the distant stars of a million AI generated skies were everywhere you looked on your Galaxy Fold, I was doing some consulting for Stability.ai, helping with ideas for how to publish APIs around generative text to image models. While thinking about how researchers prototype their work, I realised that GraphQL was the perfect tool for quickly prototyping and sharing models.

The Python package inspired by these conversations, created with Chris Bishop and now called FastQL allows you to easily prototype and share models like Stable Diffusion, Midjourney or RunwayML in seconds. FastQL can be used to serve up any model or Python function and thanks to it’s use of Rust under the hood and the powerful Actix web framework, FastQL is lightning fast and keeps your API separate from your model code.

“FastQL allows you to easily prototype and share models like Stable Diffusion, Midjourney or RunwayML in seconds”

FastQL is incredibly simple to use. We’ve included simple steps and Docker files that allow you to quickly spin up example Hugging face diffusion models and start running them on AWS in minutes. We even included a fun prototype that allows you to fine tune Hugging Face diffusers models on your own GDrive photo uploads. You can find the FastQL package on pypi with code and resources on Github. Go ahead and install FastQL with

pip install fastqlapi.

This is how simple it is to spin up a blazing fast GraphQL API around your model…

from fastqlapi import fastql_server

def my_callback(**kwargs):
  input = kwargs[‘input’]
  # do some transformation to your input params
return {‘output’: [f”https://s3.amazonaws.com/fastql/{input}”]}

fastql_server.start(
  callback=my_callback,
  query_name="Model",
  args={"input": { "type": "String!", "description": "This is my input param"}},
  fields={"output": { "type": "[String]", "description": "An array of image links"}
 })

First define your schema as a Python dictionary, give your GraphQL query a name and pass:
fastql_server.start a callback to call when a GraphQL request arrives. Then pull out your defined arguments from the request, work your magic on them and finally return a dict containing the fields you’ve made available for the response.

When you run your code, a GraphQL server matching your schema definition will spin up automatically in a separate process.

Configuration for the API server is all environment variables. In development a GraphQL Playground UI will be served up at localhost:8000/graphql for you to test your model.

djfreshblog2.webp

If you haven’t tried GraphQL yet, you are seriously missing out! Originally created by Facebook, GraphQL’s popularity has been exploding with it being involved in 22.7% of application integrations. According to the last dZone survey in 2020 usage of GraphQL had skyrocketed from its inception in 2018 to over 47% and in 2022 Apollo GraphQL was among the top 500 fastest growing tech companies. GraphQL is used in production by Meta, Airbnb, Adobe, Netflix, Twitter, Paypal, Github and over 2000 other large enterprise companies.

djfreshblog3.webp

“In our benchmarks FastQL performance was nearly twice as fast as an Ariadne and FastAPI implementation with the same schema.”

Along with the many benefits of GraphQL such as the incredible developer experience and tools like GraphQL playground, Hasura and with auto-documentation out of the box. FastQL also works really well with Apollo Federation, enabling you to mix fixed and dynamic schemas to publish a federated API, aggregating multiple models into a single supergraph that is secure, simple to version and easy to extend.

The performance of Actix which is currently TechEmpower’s no.5 overall fastest web framework improves on the speed of native Python implementations and in our benchmarks FastQL performance was nearly twice as fast as an Ariadne and FastAPI implementation with the same schema, achieving sub 20ms response times according to Apollo tracing (available to activate by env variable in FastQL).

This is just the beginning…

djfreshblog4.webp

This is a passion project, we’d love your help! Message me on Twitter @djfreshuk if you want to get involved ❤️

Thanks to the folks at Huggingface, ActiX, [email protected], the folks at Maturin and Nin, Danjo & everybody from the Stable Diffusion community for their help, libraries and inspiration!

Check out our other blogposts

Juniper - fast & type-safe GraphQL Servers in Rust
Tomek Poniatowicz
Tomek Poniatowicz
Juniper - fast & type-safe GraphQL Servers in Rust
2 min read
over 3 years ago
Conditional rendering in React
Michał Tyszkiewicz
Michał Tyszkiewicz
Conditional rendering in React
7 min read
6 months ago
Damn Vulnerable GraphQL Application
Michał Tyszkiewicz
Michał Tyszkiewicz
Damn Vulnerable GraphQL Application
2 min read
almost 3 years ago

Ready for take-off?

Elevate your work with our editor that combines world-class visual graph, documentation and API console

Get Started with GraphQL Editor