Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/srikanth-kandi/mygenaichatbotspeech

ChatBot with Open AI, LangChain and PlayHT
https://github.com/srikanth-kandi/mygenaichatbotspeech

Last synced: about 1 month ago
JSON representation

ChatBot with Open AI, LangChain and PlayHT

Awesome Lists containing this project

README

        

# ChatBot with Open AI, LangChain and PlayHT

[Main Handbook](https://inky-ironclad-8d2.notion.site/Generative-AI-Workshop-bfb0123ccf6945ebbfa5bf3328917423), [This Handbook](https://inky-ironclad-8d2.notion.site/ChatBot-with-Open-AI-LangChain-and-PlayHT-b518566d30194da093bd1b21f82085a4)

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](./images/free-trail-expired.png)

- **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](./images/open-ai-options.png)

- **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](./images/hugging-face-profile-icon.png)

- Click on `New Space`

![HuggingFace New Space](./images/hugging-face-new-space.png)

- Enter details of your new space

![HuggingFace New Space Details](./images/hugging-face-new-space-details.png)

- **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](./images/google-colab-file-section.png)

- Click on `Save a copy in Drive`

![Google Colab Save a copy in Drive](./images/google-colab-save-a-copy-in-drive.png)

- 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](./images/google-colab-install-packages.png)

Import the Required Packages to build the application

- Click on Play button to Import the Packages

![Google Colab Import Packages](./images/google-colab-import-packages.png)

Assign API Keys and User IDs

- How to get OpenAI API Key?

- Go to https://platform.openai.com/account/api-keys

- Click on `+ Create new secret key`

- Copy the secret key for your use

- Assign OpenAI API Key

- Replace `OPENAI_API_KEY` with your OpenAI API Key

![Google Colab OpenAI API Key](./images/google-colab-openai-api-key.png)

- How to get PlayHT User ID and PlayHT API Key?

- Login to PlayHT - https://play.ht/

- Go to https://play.ht/studio/api-access/

- Generate the Secret Key

![PlayHT Secret Key](./images/play-ht-secret-key.png)

- Replace `PLAY_HT_API_KEY` with Secret Key you copied

![PlayHT Secret Key](./images/play-ht-api-key.png)

- Replace `PLAY_HT_USER_ID` with User ID you copied

![PlayHT User ID](./images/play-ht-user-id.png)

Set OpenAI API Key to Environmental Variables

- Click on Play button to set it as a Key

![OpenAI API Key Environmental Variable](./images/openai-api-key-environ.png)

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](./images/play-ht-language.png)

- Enter Authorization as `Bearer PLAY_HT_API_KEY` replace `PLAY_HT_API_KEY` with your API Key refer [here](#playht-ids)

![PlayHT Authorization](./images/play-ht-auth-api-key.png)

- Enter X-USER-ID as your `PLAY_HT_USER_ID` refer [here](#playht-ids)

![PlayHT User ID](./images/play-ht-user-id-key.png)

- Click on `Try it!`

![PlayHT Try it](./images/play-ht-try-it.png)

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

![PlayHT Response](./images/play-ht-response.png)

- 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](./images/play-ht-voice-id.png)

- Click on Play button to set the values

![PlayHT Voice ID Play](./images/play-ht-voice-id-play.png)

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 `` given in Prompts and Examples

- Click on Play button

![Google Colab Template Play](./images/google-colab-template-play.png)

Initializing LLM Chain using OpenAI

- Using `ChatOpenAI` method we are creating an LLM Chain

- Click on Play button

![Google Colab LLM Chain Play](./images/google-colab-llm-chain-play.png)

Setting headers

- Click on Play button

![Google Colab Headers Play](./images/google-colab-headers-play.png)

Define functions to fetch Audio Response from PlayHT

- Click on Play button

![Google Colab Functions Play](./images/google-colab-get-payload-play.png)

Define a function to generate the response for the question you ask

- Click on Play button

![Google Colab Generate Response Play](./images/google-colab-gettext-response-play.png)

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](https://www.gradio.app/docs/chatinterface)

- Click on Play button to create an interface

![Google Colab Chat Interface Play](./images/google-colab-chat-interface-play.png)

Launch your ChatBot with Gradio App

- Click on Play button to launch the App

![Google Colab Launch App Play](./images/google-colab-launch-app-play.png)

### 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

```python
if __name__ == "__main__":
demo.launch(debug = True)
```

- You can add try and except block to handle the errors

```python
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

- Login to Hugging Face https://huggingface.co/

- Open https://huggingface.co/settings/tokens

- 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

![Google Colab Hugging Face Token](./images/google-colab-hugging-face-token.png)

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

![Google Colab Hugging Face Token Login](./images/google-colab-hugging-face-token-login.png)

Create Hugging Face API to push code from Google Colab

- Click on Play button to create API

![Google Colab Hugging Face API](./images/google-colab-hugging-face-api.png)

Adding Hugging Face Repository ID

- Copy Hugging Face Repository ID by opening the Hugging Face Repo Created

![Hugging Face Repo ID](./images/hugging-face-repo-id.png)

- Replace your Repo ID

![Replace Hugging Face Repo ID](./images/replace-hugging-face-repo-id.png)

- Click on Play button to assign Hugging Face Repo ID

![Replace Hugging Face Repo ID Play Button](./images/replace-hugging-face-repo-id-play.png)

Adding APIs to Hugging Face secrets

- Click on Settings Button

![Hugging Face Settings Button](./images/hugging-face-settings-button.png)

- Go to `Variables and secrets` section

![Hugging Face Variables and secrets section](./images/hugging-face-variables-and-secrets-section.png)

- Click on `New secret`

![Hugging Face New secret](./images/hugging-face-new-secret.png)

- Enter Name as `OPENAI_API_KEY` and Value as your OpenAI API Key

![Hugging Face Name and Value](./images/hugging-face-name-and-value.png)

- 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](./images/google-colab-download-files.png)

- You can check downloaded files in the left side panel

![Google Colab Downloaded Files](./images/google-colab-downloaded-files.png)

- You can edit the downloaded files

- Click on the `app.py` file

![Google Colab Edit app.py](./images/google-colab-edit-app-py.png)

- The file will be opened and you can edit the `template` section by referring to [Prompts and Examples](./prompts-and-examples.md)

![Google Colab Modify app.py](./images/google-colab-modify-app-py.png)

- 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](./images/hugging-face-push-code.png)

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

![Hugging Face Building](./images/hugging-face-building.png)

- On Succesful Build, you should see `Running` status

![Hugging Face Running](./images/hugging-face-running.png)

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

![Hugging Face Runtime Error](./images/hugging-face-runtime-error.png)

- Click on Logs to check more details about the `error`

---

Show some ❤️ by starring ⭐ this repository!