Spaces:
Sleeping
Sleeping
hashirehtisham
commited on
Commit
•
79f89e6
1
Parent(s):
24c3761
Update app.py
Browse files
app.py
CHANGED
@@ -2,98 +2,116 @@ import streamlit as st
|
|
2 |
from ai71 import AI71
|
3 |
import os
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
system_message=st.session_state.system_message,
|
50 |
-
max_tokens=st.session_state.max_tokens,
|
51 |
-
temperature=st.session_state.temperature,
|
52 |
-
top_p=st.session_state.top_p,
|
53 |
-
)
|
54 |
-
st.session_state.history.append((message, response))
|
55 |
-
st.session_state["new_message"] = ""
|
56 |
-
|
57 |
-
# Streamlit UI
|
58 |
-
st.title("AI Lawyer Chatbot")
|
59 |
-
st.write("Welcome to your legal assistant. How can I assist you with your legal questions today?")
|
60 |
-
st.markdown("<p style='color:blue;'>Developed by Hashir Ehtisham</p>", unsafe_allow_html=True)
|
61 |
-
|
62 |
-
# Add description to sidebar
|
63 |
-
st.sidebar.markdown("""
|
64 |
-
### AI Lawyer Chatbot
|
65 |
-
Welcome to your AI Lawyer Chatbot, a cutting-edge tool designed to assist you with legal questions and provide insightful legal advice. Developed by Hashir Ehtisham, this application leverages advanced AI technology to offer accurate and relevant legal information.
|
66 |
-
|
67 |
-
#### Features:
|
68 |
-
- **Interactive Chat**: Engage in real-time conversations with the chatbot, designed to simulate interactions with a knowledgeable legal expert.
|
69 |
-
- **Customizable Settings**: Adjust the behavior of the AI with settings for max tokens, temperature, and top-p (nucleus sampling) to fine-tune responses according to your needs.
|
70 |
-
- **Chat History**: Review previous interactions with the chatbot directly in the app, keeping track of your legal inquiries and the AI's responses.
|
71 |
-
|
72 |
-
#### How It Works:
|
73 |
-
- **System Message**: The chatbot operates with a predefined system message that sets the context for its responses, ensuring it provides legal advice and information accurately.
|
74 |
-
- **Real-Time Interaction**: As you input your messages, the AI processes them and generates responses based on the ongoing conversation history and the selected settings.
|
75 |
-
- **Adaptive Response**: The chatbot's response can be adjusted by changing parameters such as the number of tokens generated, the randomness of the responses (temperature), and the focus on the most likely outcomes (top-p).
|
76 |
-
|
77 |
-
This tool is designed to enhance your understanding of legal matters and provide immediate support for your legal inquiries. Enjoy a seamless and informative interaction with your AI-powered legal assistant!
|
78 |
""")
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
temperature_options = [0.1, 0.3, 0.5, 0.7, 1.0, 1.5, 2.0, 3.0, 4.0]
|
87 |
-
top_p_options = [0.1, 0.2, 0.3, 0.5, 0.7, 0.8, 0.9, 0.95, 1.0]
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
top_p = st.selectbox("Top-p (nucleus sampling)", options=top_p_options, index=top_p_options.index(0.95), key="top_p")
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
st.
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from ai71 import AI71
|
3 |
import os
|
4 |
|
5 |
+
# Access the API key from Streamlit secrets
|
6 |
+
ai71_api_key = st.secrets["AI71_API_KEY"]
|
7 |
+
|
8 |
+
# Initialize the AI71 client with the API key
|
9 |
+
client = AI71(ai71_api_key)
|
10 |
+
|
11 |
+
# Set page config with title and favicon
|
12 |
+
st.set_page_config(
|
13 |
+
page_title="AI Lawyer 🧑⚖️",
|
14 |
+
page_icon="assets/lawyer_icon.png", # Replace with your favicon path
|
15 |
+
)
|
16 |
+
|
17 |
+
# Add custom CSS for styling
|
18 |
+
st.markdown(
|
19 |
+
"""
|
20 |
+
<style>
|
21 |
+
.main {
|
22 |
+
background-color: #f0f4f8;
|
23 |
+
}
|
24 |
+
.sidebar .sidebar-content {
|
25 |
+
background-color: #003366;
|
26 |
+
}
|
27 |
+
.stButton>button {
|
28 |
+
color: #FFFFFF;
|
29 |
+
background-color: #003366;
|
30 |
+
}
|
31 |
+
.stChatMessage--assistant {
|
32 |
+
background-color: #e0f7fa;
|
33 |
+
}
|
34 |
+
.stChatMessage--user {
|
35 |
+
background-color: #ffffff;
|
36 |
+
}
|
37 |
+
.title {
|
38 |
+
color: #003366;
|
39 |
+
}
|
40 |
+
</style>
|
41 |
+
""",
|
42 |
+
unsafe_allow_html=True
|
43 |
+
)
|
44 |
+
|
45 |
+
# Sidebar
|
46 |
+
st.sidebar.image("assets/lawyer_icon.png", use_column_width=True) # Replace with your image path
|
47 |
+
st.sidebar.write("""
|
48 |
+
**AI Lawyer** is your intelligent assistant for legal advice and information. Powered by advanced AI technology, AI Lawyer helps you understand legal matters by providing detailed insights and potential solutions. Whether you have questions about contracts, disputes, or legal procedures, AI Lawyer is here to assist you.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
""")
|
50 |
|
51 |
+
st.sidebar.header("How to Use AI Lawyer")
|
52 |
+
st.sidebar.write("""
|
53 |
+
1. **Enter Your Legal Question**:
|
54 |
+
- Provide your legal query or describe the legal issue you are experiencing.
|
55 |
|
56 |
+
2. **Submit the Question**:
|
57 |
+
- Use the input field at the bottom of the page to enter your query.
|
|
|
|
|
58 |
|
59 |
+
3. **Get a Response**:
|
60 |
+
- AI Lawyer will process your input and generate a detailed response with relevant legal information and advice.
|
|
|
61 |
|
62 |
+
4. **Review and Take Action**:
|
63 |
+
- Read the response provided by AI Lawyer and follow the suggested advice. Consult with a legal professional for further assistance if needed.
|
64 |
+
""")
|
65 |
+
st.sidebar.markdown("### Social Links:")
|
66 |
+
st.sidebar.write("🔗 [GitHub](https://www.github.com)")
|
67 |
+
|
68 |
+
# Show title and description.
|
69 |
+
st.markdown('<h1 class="title">AI Lawyer 🧑⚖️</h1>', unsafe_allow_html=True)
|
70 |
+
st.write(
|
71 |
+
"This is your AI Lawyer that uses the AI71 model to provide legal advice and information."
|
72 |
+
)
|
73 |
+
|
74 |
+
# Create a session state variable to store the chat messages. This ensures that the
|
75 |
+
# messages persist across reruns.
|
76 |
+
if "messages" not in st.session_state:
|
77 |
+
st.session_state.messages = []
|
78 |
+
instruction = "Hi! This is your AI Lawyer 🧑⚖️. Please describe your legal question or issue. For example: 'I need help understanding a contract clause.'"
|
79 |
+
st.session_state.messages.append({"role": "assistant", "content": instruction})
|
80 |
+
|
81 |
+
# Display the existing chat messages via st.chat_message.
|
82 |
+
for message in st.session_state.messages:
|
83 |
+
with st.chat_message(message["role"]):
|
84 |
+
st.markdown(message["content"])
|
85 |
+
|
86 |
+
# Create a chat input field to allow the user to enter a message. This will display
|
87 |
+
# automatically at the bottom of the page.
|
88 |
+
if prompt := st.chat_input("What legal question or issue do you need help with?"):
|
89 |
+
|
90 |
+
# Store and display the current prompt.
|
91 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
92 |
+
with st.chat_message("user"):
|
93 |
+
st.markdown(prompt)
|
94 |
+
|
95 |
+
# Generate a response using the AI71 API.
|
96 |
+
with st.spinner("Generating response..."):
|
97 |
+
try:
|
98 |
+
response = client.chat.completions.create(
|
99 |
+
model="tiiuae/falcon-180B-chat",
|
100 |
+
messages=[
|
101 |
+
{"role": "user", "content": prompt}
|
102 |
+
],
|
103 |
+
max_tokens=st.session_state.max_tokens,
|
104 |
+
temperature=st.session_state.temperature,
|
105 |
+
top_p=st.session_state.top_p
|
106 |
+
)
|
107 |
+
# Collect and concatenate response chunks
|
108 |
+
if response.choices and response.choices[0].message:
|
109 |
+
full_response = response.choices[0].message.content
|
110 |
+
|
111 |
+
# Stream the full response to the chat using st.write
|
112 |
+
with st.chat_message("assistant"):
|
113 |
+
st.markdown(full_response)
|
114 |
+
|
115 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
116 |
+
except Exception as e:
|
117 |
+
st.error(f"An error occurred: {e}")
|