Spaces:
Build error
Build error
YeBhoneLin10
commited on
Commit
•
04d366d
1
Parent(s):
75f626d
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pyttsx3
|
3 |
+
import speech_recognition as sr
|
4 |
+
import openai
|
5 |
+
|
6 |
+
openai.api_key = 'sk-proj-GaLPmq1cEwOIoOctb8sXT3BlbkFJGiU2jNLx4c5ibssFFLUp'
|
7 |
+
def chat_gpt(prompt):
|
8 |
+
response = openai.ChatCompletion.create(
|
9 |
+
model='gpt-3.5-turbo',
|
10 |
+
messages = [{'role':'user','content': prompt}]
|
11 |
+
)
|
12 |
+
return response.choices[0].message.content.strip()
|
13 |
+
|
14 |
+
choose = st.sidebar.radio('Options to ask', ('Voice', 'Text'))
|
15 |
+
|
16 |
+
def record_audio():
|
17 |
+
r = sr.Recognizer()
|
18 |
+
with sr.Microphone() as source:
|
19 |
+
st.write("Say something...")
|
20 |
+
audio = r.listen(source)
|
21 |
+
return r.recognize_google(audio)
|
22 |
+
|
23 |
+
def speak_text(text):
|
24 |
+
engine = pyttsx3.init()
|
25 |
+
engine.say(text)
|
26 |
+
engine.runAndWait()
|
27 |
+
|
28 |
+
if choose == 'Voice':
|
29 |
+
user_input = st.button("Start Recording")
|
30 |
+
if user_input:
|
31 |
+
text = record_audio()
|
32 |
+
st.write("You: ", text)
|
33 |
+
output = chat_gpt(text)
|
34 |
+
st.write("Bot: ", output)
|
35 |
+
|
36 |
+
speak_text(output)
|
37 |
+
else:
|
38 |
+
text = st.text_input(' ')
|
39 |
+
if text:
|
40 |
+
output = chat_gpt(str(text))
|
41 |
+
st.write("Bot: ", output)
|
42 |
+
speak_text(output)
|