Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,68 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from audio_recorder_streamlit import audio_recorder
|
| 3 |
-
import openai
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
def transcribe_text_to_voice(audio_location):
|
| 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 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 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.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|