import langchain
import os
import streamlit as st
import requests
import sounddevice as sd
import wavio
os.environ["OPENAI_API_KEY"]="ADD KEY" 
import openai
from openai import OpenAI
client=OpenAI()
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.prompts import HumanMessagePromptTemplate
from langchain.schema.messages import SystemMessage

chat_template = ChatPromptTemplate.from_messages(
    [
        SystemMessage(
            content=(
                "You are a presonal assistant for {your name] and your name is luna "
                "if the user call you by any other name than luna you need to correct him by your orginal name."
                "And for every output you can also use the username  in the answer which will be nice gesture"
                "you can act more,like an human speaking more than an ai replying to the message"
                "Consider the user as your friend"
                "Speak like a friend"
                "Be more creative and funny way"
            )
        ),
        HumanMessagePromptTemplate.from_template("{text}"),
    ]
)

llm = ChatOpenAI()


# Record audio
def record_audio(filename, duration, fs):
    print("Recording audio...")
    recording = sd.rec(int(duration * fs), samplerate=fs, channels=2)
    sd.wait()
    wavio.write(filename, recording, fs, sampwidth=2)
    print("Audio recorded and saved as", filename)


## Streamlit UI
st.set_page_config(page_title="Personal voice assistant ")

website_heading = "I am Your Personal Voice assistant"

# Display as a heading
st.markdown(f"<h1 style='text-align: center; color: #a274a3;'>{website_heading}</h1>", unsafe_allow_html=True)


st.write("Speak here")
if st.button(label="Click here to speak"):
    audio_filename = "input.wav"
    duration = 5  # Duration of the recording in seconds
    fs = 44100  # Sample rate
    record_audio(audio_filename, duration, fs) ## user input recorded and stores

    ##converting to text using whisper
  
    audio_file= open("input.wav", "rb")
    transcript = client.audio.translations.create(
    model="whisper-1", 
    file=audio_file)
    a=transcript.text
    # st.write(a)
    print(a)
    ##model
    a=llm(chat_template.format_messages(text=a))
    a=a.content

    ##audio output

    speech_file_path ="speech.mp3"
    response = client.audio.speech.create(
    model="tts-1",
    voice="nova",
    input=a)
    response.stream_to_file(speech_file_path)

    st.audio("speech.mp3")


langchain.prompts.HumanMessagePromptTemplate.from_template langchain.schema.messages.SystemMessage langchain.chat_models.ChatOpenAI
[((191, 199), 'openai.OpenAI', 'OpenAI', ([], {}), '()\n', (197, 199), False, 'from openai import OpenAI\n'), ((1151, 1163), 'langchain.chat_models.ChatOpenAI', 'ChatOpenAI', ([], {}), '()\n', (1161, 1163), False, 'from langchain.chat_models import ChatOpenAI\n'), ((1462, 1520), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': '"""Personal voice assistant """'}), "(page_title='Personal voice assistant ')\n", (1480, 1520), True, 'import streamlit as st\n'), ((1601, 1720), 'streamlit.markdown', 'st.markdown', (['f"""<h1 style=\'text-align: center; color: #a274a3;\'>{website_heading}</h1>"""'], {'unsafe_allow_html': '(True)'}), '(\n    f"<h1 style=\'text-align: center; color: #a274a3;\'>{website_heading}</h1>",\n    unsafe_allow_html=True)\n', (1612, 1720), True, 'import streamlit as st\n'), ((1714, 1736), 'streamlit.write', 'st.write', (['"""Speak here"""'], {}), "('Speak here')\n", (1722, 1736), True, 'import streamlit as st\n'), ((1740, 1778), 'streamlit.button', 'st.button', ([], {'label': '"""Click here to speak"""'}), "(label='Click here to speak')\n", (1749, 1778), True, 'import streamlit as st\n'), ((1329, 1338), 'sounddevice.wait', 'sd.wait', ([], {}), '()\n', (1336, 1338), True, 'import sounddevice as sd\n'), ((1343, 1392), 'wavio.write', 'wavio.write', (['filename', 'recording', 'fs'], {'sampwidth': '(2)'}), '(filename, recording, fs, sampwidth=2)\n', (1354, 1392), False, 'import wavio\n'), ((2490, 2512), 'streamlit.audio', 'st.audio', (['"""speech.mp3"""'], {}), "('speech.mp3')\n", (2498, 2512), True, 'import streamlit as st\n'), ((468, 916), 'langchain.schema.messages.SystemMessage', 'SystemMessage', ([], {'content': '"""You are a presonal assistant for {your name] and your name is luna if the user call you by any other name than luna you need to correct him by your orginal name.And for every output you can also use the username  in the answer which will be nice gestureyou can act more,like an human speaking more than an ai replying to the messageConsider the user as your friendSpeak like a friendBe more creative and funny way"""'}), "(content=\n    'You are a presonal assistant for {your name] and your name is luna if the user call you by any other name than luna you need to correct him by your orginal name.And for every output you can also use the username  in the answer which will be nice gestureyou can act more,like an human speaking more than an ai replying to the messageConsider the user as your friendSpeak like a friendBe more creative and funny way'\n    )\n", (481, 916), False, 'from langchain.schema.messages import SystemMessage\n'), ((1084, 1134), 'langchain.prompts.HumanMessagePromptTemplate.from_template', 'HumanMessagePromptTemplate.from_template', (['"""{text}"""'], {}), "('{text}')\n", (1124, 1134), False, 'from langchain.prompts import HumanMessagePromptTemplate\n')]