Spaces:
Runtime error
Runtime error
File size: 4,696 Bytes
24e958c cdf604b 24e958c cdf604b 24e958c cdf604b 24e958c |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import streamlit as st
from streamlit_chat import message
import os
from dotenv import load_dotenv
import requests
import json
import whisper
from audiorecorder import audiorecorder
load_dotenv()
# whisper
model = whisper.load_model('base')
DEFAULT_WIDTH = 80
width = 40
width = max(width, 0.01)
side = max((100 - width) / 2, 0.01)
# Judini
judini_api_key= os.getenv("JUDINI_API_KEY")
agent_id_cap_7= os.getenv("JUDINI_AGENT_ID_CAP_7")
headers = {"Content-Type": "application/json; charset=utf-8", "Authorization": "Bearer "+judini_api_key}
# Initialise session state variables
if 'generated' not in st.session_state:
st.session_state['generated'] = []
if 'past' not in st.session_state:
st.session_state['past'] = []
if 'messages' not in st.session_state:
st.session_state['messages'] = [
{"role": "system", "content": "You are a helpful assistant."}
]
def load_chat():
try:
# container for chat history
response_container = st.container()
# container for text box
container = st.container()
with container:
st.markdown("---", unsafe_allow_html=True)
st.write('Realiza una pregunta por texto o por audio sobre el capitulo')
audio = audiorecorder("Grabar audio", "Stop")
user_audio = ''
if len(audio) > 0:
st.audio(audio.tobytes())
# To save audio to a file:
wav_file = open("audio.mp3", "wb")
wav_file.write(audio.tobytes())
with st.form(key='my_form', clear_on_submit=True):
# Whisper
if os.path.exists("audio.mp3"):
output = model.transcribe("audio.mp3")
user_audio = output['text']
user_input = st.text_area("", key='input', height=100)
submit_button = st.form_submit_button(label='Enviar pregunta')
if submit_button and (user_input or user_audio != ''):
if os.path.exists("audio.mp3"):
os.remove("audio.mp3")
output, total_tokens, prompt_tokens, completion_tokens = generate_response(agent_id_cap_7, user_input, user_audio)
st.session_state['past'].append(user_input)
st.session_state['generated'].append(output)
if st.session_state['generated']:
with response_container:
for i in range(len(st.session_state['generated'])):
try:
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user')
message(st.session_state["generated"][i], key=str(i))
except json.JSONDecodeError as e:
print(f"Error: {e}")
except json.JSONDecodeError as e:
print(f"Error: {e}")
def generate_response(agent_id, text_input, audio_input):
prompt = text_input + ' .'+ audio_input
st.session_state['messages'].append({"role": "user", "content": prompt})
data = {
"messages": [
{
"role": "user",
"content": prompt
}
]
}
url = 'https://playground.judini.ai/api/v1/agent/'+agent_id
response = requests.post(url, headers=headers, json=data, stream=True)
token = ''
for chunk in response.iter_content(chunk_size=1024):
if chunk:
raw_data = chunk.decode('utf-8').replace("data: ", '')
if raw_data != "[DONE]":
try:
json_object = json.loads(raw_data.strip())
token += json_object['data']
except json.JSONDecodeError as e:
print(f"Error al cargar el JSON: {e}")
response = token
st.session_state['messages'].append({"role": "assistant", "content": response})
print(st.session_state['messages'])
total_tokens = 0
prompt_tokens = 0
completion_tokens = 0
return response, total_tokens, prompt_tokens, completion_tokens
def main():
st.set_page_config(page_title="El Club de la IA", layout="wide")
st.title("El Club de la IA")
st.write("Chatea con nuestro bot Isaac, experto en cada capitulo")
tabs = ['Cap 7']
selected_tab = st.sidebar.radio("Selecciona un Capitulo", tabs)
clear_button = st.sidebar.button("Limpiar conversación", key="clear")
if selected_tab == "Cap 7":
_, container, _ = st.columns([side, 47, side])
VIDEO_DATA = "https://youtu.be/kXwl2KE4C9w"
container.video(data=VIDEO_DATA)
load_chat()
st.markdown('Potenciado por [Judini](https://judini.ai)')
if __name__ == "__main__":
main() |