File size: 2,436 Bytes
afe4a62
9c931f4
b76d0bb
afe4a62
b76d0bb
 
afe4a62
b76d0bb
 
 
afe4a62
b76d0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0a6679
b76d0bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import streamlit as st
from audio_recorder_streamlit import audio_recorder
import openai

# Prompt the user to input their OpenAI API key
API_KEY = st.text_input("Enter your OpenAI API Key", type="password")

# Check if the API key is provided
if API_KEY:
    openai.api_key = API_KEY  # Set the API key

    def transcribe_text_to_voice(audio_location):
        # Transcribe audio to text using Whisper API
        with open(audio_location, "rb") as audio_file:
            transcript = openai.Audio.transcriptions.create(
                model="whisper-1", file=audio_file
            )
        return transcript['text']

    def chat_completion_call(text):
        # Send the text to GPT-3.5 for chat completion
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo-1106", 
            messages=[{"role": "user", "content": text}]
        )
        return response['choices'][0]['message']['content']

    def text_to_speech_ai(speech_file_path, api_response):
        # Convert the text to speech using OpenAI's TTS API
        response = openai.Audio.speech.create(
            model="text-to-speech-1",  # TTS model name may differ
            voice="nova",  # Specify a voice (choose one that is available)
            input=api_response
        )
        # Save the speech response to a file
        with open(speech_file_path, "wb") as f:
            f.write(response['audio'])

    # Streamlit interface for voice assistant
    st.title("πŸ§‘β€πŸ’» Skolo Online πŸ’¬ Talking Assistant")
    """
    HiπŸ€– just click on the voice recorder and let me know how I can help you today?
    """

    audio_bytes = audio_recorder()
    if audio_bytes:
        # Save the Recorded Audio File
        audio_location = "audio_file.wav"
        with open(audio_location, "wb") as f:
            f.write(audio_bytes)

        # Transcribe the saved file to text
        text = transcribe_text_to_voice(audio_location)
        st.write("You said:", text)

        # Get AI response from GPT-3.5
        api_response = chat_completion_call(text)
        st.write("AI says:", api_response)

        # Convert the response to speech and save it as a file
        speech_file_path = 'audio_response.mp3'
        text_to_speech_ai(speech_file_path, api_response)

        # Play the audio response in the app
        st.audio(speech_file_path)

else:
    st.warning("Please enter your OpenAI API key to continue.")