muzammil-eds
commited on
Commit
•
67be838
1
Parent(s):
a742da5
Upload 2 files
Browse files- app.py +109 -0
- requirements.txt +25 -0
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import pickle
|
4 |
+
import time
|
5 |
+
import requests
|
6 |
+
|
7 |
+
st.set_page_config(page_title="LEGAL ASSISTANT")
|
8 |
+
|
9 |
+
st.markdown(
|
10 |
+
"""
|
11 |
+
<style>
|
12 |
+
.title {
|
13 |
+
text-align: center;
|
14 |
+
font-size: 2em;
|
15 |
+
font-weight: bold;
|
16 |
+
}
|
17 |
+
</style>
|
18 |
+
<div class="title"> Psychedelics Chatbot </div>
|
19 |
+
""",
|
20 |
+
unsafe_allow_html=True
|
21 |
+
)
|
22 |
+
# Load and Save Conversations
|
23 |
+
conversations_file = "conversations.pkl"
|
24 |
+
|
25 |
+
|
26 |
+
@st.cache_data
|
27 |
+
def load_conversations():
|
28 |
+
try:
|
29 |
+
with open(conversations_file, "rb") as f:
|
30 |
+
return pickle.load(f)
|
31 |
+
except (FileNotFoundError, EOFError):
|
32 |
+
return []
|
33 |
+
|
34 |
+
|
35 |
+
def save_conversations(conversations):
|
36 |
+
temp_conversations_file = conversations_file
|
37 |
+
with open(temp_conversations_file, "wb") as f:
|
38 |
+
pickle.dump(conversations, f)
|
39 |
+
os.replace(temp_conversations_file, conversations_file)
|
40 |
+
|
41 |
+
|
42 |
+
if 'conversations' not in st.session_state:
|
43 |
+
st.session_state.conversations = load_conversations()
|
44 |
+
|
45 |
+
if 'current_conversation' not in st.session_state:
|
46 |
+
st.session_state.current_conversation = [{"role": "assistant", "content": "How may I assist you today?"}]
|
47 |
+
|
48 |
+
|
49 |
+
def truncate_string(s, length=30):
|
50 |
+
return s[:length].rstrip() + "..." if len(s) > length else s
|
51 |
+
|
52 |
+
|
53 |
+
def display_chats_sidebar():
|
54 |
+
with st.sidebar.container():
|
55 |
+
st.header('Settings')
|
56 |
+
col1, col2 = st.columns([1, 1])
|
57 |
+
|
58 |
+
with col1:
|
59 |
+
if col1.button('Start New Chat', key="new_chat"):
|
60 |
+
st.session_state.current_conversation = []
|
61 |
+
st.session_state.conversations.append(st.session_state.current_conversation)
|
62 |
+
|
63 |
+
with col2:
|
64 |
+
if col2.button('Clear All Chats', key="clear_all"):
|
65 |
+
st.session_state.conversations = []
|
66 |
+
st.session_state.current_conversation = []
|
67 |
+
|
68 |
+
with st.sidebar.container():
|
69 |
+
st.header('Conversations')
|
70 |
+
for idx, conversation in enumerate(st.session_state.conversations):
|
71 |
+
if conversation:
|
72 |
+
chat_title_raw = next((msg["content"] for msg in conversation if msg["role"] == "user"), "New Chat")
|
73 |
+
chat_title = truncate_string(chat_title_raw)
|
74 |
+
if st.sidebar.button(f"{chat_title}", key=f"chat_button_{idx}"):
|
75 |
+
st.session_state.current_conversation = st.session_state.conversations[idx]
|
76 |
+
|
77 |
+
|
78 |
+
def main_app():
|
79 |
+
for message in st.session_state.current_conversation:
|
80 |
+
with st.chat_message(message["role"]):
|
81 |
+
st.write(message["content"])
|
82 |
+
|
83 |
+
def generate_response(prompt_input):
|
84 |
+
json = {
|
85 |
+
|
86 |
+
"user_prompt": prompt_input,
|
87 |
+
"chat_history": []
|
88 |
+
|
89 |
+
}
|
90 |
+
response = requests.post('http://18.190.164.123:8000/generate', json=json)
|
91 |
+
|
92 |
+
return response.json()
|
93 |
+
|
94 |
+
if prompt := st.chat_input('Send a Message'):
|
95 |
+
st.session_state.current_conversation.append({"role": "user", "content": prompt})
|
96 |
+
with st.chat_message("user"):
|
97 |
+
st.write(prompt)
|
98 |
+
|
99 |
+
with st.chat_message("assistant"):
|
100 |
+
with st.spinner("Thinking..."):
|
101 |
+
response = generate_response(prompt)
|
102 |
+
st.markdown(response['response'])
|
103 |
+
st.session_state.current_conversation.append({"role": "assistant", "content": response['response']})
|
104 |
+
save_conversations(st.session_state.conversations)
|
105 |
+
|
106 |
+
|
107 |
+
display_chats_sidebar()
|
108 |
+
|
109 |
+
main_app()
|
requirements.txt
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.99.1
|
2 |
+
uvicorn==0.23.2
|
3 |
+
langchain==v0.0.331rc2
|
4 |
+
joblib==1.2.0
|
5 |
+
numpy==1.23.5
|
6 |
+
pandas==2.0.3
|
7 |
+
gtts==2.3.1
|
8 |
+
pydantic==1.10.8
|
9 |
+
scikit-learn==1.0.2
|
10 |
+
pypdf
|
11 |
+
tiktoken
|
12 |
+
sentence-transformers
|
13 |
+
faiss-cpu
|
14 |
+
pytesseract
|
15 |
+
unstructured_inference
|
16 |
+
unstructured==0.7.12
|
17 |
+
boto3
|
18 |
+
text_generation
|
19 |
+
python-multipart
|
20 |
+
starlette==0.27.0
|
21 |
+
openai==1.3.5
|
22 |
+
langchain-openai
|
23 |
+
langchain-community
|
24 |
+
pymupdf
|
25 |
+
rank_bm25
|