cjzhi98 commited on
Commit
1be2c8e
1 Parent(s): b138cf2

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +131 -0
  2. dataset_loader.py +18 -0
  3. requirements.txt +11 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pickle
3
+ from pathlib import Path
4
+ import streamlit as st
5
+ import streamlit_authenticator as stauth
6
+ import traceback
7
+ from dataset_loader import load_dataset
8
+ from dotenv import load_dotenv
9
+
10
+ load_dotenv()
11
+
12
+ if not os.path.isdir("spsetia_chatbot_data"):
13
+ load_dataset()
14
+
15
+ from spsetia_chatbot_data.utils import (
16
+ run_conversation,
17
+ record_db,
18
+ hashed_passwords,
19
+ )
20
+
21
+
22
+ st.set_page_config(page_title="S P Setia Chatbot", page_icon=":robot:", layout="wide")
23
+
24
+
25
+ # --- USER AUTHENTICATION ---
26
+ names = ["S P Setia Admin"]
27
+ usernames = ["spsetia_admin"]
28
+
29
+ authenticator = stauth.Authenticate(
30
+ names,
31
+ usernames,
32
+ hashed_passwords,
33
+ "spsetia_chatbot",
34
+ "eqiddnksadjw",
35
+ cookie_expiry_days=30,
36
+ )
37
+
38
+ name, authentication_status, username = authenticator.login("Login", "main")
39
+
40
+ if authentication_status == False:
41
+ st.error("Username/password is incorrect")
42
+
43
+ if authentication_status == None:
44
+ st.warning("Please enter your username and password")
45
+
46
+ if authentication_status:
47
+ st.sidebar.title(f"S P Setia Chatbot")
48
+ st.sidebar.header(f"Welcome {name}")
49
+ authenticator.logout("Logout", "sidebar")
50
+ if st.sidebar.button("Refresh Chat"):
51
+ st.session_state.messages = [
52
+ {"role": "assistant", "content": "How may I help you?"}
53
+ ]
54
+ if "messages" not in st.session_state:
55
+ st.session_state.messages = [
56
+ {"role": "assistant", "content": "How may I help you?"}
57
+ ]
58
+
59
+ for message in st.session_state.messages:
60
+ with st.chat_message(message["role"]):
61
+ st.write(message["content"])
62
+
63
+ if prompt := st.chat_input():
64
+ st.session_state.messages.append({"role": "user", "content": prompt})
65
+ with st.chat_message("user"):
66
+ st.write(prompt)
67
+
68
+ if st.session_state.messages[-1]["role"] != "assistant":
69
+ try:
70
+ with st.chat_message("assistant"):
71
+ with st.spinner("Thinking..."):
72
+ response = run_conversation(
73
+ st.session_state.messages, prompt, username
74
+ )
75
+ qa_id = response[4]
76
+ prompt_tokens = response[5]
77
+ completion_tokens = response[6]
78
+ if response[3]:
79
+ if isinstance(response[0], list):
80
+ answer = ""
81
+ for item in response[0]:
82
+ answer += f"""{item[0]}\n"""
83
+ st.text(item[0])
84
+ for image in item[1]:
85
+ if image:
86
+ st.image(image)
87
+ reference = f""" \n\nReference \nfolder: {response[2]["folder"]} \nfile: {response[2]["file"]}"""
88
+ st.text(reference)
89
+ answer += reference
90
+ record_db(
91
+ content=answer,
92
+ qa_id=qa_id,
93
+ msg_mode=1,
94
+ recipient_id=username,
95
+ prompt_tokens=prompt_tokens,
96
+ completion_tokens=completion_tokens,
97
+ )
98
+ else:
99
+ answer = response[0]
100
+ reference = f""" \n\nReference \nfolder: {response[2]["folder"]} \nfile: {response[2]["file"]}"""
101
+ answer += reference
102
+ st.text(answer)
103
+ record_db(
104
+ content=answer,
105
+ qa_id=qa_id,
106
+ msg_mode=1,
107
+ recipient_id=username,
108
+ prompt_tokens=prompt_tokens,
109
+ completion_tokens=completion_tokens,
110
+ )
111
+ else:
112
+ answer = response[0]
113
+ st.text(answer)
114
+ if images := response[1]:
115
+ if isinstance(images, list):
116
+ for image in images:
117
+ st.image(image)
118
+ record_db(
119
+ content=answer,
120
+ qa_id=qa_id,
121
+ msg_mode=1,
122
+ recipient_id=username,
123
+ prompt_tokens=prompt_tokens,
124
+ completion_tokens=completion_tokens,
125
+ )
126
+
127
+ message = {"role": "assistant", "content": answer}
128
+ st.session_state.messages.append(message)
129
+ except:
130
+ st.error("An error occurred. Please try again.")
131
+ traceback.print_exc()
dataset_loader.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import Repository
2
+ from huggingface_hub import login
3
+ import os
4
+ from dotenv import load_dotenv
5
+
6
+ load_dotenv()
7
+
8
+
9
+ def load_dataset():
10
+ login(token=os.getenv("HUB_TOKEN"))
11
+
12
+ repo = Repository(
13
+ local_dir="spsetia_chatbot_data",
14
+ repo_type="dataset",
15
+ clone_from="cjzhi98/spsetia-chatbot",
16
+ token=True,
17
+ )
18
+ repo.git_pull()
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ openai
2
+ pandas
3
+ fuzzywuzzy
4
+ python-dotenv
5
+ numpy
6
+ python-Levenshtein
7
+ streamlit
8
+ streamlit-authenticator==0.1.5
9
+ prettyprint
10
+ boto3
11
+ huggingface_hub