Skip to content

Instantly share code, notes, and snippets.

@mdwoicke
Forked from skolo-online/talking_assistant.py
Created December 31, 2023 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdwoicke/9d8b2822e190159303ab0f25905bbd0b to your computer and use it in GitHub Desktop.
Save mdwoicke/9d8b2822e190159303ab0f25905bbd0b to your computer and use it in GitHub Desktop.
Talking AI Assistant Application on Streamlit
# pip install streamlit
# pip install audio-recorder-streamlit
# pip install openai
import streamlit as st
from audio_recorder_streamlit import audio_recorder
from openai import OpenAI
API_KEY = 'enter-openai-api-key-here'
def transcribe_text_to_voice(audio_location):
client = OpenAI(api_key=API_KEY)
audio_file= open(audio_location, "rb")
transcript = client.audio.transcriptions.create(model="whisper-1", file=audio_file)
return transcript.text
def chat_completion_call(text):
client = OpenAI(api_key=API_KEY)
messages = [{"role": "user", "content": text}]
response = client.chat.completions.create(model="gpt-3.5-turbo-1106", messages=messages)
return response.choices[0].message.content
def text_to_speech_ai(speech_file_path, api_response):
client = OpenAI(api_key=API_KEY)
response = client.audio.speech.create(model="tts-1",voice="nova",input=api_response)
response.stream_to_file(speech_file_path)
st.title("🧑‍💻 Skolo Online 💬 Talking Assistant")
"""
Hi🤖 just click on the voice recorder and let me know how I can help you today?
"""
audio_bytes = audio_recorder()
if audio_bytes:
##Save the Recorded File
audio_location = "audio_file.wav"
with open(audio_location, "wb") as f:
f.write(audio_bytes)
#Transcribe the saved file to text
text = transcribe_text_to_voice(audio_location)
st.write(text)
#Use API to get an AI response
api_response = chat_completion_call(text)
st.write(api_response)
# Read out the text response using tts
speech_file_path = 'audio_response.mp3'
text_to_speech_ai(speech_file_path, api_response)
st.audio(speech_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment