File size: 1,566 Bytes
dc3f654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from api_call_module import get_answer

processes = ["Praksa", "Izrada završnog rada"]
file_name = "ans.txt"

# Read answers from ans.txt
answers = []
try:
    answers = [line.replace("\n", "") for line in open(file_name, "r")]
except FileNotFoundError:
    open(file_name, "w")


# Save the answer to ans.txt
def save_answer(answer: str) -> None:
    f = open(file_name, "a")
    f.write(f"{answer}\n")
    f.close()


# Submits the query and saves the answer
def submit_query() -> None:
    if query == "":
        return

    ans = get_answer(query, process)

    save_answer("A: " + ans)
    save_answer("Q: " + query)
    save_answer("**Question {}**")
    save_answer("")


# Clears the chat
def clear() -> None:
    open(file_name, 'w').close()
    pass


# Sidebar
with st.sidebar:
    st.title("Set up your chatbot!")
    process = st.selectbox("Select a process", processes)
    st.caption("You can find this chatbot implementation [here](https://huggingface.co/spaces/rkrstacic/"
               "Software-module-for-answering-questions-on-processes/tree/main)")

# Main
st.title("Welcome to the process answering chatbot!")
query = st.text_input("Ask me a question", placeholder="e.g. Koliko traje proces praksa?")

# Buttons
col1, col2 = st.columns([6, 1])
col1.button("Send", on_click=submit_query)
col2.button("Clear chat", on_click=clear)


with st.expander("Chat History", expanded=True):
    for index, ans in enumerate(answers[::-1]):
        st.write(ans if index % 4 != 1 else ans.format((len(answers) - index) // 4 + 1))