Skip to main content

openai-gradio

is a Python package that makes it very easy for developers to create machine learning apps that are powered by OpenAI's API.

Installation

You can install openai-gradio directly using pip:

pip install openai-gradio

That's it!

Basic Usage

Just like if you were to use the openai API, you should first save your OpenAI API key to this environment variable:

export OPENAI_API_KEY=<your token>

Then in a Python file, write:

import gradio as gr
import openai_gradio

gr.load(
    name='gpt-4-turbo',
    src=openai_gradio.registry,
).launch()

Run the Python file, and you should see a Gradio Interface connected to the model on OpenAI!

ChatInterface

Voice Chat

OpenAI-Gradio also supports voice chat capabilities. You can enable this in two ways:

  1. Using a realtime model:
import gradio as gr
import openai_gradio

gr.load(
    name='gpt-4o-realtime-preview-2024-10-01',
    src=openai_gradio.registry
).launch()
  1. Explicitly enabling voice chat with any realtime model:
import gradio as gr
import openai_gradio

gr.load(
    name='gpt-4o-mini-realtime-preview-2024-12-17',
    src=openai_gradio.registry,
    enable_voice=True
).launch()

This will create a WebRTC-based interface that allows for real-time voice conversations with the AI model.

Voice Chat API Keys

For voice chat functionality, you'll need:

  1. OpenAI API key (required for all features):
export OPENAI_API_KEY=<your OpenAI token>
  1. Twilio credentials (required for WebRTC voice chat):
export TWILIO_ACCOUNT_SID=<your Twilio account SID>
export TWILIO_AUTH_TOKEN=<your Twilio auth token>

You can get Twilio credentials by:

  1. Creating a free account at Twilio
  2. Finding your Account SID and Auth Token in the Twilio Console

Without Twilio credentials, the voice chat will still work but might have connectivity issues in some network environments.

Customization

Once you can create a Gradio UI from an OpenAI endpoint, you can customize it by setting your own input and output components, or any other arguments to gr.Interface. For example, the screenshot below was generated with:

import gradio as gr
import openai_gradio

gr.load(
    name='gpt-4-turbo',
    src=openai_gradio.registry,
    title='OpenAI-Gradio Integration',
    description="Chat with GPT-4-turbo model.",
    examples=["Explain quantum gravity to a 5-year old.", "How many R are there in the word Strawberry?"]
).launch()

ChatInterface with customizations

Composition

Or use your loaded Interface within larger Gradio Web UIs, e.g.

import gradio as gr
import openai_gradio

with gr.Blocks() as demo:
    with gr.Tab("GPT-4-turbo"):
        gr.load('gpt-4-turbo', src=openai_gradio.registry)
    with gr.Tab("GPT-3.5-turbo"):
        gr.load('gpt-3.5-turbo', src=openai_gradio.registry)

demo.launch()

Under the Hood

The openai-gradio Python library has two dependencies: openai and gradio. It defines a "registry" function openai_gradio.registry, which takes in a model name and returns a Gradio app.

Supported Models in OpenAI

All chat API models supported by OpenAI are compatible with this integration. For a comprehensive list of available models and their specifications, please refer to the OpenAI Models documentation.


Note: if you are getting a 401 authentication error, then the OpenAI API Client is not able to get the API token from the environment variable. This happened to me as well, in which case save it in your Python session, like this:

import os

os.environ["OPENAI_API_KEY"] = ...

Metadata

Release files for openai-gradio 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openai-gradio 0.0.6
File Size Uploaded Provenance
openai_gradio-0.0.6.tar.gz 110.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for openai-gradio 0.0.6
File Interpreter ABI Platform
openai_gradio-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 115.3 kB

Release files / openai_gradio-0.0.6.tar.gz

Download URL openai_gradio-0.0.6.tar.gz
Size 110.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a01c6998e89fe33bab235bb4dd1811849ef88fc230c8d21bdf8605c6f1fa1b1e
BLAKE2b-256 checksum
How to use checksums
758bd7a9bb49d09703031eef313a051a630e4bf0ad943f99a2ce59c151072294
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.6

Release files / openai_gradio-0.0.6-py3-none-any.whl

Download URL openai_gradio-0.0.6-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
465b6a79eb3fb86a60fe59d5e1a860432061e16607fa1db339237ee6f351cece
BLAKE2b-256 checksum
How to use checksums
c53ba2764e9a1b92adf0ff52d9c01eefa0b74765fb417513944c1ee531b010f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.9.6

Security for openai-gradio 0.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page