File size: 6,299 Bytes
3378b23
4dd424d
550e87b
be06203
 
ac46aeb
27a479a
1c516fb
4dd424d
47a81d9
be06203
c3b9b9a
7b594ac
 
 
 
 
6a2d3ac
83ed4d1
 
 
6a2d3ac
 
 
 
 
 
ac46aeb
6a2d3ac
b8c8744
41b589c
 
0802dfc
f060825
 
 
0802dfc
 
 
 
41b589c
 
 
0802dfc
b8c8744
0802dfc
f419f72
 
 
6a2d3ac
b8c8744
dca80da
 
 
 
 
 
3560824
 
 
 
 
 
6a2d3ac
 
 
dca80da
 
 
 
 
 
7b594ac
 
f060825
 
7b594ac
 
346c949
 
 
 
 
 
 
 
 
 
 
 
7b594ac
 
 
 
decc199
 
 
7b594ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41b589c
7b594ac
6a2d3ac
dca80da
 
 
 
 
 
 
 
 
 
d5e3908
3560824
da7a60c
 
 
 
 
 
 
 
6a2d3ac
7b594ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a2d3ac
7b594ac
c885074
7b594ac
b8c8744
3378b23
a843121
3378b23
 
a843121
 
e4efa7a
 
a843121
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import json
import os

import streamlit as st
import streamlit.components.v1 as components
from chain import get_chain
from chat_history import insert_chat_history, insert_chat_history_articles
from connection import connect
from css import load_css
from langchain.callbacks import get_openai_callback
from message import Message

st.set_page_config(layout="wide")

st.title("Sorbobot - Le futur de la recherche scientifique interactive")

chat_column, doc_column = st.columns([2, 1])

conn = connect()


def initialize_session_state():
    if "history" not in st.session_state:
        st.session_state.history = []
    if "token_count" not in st.session_state:
        st.session_state.token_count = 0
    if "conversation" not in st.session_state:
        st.session_state.conversation = get_chain(conn)


def send_message_callback():
    with st.spinner("Wait for it..."):
        with get_openai_callback() as cb:
            human_prompt = st.session_state.human_prompt.strip()
            if len(human_prompt) == 0:
                return
            llm_response = st.session_state.conversation(human_prompt)
            st.session_state.history.append(Message("human", human_prompt))
            st.session_state.history.append(
                Message(
                    "ai",
                    llm_response["answer"],
                    documents=llm_response["source_documents"],
                )
            )
            st.session_state.token_count += cb.total_tokens
            if os.environ.get("ENVIRONMENT") == "dev":
                history_id = insert_chat_history(conn, human_prompt, llm_response["answer"])
                insert_chat_history_articles(conn, history_id, llm_response["source_documents"])


def exemple_message_callback_button(args):
    st.session_state.human_prompt = args
    send_message_callback()
    st.session_state.human_prompt = ""


def clear_history():
    st.session_state.history.clear()
    st.session_state.token_count = 0
    st.session_state.conversation.memory.clear()


load_css()
initialize_session_state()

exemples = [
    "Who has published influential research on quantum computing?",
    "List any prominent authors in the field of artificial intelligence ethics?",
    "Who are the leading experts on climate change mitigation strategies?",
]

with chat_column:
    chat_placeholder = st.container()
    prompt_placeholder = st.form("chat-form", clear_on_submit=True)
    information_placeholder = st.container()

    with chat_placeholder:
        div = f"""
            <div class="chat-row">
                <img class="chat-icon" src="https://cdn-icons-png.flaticon.com/512/1129/1129398.png" width=32 height=32>
                <div class="chat-bubble ai-bubble">
                    Welcome to SorboBot, a Hugging Face Space designed to revolutionize the way you find published articles. <br/>
                    Powered by a full export from ScanR and Hal at Sorbonne University, SorboBot utilizes advanced language model technology
                    to provide you with a list of published articles based on your prompt.
                </div>
            </div>
        """
        st.markdown(div, unsafe_allow_html=True)

        for chat in st.session_state.history:
            div = f"""
                <div class="chat-row 
                    {'' if chat.origin == 'ai' else 'row-reverse'}">
                    <img class="chat-icon" src="https://cdn-icons-png.flaticon.com/512/{
                        '1129/1129398.png' if chat.origin == 'ai' 
                                    else '1077/1077012.png'}"
                        width=32 height=32>
                    <div class="chat-bubble
                    {'ai-bubble' if chat.origin == 'ai' else 'human-bubble'}">
                        &#8203;{chat.message}
                    </div>
                </div>
            """
            st.markdown(div, unsafe_allow_html=True)

        for _ in range(3):
            st.markdown("")

    with prompt_placeholder:
        st.markdown("**Chat**")
        cols = st.columns((6, 1))
        cols[0].text_input(
            "Chat",
            label_visibility="collapsed",
            key="human_prompt",
        )
        cols[1].form_submit_button(
            "Submit",
            type="primary",
            on_click=send_message_callback,
        )

    if st.session_state.token_count == 0:
        information_placeholder.markdown("### Test me !")
        for idx_exemple, exemple in enumerate(exemples):
            information_placeholder.button(
                exemple,
                key=f"{idx_exemple}_button",
                on_click=exemple_message_callback_button,
                args=(exemple,)
            )

    st.button(":new: Start a new conversation", on_click=clear_history, type="secondary")

    if os.environ.get("ENVIRONMENT") == "dev":
        information_placeholder.caption(
            f"""
        Used {st.session_state.token_count} tokens \n
        Debug Langchain conversation: 
        {st.session_state.history}
        """
        )

    components.html(
        """
    <script>
    const streamlitDoc = window.parent.document;

    const buttons = Array.from(
        streamlitDoc.querySelectorAll('.stButton > button')
    );
    const submitButton = buttons.find(
        el => el.innerText === 'Submit'
    );

    streamlitDoc.addEventListener('keydown', function(e) {
        switch (e.key) {
            case 'Enter':
                submitButton.click();
                break;
        }
    });
    </script>
    """,
        height=0,
        width=0,
    )

with doc_column:
    st.markdown("**Source documents**")
    if len(st.session_state.history) > 0:
        for doc in st.session_state.history[-1].documents:
            doc_content = json.loads(doc.page_content)
            doc_metadata = doc.metadata

            expander = st.expander(doc_content["title"])
            expander.markdown(f"**HalID** : https://hal.science/{doc_metadata['hal_id']}")
            expander.markdown(doc_metadata["abstract"])
            expander.markdown(f"**Authors** : {doc_content['authors']}")
            expander.markdown(f"**Keywords** : {doc_content['keywords']}")
            expander.markdown(f"**Distance** : {doc_metadata['distance']}")