File size: 5,207 Bytes
8acbf6e
 
5b4d076
8acbf6e
d9e6c85
28ac775
375bf89
8acbf6e
 
2679d8c
7b7b42e
d9e6c85
 
8acbf6e
d9e6c85
 
 
 
 
8acbf6e
d92c6ee
d9e6c85
d92c6ee
d9e6c85
 
 
 
30b0fcd
8acbf6e
d9e6c85
3b808f0
8acbf6e
30b0fcd
3b808f0
8acbf6e
d9e6c85
8acbf6e
 
30b0fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8acbf6e
 
 
 
 
 
 
 
375bf89
 
 
7b7b42e
8acbf6e
375bf89
 
 
8acbf6e
 
375bf89
 
5b4d076
375bf89
 
 
2679d8c
30b0fcd
375bf89
 
 
 
 
 
 
d92c6ee
 
375bf89
2679d8c
d9e6c85
2679d8c
1d71dd5
 
 
 
30b0fcd
1d71dd5
 
375bf89
30b0fcd
375bf89
 
 
30b0fcd
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
import streamlit as st
import requests
import json

# Define initial configurations and variables
siteUrl = "https://aloqas-aloqas-qa-fastapi.hf.space"
response = None
userContext = None
context = None
error = 0
limit = 9000
model = None


def on_change_write_name_of_model():
    selected_model = st.session_state['model']
    st.session_state.messages.append({"role": "assistant", "content": f"Switched to {selected_model} model."})

# Sidebar configuration
with st.sidebar:
    st.title("Configuration")
    # Define a selectbox for model selection
    model = st.selectbox('Which model would you like to use?',
                         ('SqueezeBERT', 'BERT', 'DeBERTa'), index=1,
                         help="SqueezeBERT is faster but less accurate, BERT is balanced, and DeBERTa is slower but more accurate.",
                         on_change=on_change_write_name_of_model, key='model')
    # Define a radio button group for context mode selection
    contextSelect = st.radio("Pick a context mode:", ["Text", "File"], index=0, help="Choose between text or file context.")
    if contextSelect == "File":
        userContext = st.file_uploader("Pick a file for the context", accept_multiple_files=True, help="Upload files of types: txt, json, docx, pdf, csv for context.", type=['txt', 'json', 'docx', 'pdf', 'csv'])
        context = "/uploadfile/"
    else:
        userContext = st.text_area("Write the context", max_chars=limit, help="Insert the context text here.")
        context = "/contextText/"

# Main page title
st.title("ALOQAS")

# Dynamic welcome message based on model and context selection
if model and contextSelect:
    if model == 'DeBERTa':
        model_info = "Using the DeBERTa model for the most accurate answers, although it may be slower."
    elif model == 'SqueezeBERT':
        model_info = "Using the SqueezeBERT model for faster but less precise answers."
    else:
        model_info = "Using the BERT model as a balance between speed and accuracy."

    if contextSelect == "File":
        context_info = "Upload files of types: txt, json, docx, pdf, csv for context."
    else:
        context_info = f"Insert text context up to {limit} characters."

    st.subheader("Welcome to ALOQAS AI")
    st.write(f"{model_info} {context_info}")
    st.write("")
    st.write("")

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:
            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.")})

    if response is None:
        error = 3
    if userContext != '' and len(userContext) <= limit and response is not None:
        with st.sidebar:
            st.markdown(f"<div style='color: white; background-color: #0F1116 ; padding: 10px; border-radius: 5px;'><h2 style='color: white;'>Statistics on the last answer</h2><p>Time: {json.loads(str(round(response.elapsed.total_seconds(), 2)))}s</p><p>Score: {round(json.loads(response.text).get('score', 0), 2)}</p><p>Start: {json.loads(response.text).get('start', 0)}</p><p>End: {json.loads(response.text).get('end', 0)}</p></div>", unsafe_allow_html=True)

    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'<div style="color: white; background-color: #ff4444; padding: 10px; border-radius: 5px;">{message_rouge}</div>', 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} characters."
    st.markdown(f'<div style="color: white; background-color: #ff4444; padding: 10px; border-radius: 5px;">{message_rouge}</div>', unsafe_allow_html=True)
    error = 0
elif error == 3:
    message_rouge = f"⚠️ An error occurred, please try again."
    st.markdown(f'<div style="color: white; background-color: #ff4444; padding: 10px; border-radius: 5px;">{message_rouge}</div>', unsafe_allow_html=True)
    error = 0
else:
    st.write("")