Spaces:
Sleeping
Sleeping
DataRaptor
commited on
Commit
•
7780f5c
1
Parent(s):
d7e7ba2
Upload chat.py
Browse files
chat.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
ctx_checker_tmp = st.slider('Context Checker Sensitivity', min_value=0.001, max_value=1.0, value=0.008, step=0.001)
|
76 |
+
lm_tmp = st.slider('Language Model Sensitivity', min_value=0.001, max_value=1.0, value=0.1, step=0.001)
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
endpoint = st.secrets["LLMEndpoint"]
|
82 |
+
|
83 |
+
|
84 |
+
def main():
|
85 |
+
if model_name == 'None':
|
86 |
+
st.markdown('##### Please select a model.')
|
87 |
+
return
|
88 |
+
|
89 |
+
# Initialize chat history
|
90 |
+
if "messages" not in st.session_state:
|
91 |
+
st.session_state.messages = [{"role": 'assistant', "content": 'হ্যালো! আমি একটি এআই অ্যাসিস্ট্যান্ট। কীভাবে সাহায্য করতে পারি? 😊'}]
|
92 |
+
|
93 |
+
# Display chat messages from history on app rerun
|
94 |
+
for message in st.session_state.messages:
|
95 |
+
with st.chat_message(message["role"]):
|
96 |
+
st.markdown(message["content"])
|
97 |
+
|
98 |
+
|
99 |
+
# Accept user input
|
100 |
+
if prompt := st.chat_input("এখানে মেসেজ লিখুন"):
|
101 |
+
# Display user message in chat message container
|
102 |
+
with st.chat_message("user"):
|
103 |
+
st.markdown(prompt)
|
104 |
+
# Add user message to chat history
|
105 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
106 |
+
|
107 |
+
|
108 |
+
## Get context
|
109 |
+
params = {
|
110 |
+
"chat_history": [
|
111 |
+
{"content": prompt}
|
112 |
+
],
|
113 |
+
"model": "bloom-7b",
|
114 |
+
"mode": "specific",
|
115 |
+
"config": {
|
116 |
+
"ctx_checker_tmp": ctx_checker_tmp,
|
117 |
+
"lm_tmp": lm_tmp,
|
118 |
+
}
|
119 |
+
}
|
120 |
+
resp = get_user_data(endpoint, params)
|
121 |
+
if resp == None:
|
122 |
+
st.markdown('#### INTERNAL ERROR')
|
123 |
+
return
|
124 |
+
|
125 |
+
response = resp['data']['responses'][0]['content']
|
126 |
+
context = resp['data']['logs']['content']['retrival_model']['matched_doc']
|
127 |
+
|
128 |
+
clen = len(context)
|
129 |
+
context = '\n\n===============================\n\n'.join(context)
|
130 |
+
|
131 |
+
response = f'###### Config: Context Checker Value: {ctx_checker_tmp}, LM Value: {lm_tmp}\n\n##### Matched Context: {clen}\n{context}\n\n##### Response:\n{response}'
|
132 |
+
|
133 |
+
# Display assistant response in chat message container
|
134 |
+
with st.chat_message("assistant", avatar=None):
|
135 |
+
st.markdown(response)
|
136 |
+
|
137 |
+
# Add assistant response to chat history
|
138 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
139 |
+
|
140 |
+
|
141 |
+
|
142 |
+
main()
|