DataRaptor commited on
Commit
ba68f4f
·
1 Parent(s): 7cae30c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +186 -0
app.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Fri Aug 18 08:01:41 2023
4
+
5
+ @author: Shamim Ahamed, RE AIMS Lab
6
+ """
7
+
8
+ import streamlit as st
9
+ import pandas as pd
10
+ from tqdm.cli import tqdm
11
+ import numpy as np
12
+ import requests
13
+ import pandas as pd
14
+ from tqdm import tqdm
15
+
16
+
17
+ def get_user_data(api, parameters):
18
+ response = requests.post(f"{api}", json=parameters)
19
+ if response.status_code == 200:
20
+ return response.json()
21
+ else:
22
+ print(f"ERROR: {response.status_code}")
23
+ return None
24
+
25
+
26
+
27
+ st.set_page_config(page_title="SuSastho.AI Chatbot", page_icon="🚀", layout='wide')
28
+
29
+ st.markdown("""
30
+ <style>
31
+ p {
32
+ font-size:0.8rem !important;
33
+ }
34
+ textarea {
35
+ font-size: 0.8rem !important;
36
+ padding: 0.8rem 1rem 0.75rem 0.8rem !important;
37
+ }
38
+ button {
39
+ padding: 0.65rem !important;
40
+ }
41
+
42
+ .css-1lr5yb2 {
43
+ background-color: rgb(105 197 180) !important;
44
+ }
45
+
46
+
47
+ .css-1c7y2kd {
48
+ background-color: Transparent !important;
49
+ }
50
+ .css-4oy321 {
51
+ background-color: rgba(240, 242, 246, 0.5) !important;
52
+ }
53
+
54
+ </style>
55
+ """, unsafe_allow_html=True)
56
+
57
+ st.markdown("""
58
+ <style>
59
+ #MainMenu {visibility: hidden;}
60
+ footer {visibility: hidden;}
61
+ </style>
62
+ """,unsafe_allow_html=True)
63
+
64
+
65
+ model_names = {
66
+ 'BLOOM 7B': 'bloom-7b',
67
+ }
68
+
69
+
70
+
71
+ with st.sidebar:
72
+ st.title("SuSastho.AI - ChatBot 🚀")
73
+ model_name = model_names[st.selectbox('Model', list(model_names.keys()), 0)]
74
+
75
+ max_ctx = st.slider('Select Top N Context', min_value=1, max_value=6, value=3, step=1)
76
+ # ctx_checker_tmp = st.slider('Context Checker Sensitivity', min_value=0.001, max_value=1.0, value=0.008, step=0.001)
77
+ ctx_checker_tmp = 0.008
78
+ lm_tmp = st.slider('Language Model Sensitivity', min_value=0.001, max_value=1.0, value=0.1, step=0.001)
79
+
80
+ cls_threshold = st.slider('Classification Threshold', min_value=0.01, max_value=1.0, value=0.5, step=0.01)
81
+
82
+ verbose = st.checkbox('Show Detailed Response', value=False)
83
+ if verbose == True:
84
+ retv_cnt = st.slider('Display N retrived Doc', min_value=0, max_value=32, value=0, step=1)
85
+
86
+
87
+
88
+
89
+ endpoint = st.secrets["LLMEndpoint"]
90
+
91
+
92
+ def main():
93
+ if model_name == 'None':
94
+ st.markdown('##### Please select a model.')
95
+ return
96
+
97
+ # Initialize chat history
98
+ if "messages" not in st.session_state:
99
+ st.session_state.messages = [{"role": 'assistant', "content": 'হ্যালো! আমি একটি এআই অ্যাসিস্ট্যান্ট। কীভাবে সাহায্য করতে পারি? 😊'}]
100
+
101
+ # Display chat messages from history on app rerun
102
+ for message in st.session_state.messages:
103
+ with st.chat_message(message["role"]):
104
+ st.markdown(message["content"])
105
+
106
+
107
+ # Accept user input
108
+ if prompt := st.chat_input("এখানে মেসেজ লিখুন"):
109
+ # Display user message in chat message container
110
+ with st.chat_message("user"):
111
+ st.markdown(prompt)
112
+ # Add user message to chat history
113
+ st.session_state.messages.append({"role": "user", "content": prompt})
114
+
115
+
116
+ ## Get context
117
+ params = {
118
+ "chat_history": [
119
+ {"content": prompt}
120
+ ],
121
+ "model": "bloom-7b",
122
+ "mode": "specific",
123
+ "config": {
124
+ "ctx_checker_tmp": ctx_checker_tmp,
125
+ "lm_tmp": lm_tmp,
126
+ "max_ctx": max_ctx,
127
+ "cls_threshold": cls_threshold,
128
+ "llm_enable": True,
129
+ }
130
+ }
131
+ resp = get_user_data(endpoint, params)
132
+ if resp == None:
133
+ st.markdown('#### INTERNAL ERROR')
134
+ return
135
+
136
+ response = resp['data']['responses'][0]['content']
137
+ context = resp['data']['logs']['content']['retrival_model']['matched_doc']
138
+ context_prob = resp['data']['logs']['content']['retrival_model']['matched_prob']
139
+
140
+ if verbose:
141
+ clen = len(context)
142
+ retrived = resp['data']['logs']['content']['retrival_model']['retrived_doc'][:retv_cnt]
143
+ retrived_prob = resp['data']['logs']['content']['retrival_model']['retrived_prob'][:retv_cnt]
144
+ retrived = [str(round(b, 3)) + ': ' + a for a, b in zip (retrived, retrived_prob)]
145
+ retrived = '\n\n===============================\n\n'.join(retrived)
146
+
147
+ context = [str(round(b, 3)) + ': ' + a for a, b in zip (context, context_prob)]
148
+ context = '\n\n===============================\n\n'.join(context)
149
+ response = f'###### Config: Context Checker Value: {ctx_checker_tmp}, LM Value: {lm_tmp}\n\n##### Retrived Context:\n{retrived}\n\n##### Matched Context:{clen}\n{context}\n\n##### Response:\n{response}'
150
+
151
+
152
+ # Display assistant response in chat message container
153
+ with st.chat_message("assistant", avatar=None):
154
+ st.markdown(response)
155
+
156
+ # Add assistant response to chat history
157
+ st.session_state.messages.append({"role": "assistant", "content": response})
158
+
159
+
160
+
161
+ def app_viewport():
162
+ passw = st.empty()
163
+ appc = st.container()
164
+
165
+ if 'logged_in' not in st.session_state:
166
+ with passw.container():
167
+ secret = st.text_input('Please Enter Access Code')
168
+ if st.button("Submit", type='primary'):
169
+ if secret == st.secrets["login_secret"]:
170
+ passw.empty()
171
+ st.session_state['logged_in'] = True
172
+ else:
173
+ st.error('Wrong Access Code.')
174
+
175
+ if 'logged_in' in st.session_state and st.session_state['logged_in'] == True:
176
+ with appc:
177
+ main()
178
+
179
+
180
+
181
+ if __name__ == '__main__':
182
+ app_viewport()
183
+
184
+
185
+
186
+