Update app2.py
Browse files
app2.py
CHANGED
@@ -1888,141 +1888,6 @@ def policy_analysis_ui():
|
|
1888 |
else:
|
1889 |
st.warning("Please enter policy text or upload a document to analyze.")
|
1890 |
|
1891 |
-
def generate_web_search_query(user_input: str) -> str:
|
1892 |
-
"""Generates a web search query based on user input using Falcon."""
|
1893 |
-
prompt = f"""
|
1894 |
-
Generate a concise and effective web search query that can be used
|
1895 |
-
to find additional information about the following legal situation
|
1896 |
-
related to US law:
|
1897 |
-
|
1898 |
-
```
|
1899 |
-
{user_input}
|
1900 |
-
```
|
1901 |
-
"""
|
1902 |
-
|
1903 |
-
try:
|
1904 |
-
response = ai71.chat.completions.create(
|
1905 |
-
model="tiiuae/falcon-180b-chat",
|
1906 |
-
messages=[{"role": "user", "content": prompt}],
|
1907 |
-
stream=False,
|
1908 |
-
)
|
1909 |
-
return response.choices[0].message.content.strip()
|
1910 |
-
except Exception as e:
|
1911 |
-
print(f"Error generating web search query: {str(e)}")
|
1912 |
-
return user_input
|
1913 |
-
|
1914 |
-
def provide_legal_advice(user_input: str) -> Dict[str, Any]:
|
1915 |
-
"""Provides legal advice, suggestions, and generates a web search query."""
|
1916 |
-
chunks = split_text(user_input)
|
1917 |
-
full_advice = ""
|
1918 |
-
|
1919 |
-
for i, chunk in enumerate(chunks):
|
1920 |
-
advice_prompt = f"""
|
1921 |
-
Provide legal advice based on US law for the following situation or question (part {i+1}/{len(chunks)}):
|
1922 |
-
|
1923 |
-
User Input (Part {i+1}/{len(chunks)}):
|
1924 |
-
```
|
1925 |
-
{chunk}
|
1926 |
-
```
|
1927 |
-
|
1928 |
-
Please include:
|
1929 |
-
1. A summary of the legal issue or question
|
1930 |
-
2. Relevant US laws or regulations that apply
|
1931 |
-
3. Possible legal implications or consequences
|
1932 |
-
4. General advice or next steps (without constituting specific legal counsel)
|
1933 |
-
5. Any important disclaimers or limitations of this advice
|
1934 |
-
Remember to maintain a professional and objective tone throughout your response.
|
1935 |
-
|
1936 |
-
Suggestions: Provide 2-3 actionable suggestions that the user could consider based on the legal situation.
|
1937 |
-
"""
|
1938 |
-
|
1939 |
-
try:
|
1940 |
-
chunk_advice = get_ai_response(advice_prompt)
|
1941 |
-
full_advice += chunk_advice + "\n\n"
|
1942 |
-
except Exception as e:
|
1943 |
-
return {"error": f"Error providing legal advice (part {i+1}): {str(e)}"}
|
1944 |
-
|
1945 |
-
return {"advice": full_advice}
|
1946 |
-
|
1947 |
-
def legal_consultant_ui():
|
1948 |
-
st.subheader("Legal Consultant")
|
1949 |
-
|
1950 |
-
if 'consultant_chat_history' not in st.session_state:
|
1951 |
-
st.session_state.consultant_chat_history = []
|
1952 |
-
|
1953 |
-
if 'uploaded_document' not in st.session_state:
|
1954 |
-
st.session_state.uploaded_document = None
|
1955 |
-
|
1956 |
-
if 'consultant_chat_mode' not in st.session_state: # New variable for consultant chat mode
|
1957 |
-
st.session_state.consultant_chat_mode = "normal"
|
1958 |
-
|
1959 |
-
st.write('''
|
1960 |
-
Describe your legal situation or ask your legal question related to US law.
|
1961 |
-
LexAI will provide information and guidance based on its understanding of the US legal system.
|
1962 |
-
Please remember that this is not a substitute for real legal advice from a qualified attorney.
|
1963 |
-
''')
|
1964 |
-
|
1965 |
-
st.warning("Please do not upload files larger than 5MB as it may cause issues and consume all available tokens.")
|
1966 |
-
|
1967 |
-
uploaded_file = st.file_uploader("Upload a legal document (PDF, DOCX, or TXT)", type=["pdf", "docx", "txt"])
|
1968 |
-
|
1969 |
-
if uploaded_file:
|
1970 |
-
st.session_state.uploaded_document = extract_text_from_document(uploaded_file)
|
1971 |
-
st.success("Document uploaded successfully!")
|
1972 |
-
|
1973 |
-
# Chat mode toggle for Legal Consultant
|
1974 |
-
if st.session_state.uploaded_document:
|
1975 |
-
if st.button("Switch Chat Mode"):
|
1976 |
-
st.session_state.consultant_chat_mode = "document" if st.session_state.consultant_chat_mode == "normal" else "normal"
|
1977 |
-
|
1978 |
-
st.write(f"Current mode: {'Document-based' if st.session_state.consultant_chat_mode == 'document' else 'Normal'} chat")
|
1979 |
-
|
1980 |
-
# Display chat history (similar to Legal Chatbot)
|
1981 |
-
for message in st.session_state.consultant_chat_history:
|
1982 |
-
if isinstance(message, tuple):
|
1983 |
-
user_msg, bot_msg = message
|
1984 |
-
st.info(f"**You:** {user_msg}")
|
1985 |
-
st.success(f"**Lex AI:** {bot_msg}")
|
1986 |
-
elif isinstance(message, dict):
|
1987 |
-
if message.get('type') == 'web_search':
|
1988 |
-
web_results_msg = "Web Search Results:\n"
|
1989 |
-
for result in message.get('results', []):
|
1990 |
-
web_results_msg += f"[{result.get('title', 'No title')}]({result.get('link', '#')})\n{result.get('snippet', 'No snippet available.')}\n\n"
|
1991 |
-
st.success(f"**Lex AI:** {web_results_msg}")
|
1992 |
-
|
1993 |
-
user_input = st.text_input("Your legal question:")
|
1994 |
-
|
1995 |
-
if user_input and st.button("Send"):
|
1996 |
-
with st.spinner("Processing your question..."):
|
1997 |
-
if st.session_state.consultant_chat_mode == "document" and st.session_state.uploaded_document:
|
1998 |
-
ai_response = get_document_based_response(user_input, st.session_state.uploaded_document) # Assuming you have this function
|
1999 |
-
st.session_state.consultant_chat_history.append((user_input, ai_response))
|
2000 |
-
else:
|
2001 |
-
ai_response = provide_legal_advice(user_input)
|
2002 |
-
st.session_state.consultant_chat_history.append((user_input, ai_response))
|
2003 |
-
|
2004 |
-
# Perform web search (same as Legal Chatbot)
|
2005 |
-
web_results = search_web_duckduckgo(user_input) # Using user_input directly here
|
2006 |
-
st.session_state.consultant_chat_history.append({
|
2007 |
-
'type': 'web_search',
|
2008 |
-
'results': web_results
|
2009 |
-
})
|
2010 |
-
|
2011 |
-
st.rerun()
|
2012 |
-
|
2013 |
-
def display_chat_history_legal_advise():
|
2014 |
-
for message in st.session_state.chat_history:
|
2015 |
-
if isinstance(message, tuple):
|
2016 |
-
user_msg, bot_msg = message
|
2017 |
-
st.info(f"**You:** {user_msg}")
|
2018 |
-
st.success(f"**Lex AI:** {bot_msg}")
|
2019 |
-
elif isinstance(message, dict):
|
2020 |
-
if message.get('type') == 'web_search':
|
2021 |
-
web_results_msg = "Web Search Results:\n"
|
2022 |
-
for result in message.get('results', []):
|
2023 |
-
web_results_msg += f"[{result.get('title', 'No title')}]({result.get('link', '#')})\n{result.get('snippet', 'No snippet available.')}\n\n"
|
2024 |
-
st.success(f"**Lex AI:** {web_results_msg}")
|
2025 |
-
|
2026 |
def draft_contract(contract_details: str) -> Dict[str, Any]:
|
2027 |
chunks = split_text(contract_details)
|
2028 |
full_draft = ""
|
@@ -2238,7 +2103,7 @@ with st.sidebar:
|
|
2238 |
|
2239 |
feature = st.selectbox(
|
2240 |
"Select a feature",
|
2241 |
-
["Legal Chatbot", "Document Analysis", "Case Precedent Finder", "Legal Cost Estimator", "Contract Analysis", "Case Trend Visualizer", "Case Information Retrieval", "Automated Legal Brief Generation", "Find the Lawyers", "Policy Analysis & Impact", "
|
2242 |
)
|
2243 |
if feature == "Legal Chatbot":
|
2244 |
st.subheader("Legal Chatbot")
|
@@ -2408,11 +2273,9 @@ elif feature == "Automated Legal Brief Generation":
|
|
2408 |
|
2409 |
elif feature == "Find the Lawyers":
|
2410 |
lawyer_finder_ui()
|
|
|
2411 |
elif feature == "Policy Analysis & Impact":
|
2412 |
policy_analysis_ui()
|
2413 |
-
|
2414 |
-
elif feature == "Legal Consultant":
|
2415 |
-
legal_consultant_ui()
|
2416 |
|
2417 |
elif feature == "Contract Drafting Assistant":
|
2418 |
contract_drafting_ui()
|
|
|
1888 |
else:
|
1889 |
st.warning("Please enter policy text or upload a document to analyze.")
|
1890 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1891 |
def draft_contract(contract_details: str) -> Dict[str, Any]:
|
1892 |
chunks = split_text(contract_details)
|
1893 |
full_draft = ""
|
|
|
2103 |
|
2104 |
feature = st.selectbox(
|
2105 |
"Select a feature",
|
2106 |
+
["Legal Chatbot", "Document Analysis", "Case Precedent Finder", "Legal Cost Estimator", "Contract Analysis", "Case Trend Visualizer", "Case Information Retrieval", "Automated Legal Brief Generation", "Find the Lawyers", "Policy Analysis & Impact", "Contract Drafting Assistant", "Predictive Case Analysis"]
|
2107 |
)
|
2108 |
if feature == "Legal Chatbot":
|
2109 |
st.subheader("Legal Chatbot")
|
|
|
2273 |
|
2274 |
elif feature == "Find the Lawyers":
|
2275 |
lawyer_finder_ui()
|
2276 |
+
|
2277 |
elif feature == "Policy Analysis & Impact":
|
2278 |
policy_analysis_ui()
|
|
|
|
|
|
|
2279 |
|
2280 |
elif feature == "Contract Drafting Assistant":
|
2281 |
contract_drafting_ui()
|