File size: 2,450 Bytes
6901017
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64d99bb
 
6901017
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
import json
import sqlite3
import datetime
import yaml

chat_history = []
#"st.session_state object", st.session_state

with open('config.yaml', 'r') as file:
    config = yaml.safe_load(file)

docchat_engine_url = config['docchat-engine']['url']

conn = sqlite3.connect('message.db')
c = conn.cursor()


#Get unique id from database and set the session unique id
if 'session_unique_id' not in st.session_state:
    #Insert timestamp of the request, the unique id is autogenerated at the table level. 
    c.execute("INSERT INTO SESSION (TIMESTAMP) VALUES('{timestamp_value}')".format(timestamp_value=datetime.datetime.now()))
    conn.commit()

    #Fetch the autogenerated uniqueid from table (autoincremented number)
    c.execute("SELECT UNIQUE_ID FROM SESSION ORDER BY UNIQUE_ID DESC LIMIT 1")
    for tmp_select_return_msg in c.fetchone():
        unique_id_from_db = tmp_select_return_msg
    st.session_state['session_unique_id'] = unique_id_from_db


def call_web_service(message):
    response = requests.post('{value_url}/chat/'.format(value_url=docchat_engine_url), json={'text': message})
    return response.json()

st.title('Chat Interface')

user_input = st.text_input("Type your message here:", key="user_input")
if st.button("Send"):
    if user_input:
        response = call_web_service(user_input)
        chat_history.append("DocChat :" + " " + response["message"])
        chat_history.append("You :" + " " + user_input)
        
        for message in chat_history:
            format_sql_insert = 'INSERT INTO messages (message, unique_id) VALUES ("{value}","{uniqueid}")'.format(value=message,uniqueid=st.session_state['session_unique_id'])
            print("DEBUG:  " + format_sql_insert)
            c.execute(format_sql_insert)
        conn.commit()

        c.execute("Select message from messages where unique_id = {value_unique_id} order by id desc".format(value_unique_id=st.session_state['session_unique_id']))
        msg_from_db = c.fetchall()

        for op_message in msg_from_db:
            st.write(op_message[0])

if 'button_clicked' not in st.session_state:
    st.session_state.button_clicked = False

if st.sidebar.button('Load Chat'):
    st.session_state.button_clicked = True

if st.session_state.button_clicked:
    response = requests.post('{value_url}/loadvector/'.format(value_url=docchat_engine_url), json={"text": "_chat"})
    st.write('Context Loaded')