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
- Host: GitHub
- URL: https://github.com/srikanth-kandi/mygenaichatbotspeech
- Owner: srikanth-kandi
- License: apache-2.0
- Created: 2023-07-30T17:03:13.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-10T08:33:38.000Z (over 1 year ago)
- Last Synced: 2025-02-08T07:27:42.708Z (3 months ago)
- Language: Jupyter Notebook
- Homepage: https://srigenaispeech.ccbp.tech
- Size: 1.64 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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.

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

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

- Click on `New Space`

- Enter details of your new space

- **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**
- **Copying Code to your Google Drive**
- On the top left corner of Google Colab Notebook you can find `File`, click on it

- Click on `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

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

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

- 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

- Replace `PLAY_HT_API_KEY` with Secret Key you copied

- Replace `PLAY_HT_USER_ID` with User ID you copied

Set OpenAI API Key to Environmental Variables
- Click on Play button to set it as a Key

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`

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

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

- Click on `Try it!`

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

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

- Click on Play button to set the values

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

Initializing LLM Chain using OpenAI
- Using `ChatOpenAI` method we are creating an LLM Chain
- Click on Play button

Setting headers
- Click on Play button

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

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

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

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

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

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

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

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

- Replace your Repo ID

- Click on Play button to assign Hugging Face Repo ID

Adding APIs to Hugging Face secrets
- Click on Settings Button

- Go to `Variables and secrets` section

- Click on `New secret`

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

- 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

- You can check downloaded files in the left side panel

- You can edit the downloaded files
- Click on the `app.py` file

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

- After editing the file don't forget to save, by pressing `⌃ + S` or `⌘ + S`
Push your code to Hugging Face
- Click on Play button

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

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

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

- Click on Logs to check more details about the `error`
---
Show some ❤️ by starring ⭐ this repository!