Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import requests | |
| from pyht.client import TTSOptions | |
| from pyht import Client | |
| import json | |
| client = Client( | |
| user_id = '6NvoXiI87ZUGxBZfQmx0M9i4ejh1' , | |
| api_key = '0ad6e2fe73be498992f532a8344a0b5e' | |
| ) | |
| with open('assets/voice_options.json') as json_file : voice_options = json.load(json_file) | |
| st.set_page_config(layout = 'wide') | |
| # url = 'https://ayushsinghal1510-kapil-vps-backend-2.hf.space' | |
| url = 'http://localhost:7861' | |
| def check_prompt(prompt) : | |
| try : prompt.replace('' , '') ; return True | |
| except : return False | |
| def check_mesaage() : | |
| if 'messages' not in st.session_state : st.session_state.messages = [] | |
| option = st.sidebar.selectbox('Choose option' , options = ['Add Scenario' , 'Chat' , 'Feedback' , 'History' , 'Reason']) | |
| if option == 'Add Scenario' : | |
| history = requests.post(f'{url}/get-keys') | |
| st.sidebar.write('Please do not use these, if creating a new prompt') | |
| st.sidebar.write(history.json()['response']) | |
| col_1 , col_2 = st.columns(2) | |
| scenario_id = col_1.text_input('Write Scenario ID') | |
| scenario_prompt = col_2.text_area('Write Scenario Prompt') | |
| if st.button('Add Scenario' , use_container_width = True) : | |
| response = requests.post(f'{url}/add-scenario' , json = { | |
| 'scenario_id' : scenario_id , | |
| 'scenario_prompt' : scenario_prompt | |
| }) | |
| # st.code() | |
| st.code(response.json() , wrap_lines = True) | |
| elif option == 'Chat' : | |
| session_id = st.sidebar.text_input('Enter session ID') | |
| scenario_id = st.sidebar.text_input('Enter Scenario ID') | |
| voice_key = st.sidebar.selectbox( | |
| 'Select the voice' , | |
| options = list(voice_options.keys()) , | |
| key = list(voice_options.keys())[0] | |
| ) | |
| check_mesaage() | |
| for message in st.session_state.messages : | |
| with st.chat_message(message['role']) : st.markdown(message['content']) | |
| prompt = st.chat_input('Ask me anything') | |
| if check_prompt(prompt) : | |
| with st.chat_message('user'): st.markdown(prompt) | |
| st.session_state.messages.append({ | |
| 'role' : 'user' , | |
| 'content' : prompt | |
| }) | |
| if prompt != None or prompt != '' : | |
| response = requests.post( | |
| url = f'{url}/ask' , | |
| json = { | |
| 'query' : prompt , | |
| 'session_id' : session_id , | |
| 'scenario_id' : scenario_id | |
| } | |
| ).json()['response'] | |
| with st.chat_message('assistant') : | |
| st.markdown(response) | |
| buffer_stream = bytes() | |
| for chunk in client.tts(response , TTSOptions(voice = voice_options[voice_key])) : buffer_stream += chunk | |
| st.audio(buffer_stream , format = 'multipart/byteranges') | |
| st.session_state.messages.append({ | |
| 'role' : 'assistant' , | |
| 'content' : response | |
| }) | |
| elif option == 'Reason' : | |
| session_id = st.sidebar.text_input('Enter session ID') | |
| scenario_id = st.sidebar.text_input('Enter Scenario ID') | |
| voice_key = st.sidebar.selectbox( | |
| 'Select the voice' , | |
| options = list(voice_options.keys()) , | |
| key = list(voice_options.keys())[0] | |
| ) | |
| check_mesaage() | |
| for message in st.session_state.messages : | |
| with st.chat_message(message['role']) : st.markdown(message['content']) | |
| prompt = st.chat_input('Ask me anything') | |
| if check_prompt(prompt) : | |
| with st.chat_message('user'): st.markdown(prompt) | |
| st.session_state.messages.append({ | |
| 'role' : 'user' , | |
| 'content' : prompt | |
| }) | |
| if prompt != None or prompt != '' : | |
| response = requests.post( | |
| url = f'{url}/ask-reason' , | |
| json = { | |
| 'query' : prompt , | |
| 'session_id' : session_id , | |
| 'scenario_id' : scenario_id | |
| } | |
| ).json()['response'] | |
| with st.chat_message('assistant') : | |
| st.markdown(response) | |
| buffer_stream = bytes() | |
| for chunk in client.tts(response , TTSOptions(voice = voice_options[voice_key])) : buffer_stream += chunk | |
| st.audio(buffer_stream , format = 'multipart/byteranges') | |
| st.session_state.messages.append({ | |
| 'role' : 'assistant' , | |
| 'content' : response | |
| }) | |
| elif option == 'Feedback' : | |
| session_id = st.text_input('Enter your Session ID') | |
| scenario_id = st.text_input('Enter your Secnario ID') | |
| if st.button('Feedback' , use_container_width = True) : | |
| response = requests.post(f'{url}/get-results' , json = { | |
| 'session_id' : str(session_id) , | |
| 'scenario_id' : str(scenario_id) | |
| }) | |
| st.code(response.json()['response']) | |
| elif option == 'History' : | |
| session_id = st.text_input('Enter your Session ID') | |
| scenario_id = st.text_input('Enter your Secnario ID') | |
| if st.button('History' , use_container_width = True) : | |
| response = requests.post(f'{url}/get-history' , json = { | |
| 'session_id' : str(session_id) , | |
| 'scenario_id' : str(scenario_id) | |
| }) | |
| st.write(response.json()['response']) |