Prerequisites
-
Existing Open AI Account:
-
Check whether you have a limit to using the Open AI API
-
The below screenshot indicated it is expired by June 1. So, you need to create a new account.
-
Check your API key usage here
-
-
Creating Open AI Account with New Mail and New Phone Number:
-
Open https://openai.com
-
Click on
Sign Up
button -
Choose your preffered Sign Up method
-
After logged in click here to see below options
-
-
Create a Hugging Face Account
-
Click on
Sign Up
button -
Enter your details and Click on
Sign Up
button -
Verify your email address
-
Create a New Space in Hugging Face Account
-
Open the below provided Colab link
-
Copying Code to your Google Drive
Importing the required packages
Get the OpenAI API Key and set it as environmental variable
-
Generate API Key
-
Click on
+ Create new secret key
-
Copy the Secret Key for your use
-
Replace your
OPENAI_API_KEY
with your own API Key -
Click on the Play button
Assigning the values for template, prompt, and memory
-
You can update the first line of the template with Prompts and Examples provided
-
Click on the Play button
Initializing LLM Chain using OpenAI
-
Using
ChatOpenAI
method we are creating an LLM Chain -
Click on the Play button
Define a function to generate the response for the question you ask
Create a Chat Interface using Gradio
-
We are creating the
ChatInterface
from gradio and providing a functionget_text_response
and also examples from Prompts and Examples provided -
Check for other arguments here
-
Click on the Play button to create an interface
If you are getting any errors
-
Keep print statements to identify the issue
-
To identify the error you are getting please add
debug = True
while launching the gradio appif __name__ == "__main__": demo.launch(debug = True)
def get_text_response(user_message,history): try: response = llm_chain.predict(user_message = user_message) except Exception as e: print("Error:", e) try: print("Error:", e.error.message) response = "Failed to reply: " + e.error.message except Exception as e: response = "Failed to reply" return response
Login to Hugging Face from Google Colab
-
Create a Hugging Face token and Copy
-
Login to Hugging Face https://huggingface.co/
-
Click on
New token
-
Add a Name for the Token
-
Choose
write
Role for the Token -
Click on
Generate a token
-
Copy the Token
-
Click on Play button to enter Hugging Face Token
-
Now paste the Hugging Face token in the textbox provided and click on
Login
-
Adding Hugging Face Repository ID
Add OPENAI_API_KEY in Hugging Face Repository secrets
Load Application and Requirement files
Editing the Application file
-
Click on the
app.py
file -
The file will be opened and you can edit the
<Prompt>
and<Example>
as per Prompts and Examples and save using⌃ + S
or⌘ + S