import streamlit as st import requests import json siteUrl = "https://aloqas-aloqas-qa-fastapi.hf.space" response = None userContext = None context = None error = 0 limit = 9000 with st.sidebar: st.title("Configuration") model = st.selectbox('Which model would you like to use?', ('SqueezeBERT', 'BERT', 'DeBERTa')) contextSelect = st.radio("Pick a context mode:", ["Text", "File"]) if contextSelect == "File": userContext = st.file_uploader("Pick a file for the context", accept_multiple_files=True) context = "/uploadfile/" else: userContext = st.text_area("Write the context") context = "/contextText/" st.title("ALOQAS") """ bla bla bla """ if "messages" not in st.session_state: st.session_state["messages"] = [ {"role": "assistant", "content": "Hi, I'm ALOQAS. How can I help you?"} ] if prompt := st.chat_input(placeholder="Write to ALOQAS..."): st.session_state.messages.append({"role": "user", "content": prompt}) params = {'question': prompt, "model": model.lower()} if userContext != '' and len(userContext) <= limit: if contextSelect == "File" and userContext: # Préparation de la liste des fichiers pour l'envoi files = [("files", (file.name, file, file.type)) for file in userContext] response = requests.post(siteUrl + context, data=params, files=files) st.session_state.messages.append({"role": "assistant", "content": json.loads(response.text).get("answer", "Sorry, I couldn't find an answer for your question.")}) else: params["context"] = userContext response = requests.post(siteUrl + context, json=params) st.session_state.messages.append({"role": "assistant", "content": json.loads(response.text).get("answer", "Sorry, I couldn't find an answer for your question.")}) # # st.write("Statut de la requête:", response.status_code) # # st.write("Réponse du serveur:", response.text) if response is None: error = 3 if userContext != '' and len(userContext) <= limit and response is not None: with st.sidebar: st.title("Statistics on the last answer") st.write(f"Score: {round(json.loads(response.text).get('score', 0), 2)}") st.write(f"Start: {json.loads(response.text).get('start', 0)}") st.write(f"End: {json.loads(response.text).get('end', 0)}") elif userContext == '' : error = 1 elif len(userContext) > limit: error = 2 else: error = 3 for msg in st.session_state.messages: st.chat_message(msg.get("role", "assistant")).write(msg.get("content", "Hi, I'm ALOQAS. How can I help you?")) if error == 1: message_rouge = "⚠️ Please provide a context via the menu on your left." st.markdown(f'
{message_rouge}
', unsafe_allow_html=True) error = 0 elif error == 2: message_rouge = f"⚠️ The message you submitted was too long, please reload the conversation and submit something shorter than {limit} caracter." st.markdown(f'
{message_rouge}
', unsafe_allow_html=True) error = 0 elif error == 3: message_rouge = f"⚠️ An error occured, please try again." st.markdown(f'
{message_rouge}
', unsafe_allow_html=True) error = 0 else: st.write("")