Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from ai71 import AI71
|
3 |
+
|
4 |
+
# Initialize AI71 client
|
5 |
+
AI71_API_KEY = "ai71-api-d95427d8-ce80-42cb-8117-cd12ecfe7907" # Your API key
|
6 |
+
client = AI71(AI71_API_KEY)
|
7 |
+
|
8 |
+
def respond(
|
9 |
+
message,
|
10 |
+
history: list[tuple[str, str]],
|
11 |
+
system_message,
|
12 |
+
max_tokens,
|
13 |
+
temperature,
|
14 |
+
top_p,
|
15 |
+
):
|
16 |
+
messages = [{"role": "system", "content": system_message}]
|
17 |
+
|
18 |
+
for val in history:
|
19 |
+
if val[0]:
|
20 |
+
messages.append({"role": "user", "content": val[0]})
|
21 |
+
if val[1]:
|
22 |
+
messages.append({"role": "assistant", "content": val[1]})
|
23 |
+
|
24 |
+
messages.append({"role": "user", "content": message})
|
25 |
+
|
26 |
+
response = ""
|
27 |
+
|
28 |
+
for chunk in client.chat.completions.create(
|
29 |
+
model="tiiuae/falcon-180B-chat",
|
30 |
+
messages=messages,
|
31 |
+
max_tokens=max_tokens,
|
32 |
+
temperature=temperature,
|
33 |
+
top_p=top_p,
|
34 |
+
stream=True,
|
35 |
+
):
|
36 |
+
delta_content = chunk.choices[0].delta.content
|
37 |
+
if delta_content:
|
38 |
+
response += delta_content
|
39 |
+
|
40 |
+
return response
|
41 |
+
|
42 |
+
def send_message():
|
43 |
+
message = st.session_state["new_message"]
|
44 |
+
if message:
|
45 |
+
response = respond(
|
46 |
+
message=message,
|
47 |
+
history=st.session_state.history,
|
48 |
+
system_message=st.session_state.system_message,
|
49 |
+
max_tokens=st.session_state.max_tokens,
|
50 |
+
temperature=st.session_state.temperature,
|
51 |
+
top_p=st.session_state.top_p,
|
52 |
+
)
|
53 |
+
st.session_state.history.append((message, response))
|
54 |
+
st.session_state["new_message"] = ""
|
55 |
+
|
56 |
+
# Streamlit UI
|
57 |
+
st.title("AI Lawyer Chatbot")
|
58 |
+
st.write("Welcome to your legal assistant. How can I assist you with your legal questions today?")
|
59 |
+
st.markdown("<p style='color:blue;'>Developed by Hashir Ehtisham</p>", unsafe_allow_html=True)
|
60 |
+
|
61 |
+
# Add description to sidebar
|
62 |
+
st.sidebar.markdown("""
|
63 |
+
### AI Lawyer Chatbot
|
64 |
+
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.
|
65 |
+
|
66 |
+
#### Features:
|
67 |
+
- **Interactive Chat**: Engage in real-time conversations with the chatbot, designed to simulate interactions with a knowledgeable legal expert.
|
68 |
+
- **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.
|
69 |
+
- **Chat History**: Review previous interactions with the chatbot directly in the app, keeping track of your legal inquiries and the AI's responses.
|
70 |
+
|
71 |
+
#### How It Works:
|
72 |
+
- **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.
|
73 |
+
- **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.
|
74 |
+
- **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).
|
75 |
+
|
76 |
+
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!
|
77 |
+
""")
|
78 |
+
|
79 |
+
# Set default system message directly
|
80 |
+
if 'system_message' not in st.session_state:
|
81 |
+
st.session_state.system_message = "You are a knowledgeable AI lawyer providing legal advice and information."
|
82 |
+
|
83 |
+
with st.expander("Settings"):
|
84 |
+
max_tokens_options = [64, 128, 256, 512, 1024, 2048]
|
85 |
+
temperature_options = [0.1, 0.3, 0.5, 0.7, 1.0, 1.5, 2.0, 3.0, 4.0]
|
86 |
+
top_p_options = [0.1, 0.2, 0.3, 0.5, 0.7, 0.8, 0.9, 0.95, 1.0]
|
87 |
+
|
88 |
+
max_tokens = st.selectbox("Max new tokens", options=max_tokens_options, index=max_tokens_options.index(512), key="max_tokens")
|
89 |
+
temperature = st.selectbox("Temperature", options=temperature_options, index=temperature_options.index(0.7), key="temperature")
|
90 |
+
top_p = st.selectbox("Top-p (nucleus sampling)", options=top_p_options, index=top_p_options.index(0.95), key="top_p")
|
91 |
+
|
92 |
+
if 'history' not in st.session_state:
|
93 |
+
st.session_state.history = []
|
94 |
+
|
95 |
+
# Display chat history above the message input
|
96 |
+
st.text_area("Chat History", value="\n\n".join([f"User: {h[0]}\n\nAI Lawyer: {h[1]}" for h in st.session_state.history]), height=400, key="chat_history")
|
97 |
+
|
98 |
+
message = st.text_input("Your message", key="new_message", on_change=send_message)
|