File size: 1,145 Bytes
31cb32f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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)