rkrstacic's picture
Upload app.py
dc3f654
raw
history blame
1.57 kB
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))