Spaces:
Build error
Build error
import streamlit as st | |
import pyttsx3 | |
import speech_recognition as sr | |
import openai | |
openai.api_key = 'sk-proj-GaLPmq1cEwOIoOctb8sXT3BlbkFJGiU2jNLx4c5ibssFFLUp' | |
def chat_gpt(prompt): | |
response = openai.ChatCompletion.create( | |
model='gpt-3.5-turbo', | |
messages = [{'role':'user','content': prompt}] | |
) | |
return response.choices[0].message.content.strip() | |
choose = st.sidebar.radio('Options to ask', ('Voice', 'Text')) | |
def record_audio(): | |
r = sr.Recognizer() | |
with sr.Microphone() as source: | |
st.write("Say something...") | |
audio = r.listen(source) | |
return r.recognize_google(audio) | |
def speak_text(text): | |
engine = pyttsx3.init() | |
engine.say(text) | |
engine.runAndWait() | |
if choose == 'Voice': | |
user_input = st.button("Start Recording") | |
if user_input: | |
text = record_audio() | |
st.write("You: ", text) | |
output = chat_gpt(text) | |
st.write("Bot: ", output) | |
speak_text(output) | |
else: | |
text = st.text_input(' ') | |
if text: | |
output = chat_gpt(str(text)) | |
st.write("Bot: ", output) | |
speak_text(output) |