0% found this document useful (0 votes)
19 views1 page

Peek

The document outlines the setup of a Gradio interface for interacting with OpenAI's GPT-4 model, incorporating libraries for API interaction and environment variable management. It includes functionalities for loading API keys, rate limiting, and API key verification to manage usage effectively. The interface allows user interaction with a defined prompt and provides responses while ensuring regulated access through credit tracking and rate limits.

Uploaded by

scott.abuya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views1 page

Peek

The document outlines the setup of a Gradio interface for interacting with OpenAI's GPT-4 model, incorporating libraries for API interaction and environment variable management. It includes functionalities for loading API keys, rate limiting, and API key verification to manage usage effectively. The interface allows user interaction with a defined prompt and provides responses while ensuring regulated access through credit tracking and rate limits.

Uploaded by

scott.abuya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Importing Libraries:

openai: Library to interact with OpenAI's API.


gradio: Library to create web-based interfaces.
os: Library to interact with the operating system.
load_dotenv: Function to load environment variables from a .env file.
Header, HTTPException, Depends: Classes from FastAPI for handling HTTP headers,
exceptions, and dependencies.
datetime, timedelta: Classes for handling date and time.
Loading Environment Variables:
load_dotenv(): Loads environment variables from a .env file into the environment.
1 vulnerability
Loading OpenAI API Key:
os.getenv("Openai_API_Key"): Retrieves the OpenAI API key from the environment
variables.
openai.api_key: Sets the OpenAI API key for the openai library.
Setting API Key Credits:
api_key_credits: Dictionary to store the number of credits (requests) available for
each API key.
Rate Limiting Configuration:
rate_limit: Maximum number of allowed requests within the specified time window.
time_window: Time window for rate limiting (1 minute).
In-Memory Store for API Usage:
api_usage: Dictionary to track the timestamps of API requests for each API key.
API Key Verification Function:
verify_api_key: Function to verify the API key.
x_api_key: API key passed in the HTTP header.
Checks if the API key has sufficient credits. If not, raises an HTTPException with
status code 401.
Rate Limiting Function:
rate_limiter: Function to enforce rate limiting.
x_api_key: API key passed in the HTTP header.
Tracks the timestamps of API requests and raises an HTTPException with status code
429 if the rate limit is exceeded.
Chat Function:
chat_with_gpt4: Function to interact with the GPT-4 model.
prompt: User's input prompt.
x_api_key: API key passed in the HTTP header.
Applies rate limiting using the rate_limiter function.
Calls the OpenAI API to generate a response.
Deducts a token from the API key credits if the request is successful.
Returns the AI's response or an error message if an exception occurs.
Creating the Gradio Interface:
gr.Interface: Creates a Gradio interface.
fn: Function to be called when the user interacts with the interface
(chat_with_gpt4).
inputs: Type of input expected from the user (text).
outputs: Type of output to be displayed (text).
title: Title of the interface (TUTOR).
description: Description of the interface (Chat with TUTOR).
theme: Theme of the interface (default).
Launching the Gradio Interface:
iface.launch: Launches the Gradio interface.
share=True: Allows the interface to be shared publicly.
enable_queue=True: Enables the queue for handling multiple requests.
favicon_path="./hf-logo.svg": Sets the favicon for the PWA.
This code sets up a Gradio interface to interact with OpenAI's GPT-4 model, with
rate limiting and API key verification to ensure regulated use of the API.

You might also like