Spaces:
Running
Running
Pouya
commited on
Upload 8 files
Browse files- .gitattributes +1 -0
- docs/chroma/5c10dafd-1187-4219-9cb0-87390a0ae1e9/data_level0.bin +3 -0
- docs/chroma/5c10dafd-1187-4219-9cb0-87390a0ae1e9/header.bin +3 -0
- docs/chroma/5c10dafd-1187-4219-9cb0-87390a0ae1e9/index_metadata.pickle +3 -0
- docs/chroma/5c10dafd-1187-4219-9cb0-87390a0ae1e9/length.bin +3 -0
- docs/chroma/5c10dafd-1187-4219-9cb0-87390a0ae1e9/link_lists.bin +3 -0
- docs/chroma/chroma.sqlite3 +3 -0
- requirements.txt +5 -0
- st.py +287 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
docs/chroma/chroma.sqlite3 filter=lfs diff=lfs merge=lfs -text
|
docs/chroma/5c10dafd-1187-4219-9cb0-87390a0ae1e9/data_level0.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:94c3f18968426efe81a23364bb7ba45d933952f25ef9e8d9c60a2b204880b139
|
3 |
+
size 131964000
|
docs/chroma/5c10dafd-1187-4219-9cb0-87390a0ae1e9/header.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7c4c962acb5d8678618eb6ef96da7484962d07b48b90e23ac4e2e8c0233ba7a4
|
3 |
+
size 100
|
docs/chroma/5c10dafd-1187-4219-9cb0-87390a0ae1e9/index_metadata.pickle
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:37b1b2f43d7d9360d651dc7b21968a7b05eb65bdf9b655d54b297cb6da6ccca7
|
3 |
+
size 1216256
|
docs/chroma/5c10dafd-1187-4219-9cb0-87390a0ae1e9/length.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7c2d94e7db095bf1339170726bf8e580db1670ecfb43f49d0865c77cccdde6c8
|
3 |
+
size 84000
|
docs/chroma/5c10dafd-1187-4219-9cb0-87390a0ae1e9/link_lists.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:df5c6ee3ef47d809c2d7429431d1211ec81adec2e0b4fa8caddad462c0604e80
|
3 |
+
size 180288
|
docs/chroma/chroma.sqlite3
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2c60bee85b2488e4f54ddbc6e5b6a96fe0cb4d67f732c9ac4d1128b7186e02cd
|
3 |
+
size 229933056
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
streamlit-chat
|
3 |
+
langchain
|
4 |
+
chromadb
|
5 |
+
pysqlite3-binary
|
st.py
ADDED
@@ -0,0 +1,287 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.vectorstores import Chroma
|
5 |
+
from langchain.chat_models import ChatOpenAI
|
6 |
+
from langchain.chains import RetrievalQA, LLMChain
|
7 |
+
from langchain import PromptTemplate
|
8 |
+
from langchain.memory import ConversationBufferMemory
|
9 |
+
from langchain.agents import initialize_agent, Tool, AgentExecutor
|
10 |
+
from langchain.text_splitter import CharacterTextSplitter
|
11 |
+
import chromadb
|
12 |
+
|
13 |
+
|
14 |
+
# API
|
15 |
+
openai_api_key = st.secrets["OPENAI_API_KEY"]
|
16 |
+
|
17 |
+
|
18 |
+
# Define the path to your document files
|
19 |
+
file1 = "./DIVISION OF ASSETS AFTER DIVORCE.txt"
|
20 |
+
file2 = "./INHERITANCE.txt"
|
21 |
+
|
22 |
+
|
23 |
+
# Function to initialize the OpenAI embeddings and model
|
24 |
+
def openai_setting():
|
25 |
+
embedding = OpenAIEmbeddings()
|
26 |
+
model_name = "gpt-3.5-turbo"
|
27 |
+
llm = ChatOpenAI(model_name=model_name, temperature=0)
|
28 |
+
return embedding, llm
|
29 |
+
|
30 |
+
|
31 |
+
# Function to split the law content
|
32 |
+
def law_content_splitter(path, splitter="CIVIL CODE"):
|
33 |
+
with open(path) as f:
|
34 |
+
law_content = f.read()
|
35 |
+
law_content_by_article = law_content.split(splitter)[1:]
|
36 |
+
text_splitter = CharacterTextSplitter()
|
37 |
+
return text_splitter.create_documents(law_content_by_article)
|
38 |
+
|
39 |
+
|
40 |
+
# Splitting the content of law documents
|
41 |
+
divorce_splitted = law_content_splitter(file1)
|
42 |
+
inheritance_splitted = law_content_splitter(file2)
|
43 |
+
|
44 |
+
# Initializing embedding and language model
|
45 |
+
embedding, llm = openai_setting()
|
46 |
+
|
47 |
+
# Define the prompts
|
48 |
+
divorce_prompt = """As a specialized bot in divorce law, you should offer accurate insights on Italian divorce regulations.
|
49 |
+
You should always cite the article numbers you reference.
|
50 |
+
Ensure you provide detailed and exact data.
|
51 |
+
If a query doesn't pertain to the legal documents, you should remind the user that it falls outside your expertise.
|
52 |
+
You should be adept at discussing the various Italian divorce categories, including fault-based divorce, mutual-consent divorce, and divorce due to infidelity.
|
53 |
+
You should guide users through the prerequisites and procedures of each divorce type, detailing the essential paperwork, expected duration, and potential legal repercussions.
|
54 |
+
You should capably address queries regarding asset allocation, child custody, spousal support, and other financial concerns related to divorce, all while staying true to Italian legislation.
|
55 |
+
{context}
|
56 |
+
|
57 |
+
Question: {question}"""
|
58 |
+
DIVORCE_BOT_PROMPT = PromptTemplate(
|
59 |
+
template=divorce_prompt, input_variables=["context", "question"]
|
60 |
+
)
|
61 |
+
|
62 |
+
|
63 |
+
# define inheritance prompt
|
64 |
+
inheritance_prompt = """As a specialist in Italian inheritance law, you should deliver detailed and accurate insights about inheritance regulations in Italy.
|
65 |
+
You should always cite the article numbers you reference.
|
66 |
+
When responding to user queries, you should always base your answers on the provided context.
|
67 |
+
Always cite the specific article numbers you mention and refrain from speculating.
|
68 |
+
Maintain precision in all your responses.
|
69 |
+
If a user's question doesn't align with the legal documents, you should point out that it's beyond your domain of expertise.
|
70 |
+
You should elucidate Italian inheritance law comprehensively, touching on topics such as testamentary inheritance, intestate inheritance, and other pertinent subjects.
|
71 |
+
Make sure to elaborate on the obligations and rights of inheritors, the methodology of estate distribution, asset assessment, and settling debts, all while adhering to Italian law specifics.
|
72 |
+
You should adeptly tackle questions about various will forms like holographic or notarial wills, ensuring you clarify their legitimacy within Italian jurisdiction.
|
73 |
+
Offer advice on creating a will, naming heirs, and managing potential conflicts.
|
74 |
+
You should provide detailed information on tax nuances associated with inheritance in Italy, inclusive of exemptions, tax rates, and mandatory disclosures.
|
75 |
+
|
76 |
+
{context}
|
77 |
+
|
78 |
+
Question: {question}"""
|
79 |
+
INHERITANCE_BOT_PROMPT = PromptTemplate(
|
80 |
+
template=inheritance_prompt, input_variables=["context", "question"]
|
81 |
+
)
|
82 |
+
|
83 |
+
|
84 |
+
# Setup for Chroma databases and RetrievalQA
|
85 |
+
|
86 |
+
chroma_directory = "./docs/chroma/"
|
87 |
+
|
88 |
+
|
89 |
+
inheritance_db = Chroma.from_documents(
|
90 |
+
documents=inheritance_splitted,
|
91 |
+
embedding=embedding,
|
92 |
+
persist_directory=chroma_directory,
|
93 |
+
)
|
94 |
+
|
95 |
+
inheritance = RetrievalQA.from_chain_type(
|
96 |
+
llm=llm,
|
97 |
+
chain_type="stuff",
|
98 |
+
retriever=inheritance_db.as_retriever(),
|
99 |
+
chain_type_kwargs={"prompt": INHERITANCE_BOT_PROMPT},
|
100 |
+
)
|
101 |
+
|
102 |
+
|
103 |
+
divorce_db = Chroma.from_documents(
|
104 |
+
documents=divorce_splitted, embedding=embedding, persist_directory=chroma_directory
|
105 |
+
)
|
106 |
+
|
107 |
+
|
108 |
+
divorce = RetrievalQA.from_chain_type(
|
109 |
+
llm=llm,
|
110 |
+
chain_type="stuff",
|
111 |
+
retriever=divorce_db.as_retriever(),
|
112 |
+
chain_type_kwargs={"prompt": DIVORCE_BOT_PROMPT},
|
113 |
+
)
|
114 |
+
|
115 |
+
|
116 |
+
# Define the tools for the chatbot
|
117 |
+
tools = [
|
118 |
+
Tool(
|
119 |
+
name="Divorce Italian law QA System",
|
120 |
+
func=divorce.run,
|
121 |
+
description="useful for when you need to answer questions about divorce laws in Italy.Give also the number of article you use for it.",
|
122 |
+
),
|
123 |
+
Tool(
|
124 |
+
name="Inheritance Italian law QA System",
|
125 |
+
func=inheritance.run,
|
126 |
+
description="useful for when you need to answer questions about inheritance laws in Italy.Give also the number of article you use for it.",
|
127 |
+
),
|
128 |
+
]
|
129 |
+
|
130 |
+
# Initialize conversation memory
|
131 |
+
memory = ConversationBufferMemory(
|
132 |
+
memory_key="chat_history", input_key="input", output_key="output"
|
133 |
+
)
|
134 |
+
|
135 |
+
|
136 |
+
# initialize ReAct agent
|
137 |
+
react = initialize_agent(tools, llm, agent="zero-shot-react-description")
|
138 |
+
|
139 |
+
agent = AgentExecutor.from_agent_and_tools(
|
140 |
+
tools=tools, agent=react.agent, memory=memory, verbose=False
|
141 |
+
)
|
142 |
+
|
143 |
+
|
144 |
+
# Define the chatbot function
|
145 |
+
# question = "I'm getting divorced,what's happen at my children"
|
146 |
+
|
147 |
+
|
148 |
+
def questions(question):
|
149 |
+
return agent.run(question)
|
150 |
+
|
151 |
+
|
152 |
+
def chatbot1(question):
|
153 |
+
try:
|
154 |
+
return questions(question)
|
155 |
+
|
156 |
+
except:
|
157 |
+
return "I'm sorry, I'm having trouble understanding your question. Could you please rephrase it or provide more context"
|
158 |
+
|
159 |
+
|
160 |
+
def is_greeting(input_str):
|
161 |
+
"""Check if the input is a greeting."""
|
162 |
+
greetings = [
|
163 |
+
"hello",
|
164 |
+
"hi",
|
165 |
+
"hey",
|
166 |
+
"greetings",
|
167 |
+
"good morning",
|
168 |
+
"good afternoon",
|
169 |
+
"good evening",
|
170 |
+
"hi there",
|
171 |
+
"hello there",
|
172 |
+
"hey there",
|
173 |
+
"howdy",
|
174 |
+
"sup",
|
175 |
+
"what's up",
|
176 |
+
"how's it going",
|
177 |
+
"how are you",
|
178 |
+
"good day",
|
179 |
+
"salutations",
|
180 |
+
"hiya",
|
181 |
+
"yo",
|
182 |
+
"hola",
|
183 |
+
"bonjour",
|
184 |
+
"g'day",
|
185 |
+
"how do you do",
|
186 |
+
"what’s new",
|
187 |
+
"what’s up",
|
188 |
+
"how’s everything",
|
189 |
+
"how are things",
|
190 |
+
"how’s life",
|
191 |
+
"how’s your day",
|
192 |
+
"how’s your day going",
|
193 |
+
"good to see you",
|
194 |
+
"nice to see you",
|
195 |
+
"great to see you",
|
196 |
+
"lovely to see you",
|
197 |
+
"how have you been",
|
198 |
+
"what’s going on",
|
199 |
+
"what’s happening",
|
200 |
+
"what’s new",
|
201 |
+
"long time no see",
|
202 |
+
# Italian greetings
|
203 |
+
"ciao",
|
204 |
+
"salve",
|
205 |
+
"buongiorno",
|
206 |
+
"buona sera",
|
207 |
+
"buonasera",
|
208 |
+
"buon pomeriggio",
|
209 |
+
"buonpomeriggio",
|
210 |
+
"come stai",
|
211 |
+
"comestai",
|
212 |
+
"come va",
|
213 |
+
"comeva",
|
214 |
+
"come sta",
|
215 |
+
"comesta",
|
216 |
+
"piacere di conoscerti",
|
217 |
+
"piacere",
|
218 |
+
"benvenuto",
|
219 |
+
"ben trovato",
|
220 |
+
]
|
221 |
+
return any(greet in input_str.lower() for greet in greetings)
|
222 |
+
|
223 |
+
|
224 |
+
def chatbot(input_str):
|
225 |
+
# Check for greetings first
|
226 |
+
if is_greeting(input_str):
|
227 |
+
return "Hello! Ask me your question about Italian Divorce or Inheritance Law?"
|
228 |
+
|
229 |
+
# Existing chatbot logic
|
230 |
+
response = chatbot1(input_str)
|
231 |
+
if response == "N/A":
|
232 |
+
return "I'm sorry, I'm having trouble understanding your question. Could you please rephrase it or provide more context"
|
233 |
+
else:
|
234 |
+
return response
|
235 |
+
|
236 |
+
|
237 |
+
# Streamlit Chat UI
|
238 |
+
st.set_page_config(
|
239 |
+
page_title="Italian Law Chatbot",
|
240 |
+
page_icon="⚖️",
|
241 |
+
layout="centered",
|
242 |
+
initial_sidebar_state="auto",
|
243 |
+
)
|
244 |
+
|
245 |
+
st.title("Italian Law Chatbot 🏛️")
|
246 |
+
st.info(
|
247 |
+
"Check out the full tutorial to build this app in our [📝 blog post](https://blog.streamlit.io/build-a-chatbot-with-custom-data-sources-powered-by-llamaindex/) — "
|
248 |
+
"[GitHub Repository](https://sattari.org)",
|
249 |
+
icon="ℹ️",
|
250 |
+
)
|
251 |
+
|
252 |
+
|
253 |
+
st.success(
|
254 |
+
"Check out [Prompt Examples List](https://blog.streamlit.io/build-a-chatbot-with-custom-data-sources-powered-by-llamaindex/) to learn how to interact with this ChatBot 🤗 ",
|
255 |
+
icon="✅",
|
256 |
+
)
|
257 |
+
|
258 |
+
# Initialize session state for conversation history
|
259 |
+
if "messages" not in st.session_state:
|
260 |
+
st.session_state.messages = [
|
261 |
+
{
|
262 |
+
"role": "assistant",
|
263 |
+
"content": "Hello! I'm here to help you with Italian Divorce or Inheritance Law. How can I assist you today?",
|
264 |
+
}
|
265 |
+
]
|
266 |
+
|
267 |
+
# Display previous messages
|
268 |
+
for message in st.session_state.messages:
|
269 |
+
with st.chat_message(message["role"]):
|
270 |
+
st.markdown(message["content"])
|
271 |
+
|
272 |
+
# Handle new user input
|
273 |
+
if user_input := st.chat_input(
|
274 |
+
"Ask a question about Italian Divorce or Inheritance Law:"
|
275 |
+
):
|
276 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
277 |
+
with st.chat_message("user"):
|
278 |
+
st.markdown(user_input)
|
279 |
+
|
280 |
+
# Generate and display chatbot response
|
281 |
+
with st.chat_message("assistant"):
|
282 |
+
response_placeholder = st.empty()
|
283 |
+
response = chatbot(user_input) # Your existing chatbot function
|
284 |
+
response_placeholder.markdown(response)
|
285 |
+
|
286 |
+
# Append the response to the conversation history
|
287 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|