Skip to content

srikanth-kandi/myGenAIChatBotSpeech

Repository files navigation

ChatBot with Open AI, LangChain and PlayHT

Main Handbook, This Handbook

Prerequisites
  • Existing Open AI Account

    • Check whether you have a limit to using the OpenAI API

    • The below screenshot indicated it is expired by June 1. So, you need to create a new account for free credits or purchase premium plan.

      Free trail expired

  • Creating OpenAI Account with New Mail or Mobile 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

      OpenAI Options

  • Create a HuggingFace Account

    • Open https://huggingface.co/

    • Click on Sign Up button

    • Enter your details and Click on Sign Up

    • Verify your email address

  • Create a New Space in HuggingFace Account

    • Click on Profile icon top right corner

      HuggingFace Profile Icon

    • Click on New Space

      HuggingFace New Space

    • Enter details of your new space

      HuggingFace New Space Details

  • Create PlayHT Account

    • Open https://play.ht/

    • Click on Log in

    • Since you don't have an account, go to https://play.ht/signup/

    • Choose your preferred Sign Up method

    • Account will be created and you will be redirected to your dashboard

  • Clone your Voice to PlayHT

    • Before cloning, get ready with a 45 second clean audio of your voice

    • Open https://play.ht/studio/voice-cloning/

    • Click on + Create a New Clone

    • Click on Instant

    • Enter Voice Name and Upload the File

    • Click on Create

    • Your voice will be cloned

  • Open the below provided Colab link

    Open In Colab

  • Copying Code to your Google Drive

    • On the top left corner of Google Colab Notebook you can find File, click on it

      Google Colab File

    • Click on Save a copy in Drive

      Google Colab Save a copy in Drive

    • If you are not logged in to your Google Account, please log into it

    • Once you are successfully logged in a new Google Colab Notebook with the given code will be opened

Install the Required Packages
  • Click on Play button to Install the Packages

    Google Colab Install Packages

Import the Required Packages to build the application
  • Click on Play button to Import the Packages

    Google Colab Import Packages

Assign API Keys and User IDs
Set OpenAI API Key to Environmental Variables
  • Click on Play button to set it as a Key

    OpenAI API Key Environmental Variable

Assign your Voice to a variable and set the URL to a variable
  • How to get your Voice ID

    • Go to https://docs.play.ht/reference/api-list-cloned-voices

    • In the right side section, select the language Python

      PlayHT Language

    • Enter Authorization as Bearer PLAY_HT_API_KEY replace PLAY_HT_API_KEY with your API Key refer here

      PlayHT Authorization

    • Enter X-USER-ID as your PLAY_HT_USER_ID refer here

      PlayHT User ID

    • Click on Try it!

      PlayHT Try it

    • If the Response says 200 then you have successfully created your Voice ID

      PlayHT Response

    • Copy the id from the Response like s3://.../.json

    • Now you can use that id in the get_payload as a value for voice

    • Replace PLAY_HT_VOICE_ID with your copied id

      PlayHT Voice ID

    • Click on Play button to set the values

      PlayHT Voice ID Play

Assigning the values for template, prompt, and memory
  • You can update the first line of the template You are a helpful assistant to answer user queries with <Prompts> given in Prompts and Examples

  • Click on Play button

    Google Colab Template Play

Initializing LLM Chain using OpenAI
  • Using ChatOpenAI method we are creating an LLM Chain

  • Click on Play button

    Google Colab LLM Chain Play

Setting headers
  • Click on Play button

    Google Colab Headers Play

Define functions to fetch Audio Response from PlayHT
  • Click on Play button

    Google Colab Functions Play

Define a function to generate the response for the question you ask
  • Click on Play button

    Google Colab Generate Response Play

Define a function to generate the Audio response and Create a ChatInterface using the Gradio
  • We are defining a chat_bot_response to return either an audio response format if audio file is fetched properly or returns a string response if it is an error

  • We are creating the ChatInterface from gradio and providing a function chat_bot_response and also examples

  • Check for other arguments here

  • Click on Play button to create an interface

    Google Colab Chat Interface Play

Launch your ChatBot with Gradio App
  • Click on Play button to launch the App

    Google Colab Launch App Play

Now you can try asking questions in your ChatBot and receive response as your Voice

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 app

    if __name__ == "__main__":
        demo.launch(debug = True)
  • You can add try and except block to handle the errors

    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

Publish your code to Hugging Face

Login to Hugging Face from Google Colab
  • Create a Hugging Face token and Copy

  • Click on Play button to enter Hugging Face Token

    Google Colab Hugging Face Token

  • Now paste the Hugging Face token in the textbox provided and click on Login

    Google Colab Hugging Face Token Login

Create Hugging Face API to push code from Google Colab
  • Click on Play button to create API

    Google Colab Hugging Face API

Adding Hugging Face Repository ID
  • Copy Hugging Face Repository ID by opening the Hugging Face Repo Created

    Hugging Face Repo ID

  • Replace your Repo ID

    Replace Hugging Face Repo ID

  • Click on Play button to assign Hugging Face Repo ID

    Replace Hugging Face Repo ID Play Button

Adding APIs to Hugging Face secrets
  • Click on Settings Button

    Hugging Face Settings Button

  • Go to Variables and secrets section

    Hugging Face Variables and secrets section

  • Click on New secret

    Hugging Face New secret

  • Enter Name as OPENAI_API_KEY and Value as your OpenAI API Key

    Hugging Face Name and Value

  • By the similar process create secrets for PLAY_HT_API_KEY, PLAY_HT_USER_ID and PLAY_HT_VOICE_ID

Load App and Requirements file
  • Click on Play button to download files

    Google Colab Download Files

  • You can check downloaded files in the left side panel

    Google Colab Downloaded Files

  • You can edit the downloaded files

    • Click on the app.py file

      Google Colab Edit app.py

    • The file will be opened and you can edit the template section by referring to Prompts and Examples

      Google Colab Modify app.py

    • After editing the file don't forget to save, by pressing ⌃ + S or ⌘ + S

Push your code to Hugging Face
  • Click on Play button

    Hugging Face Push Code

  • Now in your space, you should see it is Building status

    Hugging Face Building

  • On Succesful Build, you should see Running status

    Hugging Face Running

  • If the Build fails you will see a Runtime error with some errors below

    Hugging Face Runtime Error

  • Click on Logs to check more details about the error


Show some ❤️ by starring ⭐ this repository!

About

ChatBot with Open AI, LangChain and PlayHT

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published