shukdevdatta123 commited on
Commit
b76d0bb
Β·
verified Β·
1 Parent(s): e0a6679

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -57
app.py CHANGED
@@ -1,63 +1,68 @@
1
  import streamlit as st
2
  from audio_recorder_streamlit import audio_recorder
3
- import openai # Corrected import
4
 
5
- API_KEY = 'enter-openai-api-key-here'
 
6
 
7
- # Set the API key
8
- openai.api_key = API_KEY
 
9
 
10
- def transcribe_text_to_voice(audio_location):
11
- # Transcribe audio to text using Whisper API
12
- with open(audio_location, "rb") as audio_file:
13
- transcript = openai.Audio.transcriptions.create(
14
- model="whisper-1", file=audio_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  )
16
- return transcript['text']
17
-
18
- def chat_completion_call(text):
19
- # Send the text to GPT-3.5 for chat completion
20
- response = openai.ChatCompletion.create(
21
- model="gpt-3.5-turbo-1106",
22
- messages=[{"role": "user", "content": text}]
23
- )
24
- return response['choices'][0]['message']['content']
25
-
26
- def text_to_speech_ai(speech_file_path, api_response):
27
- # Convert the text to speech using OpenAI's TTS API
28
- response = openai.Audio.speech.create(
29
- model="text-to-speech-1", # TTS model name may differ
30
- voice="nova", # Specify a voice (choose one that is available)
31
- input=api_response
32
- )
33
- # Save the speech response to a file
34
- with open(speech_file_path, "wb") as f:
35
- f.write(response['audio'])
36
-
37
- st.title("πŸ§‘β€πŸ’» Skolo Online πŸ’¬ Talking Assistant")
38
-
39
- """
40
- HiπŸ€– just click on the voice recorder and let me know how I can help you today?
41
- """
42
-
43
- audio_bytes = audio_recorder()
44
- if audio_bytes:
45
- # Save the Recorded Audio File
46
- audio_location = "audio_file.wav"
47
- with open(audio_location, "wb") as f:
48
- f.write(audio_bytes)
49
-
50
- # Transcribe the saved file to text
51
- text = transcribe_text_to_voice(audio_location)
52
- st.write(text)
53
-
54
- # Get AI response from GPT-3.5
55
- api_response = chat_completion_call(text)
56
- st.write(api_response)
57
-
58
- # Convert the response to speech and save it as a file
59
- speech_file_path = 'audio_response.mp3'
60
- text_to_speech_ai(speech_file_path, api_response)
61
-
62
- # Play the audio response in the app
63
- st.audio(speech_file_path)
 
1
  import streamlit as st
2
  from audio_recorder_streamlit import audio_recorder
3
+ import openai
4
 
5
+ # Prompt the user to input their OpenAI API key
6
+ API_KEY = st.text_input("Enter your OpenAI API Key", type="password")
7
 
8
+ # Check if the API key is provided
9
+ if API_KEY:
10
+ openai.api_key = API_KEY # Set the API key
11
 
12
+ def transcribe_text_to_voice(audio_location):
13
+ # Transcribe audio to text using Whisper API
14
+ with open(audio_location, "rb") as audio_file:
15
+ transcript = openai.Audio.transcriptions.create(
16
+ model="whisper-1", file=audio_file
17
+ )
18
+ return transcript['text']
19
+
20
+ def chat_completion_call(text):
21
+ # Send the text to GPT-3.5 for chat completion
22
+ response = openai.ChatCompletion.create(
23
+ model="gpt-3.5-turbo-1106",
24
+ messages=[{"role": "user", "content": text}]
25
+ )
26
+ return response['choices'][0]['message']['content']
27
+
28
+ def text_to_speech_ai(speech_file_path, api_response):
29
+ # Convert the text to speech using OpenAI's TTS API
30
+ response = openai.Audio.speech.create(
31
+ model="text-to-speech-1", # TTS model name may differ
32
+ voice="nova", # Specify a voice (choose one that is available)
33
+ input=api_response
34
  )
35
+ # Save the speech response to a file
36
+ with open(speech_file_path, "wb") as f:
37
+ f.write(response['audio'])
38
+
39
+ # Streamlit interface for voice assistant
40
+ st.title("πŸ§‘β€πŸ’» Skolo Online πŸ’¬ Talking Assistant")
41
+ """
42
+ HiπŸ€– just click on the voice recorder and let me know how I can help you today?
43
+ """
44
+
45
+ audio_bytes = audio_recorder()
46
+ if audio_bytes:
47
+ # Save the Recorded Audio File
48
+ audio_location = "audio_file.wav"
49
+ with open(audio_location, "wb") as f:
50
+ f.write(audio_bytes)
51
+
52
+ # Transcribe the saved file to text
53
+ text = transcribe_text_to_voice(audio_location)
54
+ st.write("You said:", text)
55
+
56
+ # Get AI response from GPT-3.5
57
+ api_response = chat_completion_call(text)
58
+ st.write("AI says:", api_response)
59
+
60
+ # Convert the response to speech and save it as a file
61
+ speech_file_path = 'audio_response.mp3'
62
+ text_to_speech_ai(speech_file_path, api_response)
63
+
64
+ # Play the audio response in the app
65
+ st.audio(speech_file_path)
66
+
67
+ else:
68
+ st.warning("Please enter your OpenAI API key to continue.")