Spaces:
Sleeping
Sleeping
del email
Browse files
app.py
CHANGED
@@ -13,26 +13,26 @@ from langchain_core.runnables import RunnableLambda
|
|
13 |
import requests
|
14 |
import json
|
15 |
|
16 |
-
#
|
17 |
st.set_page_config(page_title="Status Law Assistant", page_icon="⚖️")
|
18 |
|
19 |
-
#
|
20 |
if 'kb_info' not in st.session_state:
|
21 |
st.session_state.kb_info = {
|
22 |
'build_time': None,
|
23 |
'size': None
|
24 |
}
|
25 |
|
26 |
-
#
|
27 |
st.title("Status Law Legal Assistant")
|
28 |
if st.session_state.kb_info['build_time'] and st.session_state.kb_info['size']:
|
29 |
st.caption(f"(Knowledge base build time: {st.session_state.kb_info['build_time']:.2f} seconds, "
|
30 |
f"size: {st.session_state.kb_info['size']:.2f} MB)")
|
31 |
|
32 |
-
#
|
33 |
VECTOR_STORE_PATH = "vector_store"
|
34 |
|
35 |
-
# URLs
|
36 |
urls = [
|
37 |
"https://status.law",
|
38 |
"https://status.law/about",
|
@@ -48,15 +48,14 @@ urls = [
|
|
48 |
"https://status.law/faq"
|
49 |
]
|
50 |
|
51 |
-
#
|
52 |
try:
|
53 |
-
EMAIL_WEBHOOK = st.secrets["EMAIL_WEBHOOK"]
|
54 |
GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
|
55 |
except Exception as e:
|
56 |
st.error("Error loading secrets. Please check your configuration.")
|
57 |
st.stop()
|
58 |
|
59 |
-
#
|
60 |
@st.cache_resource
|
61 |
def init_models():
|
62 |
llm = ChatGroq(
|
@@ -69,7 +68,7 @@ def init_models():
|
|
69 |
)
|
70 |
return llm, embeddings
|
71 |
|
72 |
-
#
|
73 |
def build_knowledge_base(embeddings):
|
74 |
start_time = time.time()
|
75 |
|
@@ -96,7 +95,7 @@ def build_knowledge_base(embeddings):
|
|
96 |
end_time = time.time()
|
97 |
build_time = end_time - start_time
|
98 |
|
99 |
-
#
|
100 |
total_size = 0
|
101 |
for path, dirs, files in os.walk(VECTOR_STORE_PATH):
|
102 |
for f in files:
|
@@ -104,7 +103,7 @@ def build_knowledge_base(embeddings):
|
|
104 |
total_size += os.path.getsize(fp)
|
105 |
size_mb = total_size / (1024 * 1024)
|
106 |
|
107 |
-
#
|
108 |
st.session_state.kb_info['build_time'] = build_time
|
109 |
st.session_state.kb_info['size'] = size_mb
|
110 |
|
@@ -117,33 +116,12 @@ def build_knowledge_base(embeddings):
|
|
117 |
|
118 |
return vector_store
|
119 |
|
120 |
-
#
|
121 |
-
def send_chat_history(history):
|
122 |
-
try:
|
123 |
-
body = "\n\n".join([
|
124 |
-
f"Q: {item['question']}\nA: {item['answer']}"
|
125 |
-
for item in history
|
126 |
-
])
|
127 |
-
|
128 |
-
response = requests.post(
|
129 |
-
EMAIL_WEBHOOK,
|
130 |
-
json={
|
131 |
-
'subject': 'Chat History Update',
|
132 |
-
'body': body
|
133 |
-
},
|
134 |
-
headers={'Content-Type': 'application/json'}
|
135 |
-
)
|
136 |
-
if response.status_code != 200:
|
137 |
-
st.error(f"Failed to send email through webhook: {response.text}")
|
138 |
-
except Exception as e:
|
139 |
-
st.error(f"Failed to send email: {str(e)}")
|
140 |
-
|
141 |
-
# Основной код
|
142 |
def main():
|
143 |
-
#
|
144 |
llm, embeddings = init_models()
|
145 |
|
146 |
-
#
|
147 |
if not os.path.exists(VECTOR_STORE_PATH):
|
148 |
st.warning("Knowledge base not found.")
|
149 |
if st.button("Create Knowledge Base"):
|
@@ -158,21 +136,21 @@ def main():
|
|
158 |
allow_dangerous_deserialization=True
|
159 |
)
|
160 |
|
161 |
-
#
|
162 |
if 'vector_store' in st.session_state:
|
163 |
if 'messages' not in st.session_state:
|
164 |
st.session_state.messages = []
|
165 |
|
166 |
-
#
|
167 |
for message in st.session_state.messages:
|
168 |
st.chat_message("user").write(message["question"])
|
169 |
st.chat_message("assistant").write(message["answer"])
|
170 |
|
171 |
-
#
|
172 |
if question := st.chat_input("Ask your question"):
|
173 |
st.chat_message("user").write(question)
|
174 |
|
175 |
-
#
|
176 |
with st.chat_message("assistant"):
|
177 |
with st.spinner("Thinking..."):
|
178 |
context = st.session_state.vector_store.similarity_search(question)
|
@@ -196,14 +174,11 @@ def main():
|
|
196 |
|
197 |
st.write(response)
|
198 |
|
199 |
-
#
|
200 |
st.session_state.messages.append({
|
201 |
"question": question,
|
202 |
"answer": response
|
203 |
})
|
204 |
-
|
205 |
-
# Отправка email
|
206 |
-
send_chat_history(st.session_state.messages)
|
207 |
|
208 |
if __name__ == "__main__":
|
209 |
-
main()
|
|
|
13 |
import requests
|
14 |
import json
|
15 |
|
16 |
+
# Page configuration
|
17 |
st.set_page_config(page_title="Status Law Assistant", page_icon="⚖️")
|
18 |
|
19 |
+
# Knowledge base info in session_state
|
20 |
if 'kb_info' not in st.session_state:
|
21 |
st.session_state.kb_info = {
|
22 |
'build_time': None,
|
23 |
'size': None
|
24 |
}
|
25 |
|
26 |
+
# Display title and knowledge base info
|
27 |
st.title("Status Law Legal Assistant")
|
28 |
if st.session_state.kb_info['build_time'] and st.session_state.kb_info['size']:
|
29 |
st.caption(f"(Knowledge base build time: {st.session_state.kb_info['build_time']:.2f} seconds, "
|
30 |
f"size: {st.session_state.kb_info['size']:.2f} MB)")
|
31 |
|
32 |
+
# Path to store vector database
|
33 |
VECTOR_STORE_PATH = "vector_store"
|
34 |
|
35 |
+
# Website URLs
|
36 |
urls = [
|
37 |
"https://status.law",
|
38 |
"https://status.law/about",
|
|
|
48 |
"https://status.law/faq"
|
49 |
]
|
50 |
|
51 |
+
# Load secrets
|
52 |
try:
|
|
|
53 |
GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
|
54 |
except Exception as e:
|
55 |
st.error("Error loading secrets. Please check your configuration.")
|
56 |
st.stop()
|
57 |
|
58 |
+
# Initialize models
|
59 |
@st.cache_resource
|
60 |
def init_models():
|
61 |
llm = ChatGroq(
|
|
|
68 |
)
|
69 |
return llm, embeddings
|
70 |
|
71 |
+
# Build knowledge base
|
72 |
def build_knowledge_base(embeddings):
|
73 |
start_time = time.time()
|
74 |
|
|
|
95 |
end_time = time.time()
|
96 |
build_time = end_time - start_time
|
97 |
|
98 |
+
# Calculate knowledge base size
|
99 |
total_size = 0
|
100 |
for path, dirs, files in os.walk(VECTOR_STORE_PATH):
|
101 |
for f in files:
|
|
|
103 |
total_size += os.path.getsize(fp)
|
104 |
size_mb = total_size / (1024 * 1024)
|
105 |
|
106 |
+
# Save knowledge base info
|
107 |
st.session_state.kb_info['build_time'] = build_time
|
108 |
st.session_state.kb_info['size'] = size_mb
|
109 |
|
|
|
116 |
|
117 |
return vector_store
|
118 |
|
119 |
+
# Main function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
def main():
|
121 |
+
# Initialize models
|
122 |
llm, embeddings = init_models()
|
123 |
|
124 |
+
# Check if knowledge base exists
|
125 |
if not os.path.exists(VECTOR_STORE_PATH):
|
126 |
st.warning("Knowledge base not found.")
|
127 |
if st.button("Create Knowledge Base"):
|
|
|
136 |
allow_dangerous_deserialization=True
|
137 |
)
|
138 |
|
139 |
+
# Chat mode
|
140 |
if 'vector_store' in st.session_state:
|
141 |
if 'messages' not in st.session_state:
|
142 |
st.session_state.messages = []
|
143 |
|
144 |
+
# Display chat history
|
145 |
for message in st.session_state.messages:
|
146 |
st.chat_message("user").write(message["question"])
|
147 |
st.chat_message("assistant").write(message["answer"])
|
148 |
|
149 |
+
# User input
|
150 |
if question := st.chat_input("Ask your question"):
|
151 |
st.chat_message("user").write(question)
|
152 |
|
153 |
+
# Retrieve context and generate response
|
154 |
with st.chat_message("assistant"):
|
155 |
with st.spinner("Thinking..."):
|
156 |
context = st.session_state.vector_store.similarity_search(question)
|
|
|
174 |
|
175 |
st.write(response)
|
176 |
|
177 |
+
# Save chat history
|
178 |
st.session_state.messages.append({
|
179 |
"question": question,
|
180 |
"answer": response
|
181 |
})
|
|
|
|
|
|
|
182 |
|
183 |
if __name__ == "__main__":
|
184 |
+
main()
|