Last active
March 16, 2023 06:33
-
-
Save Ishmam156/1db1146f59c398a1e33855c8bdba0659 to your computer and use it in GitHub Desktop.
Python script based on gradio library to create a quick website showcasing the ability to use audio to interact with ChatGPT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import config | |
from google.cloud import speech, texttospeech | |
import gradio as gr | |
import io | |
import openai | |
import os | |
from playsound import playsound | |
openai.api_key = config.OPENAI_API_KEY ## API Key from ChatGPT stored in a config.py file in same directory | |
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "./cloud_creds.json" ## Path to service account credentials JSON from Google Cloud Platform | |
# The text that will be spoken using playsound | |
text_for_speech = "" | |
ROLE_MAPPING = {"assistant": "ChatGPT", "user": "আপনি"} | |
# Initial message provided to ChatGPT | |
messages = [ | |
{ | |
"role": "system", | |
"content": "You are a helpful assistant. Respond to all input within 100 words.", | |
} | |
] | |
# Create a string representation of the messages list that can be displayed | |
def convert_messages_to_text(): | |
global messages | |
chat_transcript = "" | |
for message in messages: | |
if message["role"] != "system": | |
chat_transcript += ( | |
ROLE_MAPPING[message["role"]] + ": " + message["content"] + "\n\n" | |
) | |
return chat_transcript | |
# Convert the text to audio file and then play it | |
def play_audio(audio): | |
global text_for_speech | |
client = texttospeech.TextToSpeechClient() | |
synthesis_input = texttospeech.SynthesisInput(text=text_for_speech) | |
voice = texttospeech.VoiceSelectionParams( | |
language_code="bn-BD", ssml_gender=texttospeech.SsmlVoiceGender.FEMALE | |
) | |
audio_config = texttospeech.AudioConfig( | |
audio_encoding=texttospeech.AudioEncoding.MP3 | |
) | |
response = client.synthesize_speech( | |
input=synthesis_input, voice=voice, audio_config=audio_config | |
) | |
with open("output.mp3", "wb") as out: | |
out.write(response.audio_content) | |
print('Audio content written to file "output.mp3"') | |
playsound("./output.mp3") | |
chat_transcript = convert_messages_to_text() | |
return chat_transcript | |
# Receive response from ChatGPT and add it to the global messages list | |
def get_response_from_chatgpt(text): | |
global text_for_speech, messages | |
messages.append({"role": "user", "content": text}) | |
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages) | |
system_message = response["choices"][0]["message"] | |
messages.append(system_message) | |
chat_transcript = convert_messages_to_text() | |
text_for_speech = system_message["content"] | |
return chat_transcript | |
# Converted recorded audio into text | |
def get_text_from_speech(audio_path): | |
client = speech.SpeechClient() | |
if not audio_path: | |
return None | |
with io.open(audio_path, "rb") as audio_file: | |
content = audio_file.read() | |
audio = speech.RecognitionAudio(content=content) | |
config = speech.RecognitionConfig( | |
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16, | |
sample_rate_hertz=48000, | |
language_code="bn-BD", | |
) | |
response = client.recognize(config=config, audio=audio) | |
transcript = "" | |
for result in response.results: | |
transcript += result.alternatives[0].transcript | |
return transcript | |
def transcribe(audio): | |
transcript = get_text_from_speech(audio) | |
if not transcript: | |
return "অনুগ্রহ করে আপনার প্রশ্নটি আবার রেকর্ড করুন এবং সাবমিট বাটন ক্লিক করুন" | |
chat_transcript = get_response_from_chatgpt(transcript) | |
return chat_transcript | |
with gr.Blocks(title="বাংলায় ChatGPT", css="h1, p {text-align: center}") as demo: | |
gr.Markdown( | |
""" | |
# বাংলায় কথা বলুন এবং উত্তর পান ChatGPT থেকে! | |
এই ওয়েবসাইট ব্যাবহার করে আপনি বাংলায় আপনার অডিও রেকর্ড করে ChatGPT কে পাঠাতে পারবেন এবং AI থেকে বাংলায় উত্তর পাবেন! | |
""" | |
) | |
audio = gr.Audio( | |
source="microphone", type="filepath", label="আপনার প্রশ্ন রেকর্ড করুন" | |
) | |
submit_button = gr.Button("আপনার প্রশ্ন জমা দিন!") | |
output = gr.Textbox( | |
label="chatgpt থেকে উত্তর:", | |
placeholder="উত্তর এখানে দেখানো হবে", | |
) | |
audio_play = gr.Button("উত্তরের অডিও শুনতে ক্লিক করুন!") | |
submit_button.click(fn=transcribe, inputs=audio, outputs=output) | |
audio_play.click(fn=play_audio, inputs=audio, outputs=output) | |
demo.launch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment