import os
import uuid

import langchain
import requests
import streamlit as st
from dotenv import load_dotenv, find_dotenv
from langchain_community.callbacks import get_openai_callback
from langchain.schema import HumanMessage, AIMessage
from playsound import playsound
from streamlit_chat import message

from advisor.agents import init_convo_agent

langchain.debug = True


def init():
    load_dotenv(find_dotenv())

    st.set_page_config(
        page_title="Your Restaurant Advisor",
        page_icon="👩‍🍳",
    )
    st.header("Your Restaurant Advisor 👩‍🍳")

    hide_streamlit_style = """
            <style>
            #MainMenu {visibility: hidden;}
            footer {visibility: hidden;}
            </style>
            """
    st.markdown(hide_streamlit_style, unsafe_allow_html=True)


def setup_agent():
    if 'agent' not in st.session_state:
        random_session_id = str(uuid.uuid4())
        st.session_state.agent = init_convo_agent(random_session_id)


def get_response_from_ai(human_input):
    setup_agent()
    print("="*20)
    with get_openai_callback() as cb:
        result = st.session_state.agent.run(human_input)
        print("Cost:", cb)
        return result


def get_voice_message(message):
    payload = {
        "text": message,
        "model_id": "eleven_monolingual_v1",
        "voice_settings": {
            "stability": 0,
            "similarity_boost": 0,
        }
    }

    headers = {
        "accept": "audio/mpeg",
        "xi-api-key": os.getenv("ELEVEN_LABS_API_KEY"),
        "Content-Type": "application/json"
    }

    response = requests.post('https://api.elevenlabs.io/v1/text-to-speech/21m00Tcm4TlvDq8ikWAM?optimize_streaming_latency=0', json=payload, headers=headers)
    if response.status_code == 200 and response.content:
        with open("audio.mp3", "wb") as f:
            f.write(response.content)
        playsound("audio.mp3")
        return response.content


def main():
    init()

    with st.sidebar:
        user_input = st.text_input("your message", value="")

    if "messages" not in st.session_state:
        st.session_state.messages = []

    if user_input:
        st.session_state.messages.append(HumanMessage(content=user_input))
        with st.spinner("Thinking..."):
            response = get_response_from_ai(user_input)
            # get_voice_message(response)
            st.session_state.messages.append(AIMessage(content=response))

    messages = st.session_state.get('messages', [])
    for i, msg in enumerate(messages):
        if i % 2 == 0:
            message(msg.content, is_user=True, avatar_style="thumbs", key=str(i) + "_user")
        else:
            message(msg.content, is_user=False, avatar_style="avataaars", key=str(i) + "_ai")


if __name__ == "__main__":
    main()

langchain.schema.AIMessage langchain_community.callbacks.get_openai_callback langchain.schema.HumanMessage
[((424, 502), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': '"""Your Restaurant Advisor"""', 'page_icon': '"""👩\u200d🍳"""'}), "(page_title='Your Restaurant Advisor', page_icon='👩\\u200d🍳')\n", (442, 502), True, 'import streamlit as st\n'), ((525, 570), 'streamlit.header', 'st.header', (['"""Your Restaurant Advisor 👩\u200d🍳"""'], {}), "('Your Restaurant Advisor 👩\\u200d🍳')\n", (534, 570), True, 'import streamlit as st\n'), ((744, 801), 'streamlit.markdown', 'st.markdown', (['hide_streamlit_style'], {'unsafe_allow_html': '(True)'}), '(hide_streamlit_style, unsafe_allow_html=True)\n', (755, 801), True, 'import streamlit as st\n'), ((1596, 1747), 'requests.post', 'requests.post', (['"""https://api.elevenlabs.io/v1/text-to-speech/21m00Tcm4TlvDq8ikWAM?optimize_streaming_latency=0"""'], {'json': 'payload', 'headers': 'headers'}), "(\n    'https://api.elevenlabs.io/v1/text-to-speech/21m00Tcm4TlvDq8ikWAM?optimize_streaming_latency=0'\n    , json=payload, headers=headers)\n", (1609, 1747), False, 'import requests\n'), ((2453, 2489), 'streamlit.session_state.get', 'st.session_state.get', (['"""messages"""', '[]'], {}), "('messages', [])\n", (2473, 2489), True, 'import streamlit as st\n'), ((404, 417), 'dotenv.find_dotenv', 'find_dotenv', ([], {}), '()\n', (415, 417), False, 'from dotenv import load_dotenv, find_dotenv\n'), ((942, 977), 'advisor.agents.init_convo_agent', 'init_convo_agent', (['random_session_id'], {}), '(random_session_id)\n', (958, 977), False, 'from advisor.agents import init_convo_agent\n'), ((1064, 1085), 'langchain_community.callbacks.get_openai_callback', 'get_openai_callback', ([], {}), '()\n', (1083, 1085), False, 'from langchain_community.callbacks import get_openai_callback\n'), ((1110, 1149), 'streamlit.session_state.agent.run', 'st.session_state.agent.run', (['human_input'], {}), '(human_input)\n', (1136, 1149), True, 'import streamlit as st\n'), ((1497, 1529), 'os.getenv', 'os.getenv', (['"""ELEVEN_LABS_API_KEY"""'], {}), "('ELEVEN_LABS_API_KEY')\n", (1506, 1529), False, 'import os\n'), ((1884, 1906), 'playsound.playsound', 'playsound', (['"""audio.mp3"""'], {}), "('audio.mp3')\n", (1893, 1906), False, 'from playsound import playsound\n'), ((2007, 2046), 'streamlit.text_input', 'st.text_input', (['"""your message"""'], {'value': '""""""'}), "('your message', value='')\n", (2020, 2046), True, 'import streamlit as st\n'), ((895, 907), 'uuid.uuid4', 'uuid.uuid4', ([], {}), '()\n', (905, 907), False, 'import uuid\n'), ((2191, 2223), 'langchain.schema.HumanMessage', 'HumanMessage', ([], {'content': 'user_input'}), '(content=user_input)\n', (2203, 2223), False, 'from langchain.schema import HumanMessage, AIMessage\n'), ((2238, 2263), 'streamlit.spinner', 'st.spinner', (['"""Thinking..."""'], {}), "('Thinking...')\n", (2248, 2263), True, 'import streamlit as st\n'), ((2408, 2435), 'langchain.schema.AIMessage', 'AIMessage', ([], {'content': 'response'}), '(content=response)\n', (2417, 2435), False, 'from langchain.schema import HumanMessage, AIMessage\n')]