Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from api_call_module import get_answer
|
3 |
+
|
4 |
+
processes = ["Praksa", "Izrada završnog rada"]
|
5 |
+
file_name = "ans.txt"
|
6 |
+
|
7 |
+
# Read answers from ans.txt
|
8 |
+
answers = []
|
9 |
+
try:
|
10 |
+
answers = [line.replace("\n", "") for line in open(file_name, "r")]
|
11 |
+
except FileNotFoundError:
|
12 |
+
open(file_name, "w")
|
13 |
+
|
14 |
+
|
15 |
+
# Save the answer to ans.txt
|
16 |
+
def save_answer(answer: str) -> None:
|
17 |
+
f = open(file_name, "a")
|
18 |
+
f.write(f"{answer}\n")
|
19 |
+
f.close()
|
20 |
+
|
21 |
+
|
22 |
+
# Submits the query and saves the answer
|
23 |
+
def submit_query() -> None:
|
24 |
+
if query == "":
|
25 |
+
return
|
26 |
+
|
27 |
+
ans = get_answer(query, process)
|
28 |
+
|
29 |
+
save_answer("A: " + ans)
|
30 |
+
save_answer("Q: " + query)
|
31 |
+
save_answer("**Question {}**")
|
32 |
+
save_answer("")
|
33 |
+
|
34 |
+
|
35 |
+
# Clears the chat
|
36 |
+
def clear() -> None:
|
37 |
+
open(file_name, 'w').close()
|
38 |
+
pass
|
39 |
+
|
40 |
+
|
41 |
+
# Sidebar
|
42 |
+
with st.sidebar:
|
43 |
+
st.title("Set up your chatbot!")
|
44 |
+
process = st.selectbox("Select a process", processes)
|
45 |
+
st.caption("You can find this chatbot implementation [here](https://huggingface.co/spaces/rkrstacic/"
|
46 |
+
"Software-module-for-answering-questions-on-processes/tree/main)")
|
47 |
+
|
48 |
+
# Main
|
49 |
+
st.title("Welcome to the process answering chatbot!")
|
50 |
+
query = st.text_input("Ask me a question", placeholder="e.g. Koliko traje proces praksa?")
|
51 |
+
|
52 |
+
# Buttons
|
53 |
+
col1, col2 = st.columns([6, 1])
|
54 |
+
col1.button("Send", on_click=submit_query)
|
55 |
+
col2.button("Clear chat", on_click=clear)
|
56 |
+
|
57 |
+
|
58 |
+
with st.expander("Chat History", expanded=True):
|
59 |
+
for index, ans in enumerate(answers[::-1]):
|
60 |
+
st.write(ans if index % 4 != 1 else ans.format((len(answers) - index) // 4 + 1))
|