Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,126 +1,30 @@
|
|
1 |
-
import time
|
2 |
-
import os
|
3 |
import streamlit as st
|
4 |
-
from
|
5 |
-
from langchain_community.embeddings import HuggingFaceEmbeddings
|
6 |
-
from langchain.prompts import PromptTemplate
|
7 |
-
from langchain.memory import ConversationBufferWindowMemory
|
8 |
-
from langchain.chains import ConversationalRetrievalChain
|
9 |
-
from langchain_together import Together
|
10 |
-
|
11 |
-
from footer import footer
|
12 |
-
|
13 |
-
# Set the Streamlit page configuration and theme
|
14 |
-
st.set_page_config(page_title="BharatLAW", layout="centered")
|
15 |
-
|
16 |
-
# Display the logo image
|
17 |
-
col1, col2, col3 = st.columns([1, 30, 1])
|
18 |
-
with col2:
|
19 |
-
st.image("https://github.com/Nike-one/BharatLAW/blob/master/images/banner.png?raw=true", use_column_width=True)
|
20 |
-
|
21 |
-
def hide_hamburger_menu():
|
22 |
-
st.markdown("""
|
23 |
-
<style>
|
24 |
-
#MainMenu {visibility: hidden;}
|
25 |
-
footer {visibility: hidden;}
|
26 |
-
</style>
|
27 |
-
""", unsafe_allow_html=True)
|
28 |
-
|
29 |
-
hide_hamburger_menu()
|
30 |
-
|
31 |
-
# Initialize session state for messages and memory
|
32 |
-
if "messages" not in st.session_state:
|
33 |
-
st.session_state.messages = []
|
34 |
-
|
35 |
-
if "memory" not in st.session_state:
|
36 |
-
st.session_state.memory = ConversationBufferWindowMemory(k=2, memory_key="chat_history", return_messages=True)
|
37 |
|
|
|
38 |
@st.cache_resource
|
39 |
-
def
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
- [Detail any exceptions to the general rule, if applicable]
|
65 |
-
- [Include any additional relevant information that directly relates to the user's query]
|
66 |
-
</s>[INST]
|
67 |
-
"""
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
prompt = PromptTemplate(template=prompt_template,
|
72 |
-
input_variables=['context', 'question', 'chat_history'])
|
73 |
-
|
74 |
-
api_key = os.getenv('TOGETHER_API_KEY')
|
75 |
-
llm = Together(model="mistralai/Mixtral-8x22B-Instruct-v0.1", temperature=0.5, max_tokens=1024, together_api_key=api_key)
|
76 |
-
|
77 |
-
qa = ConversationalRetrievalChain.from_llm(llm=llm, memory=st.session_state.memory, retriever=db_retriever, combine_docs_chain_kwargs={'prompt': prompt})
|
78 |
-
|
79 |
-
def extract_answer(full_response):
|
80 |
-
"""Extracts the answer from the LLM's full response by removing the instructional text."""
|
81 |
-
answer_start = full_response.find("Response:")
|
82 |
-
if answer_start != -1:
|
83 |
-
answer_start += len("Response:")
|
84 |
-
answer_end = len(full_response)
|
85 |
-
return full_response[answer_start:answer_end].strip()
|
86 |
-
return full_response
|
87 |
-
|
88 |
-
def reset_conversation():
|
89 |
-
st.session_state.messages = []
|
90 |
-
st.session_state.memory.clear()
|
91 |
-
|
92 |
-
for message in st.session_state.messages:
|
93 |
-
with st.chat_message(message["role"]):
|
94 |
-
st.write(message["content"])
|
95 |
-
|
96 |
-
|
97 |
-
input_prompt = st.chat_input("Say something...")
|
98 |
-
if input_prompt:
|
99 |
-
with st.chat_message("user"):
|
100 |
-
st.markdown(f"**You:** {input_prompt}")
|
101 |
-
|
102 |
-
st.session_state.messages.append({"role": "user", "content": input_prompt})
|
103 |
-
with st.chat_message("assistant"):
|
104 |
-
with st.spinner("Thinking 💡..."):
|
105 |
-
result = qa.invoke(input=input_prompt)
|
106 |
-
message_placeholder = st.empty()
|
107 |
-
answer = extract_answer(result["answer"])
|
108 |
-
|
109 |
-
# Initialize the response message
|
110 |
-
full_response = "⚠️ **_Gentle reminder: We generally ensure precise information, but do double-check._** \n\n\n"
|
111 |
-
for chunk in answer:
|
112 |
-
# Simulate typing by appending chunks of the response over time
|
113 |
-
full_response += chunk
|
114 |
-
time.sleep(0.02) # Adjust the sleep time to control the "typing" speed
|
115 |
-
message_placeholder.markdown(full_response + " |", unsafe_allow_html=True)
|
116 |
-
|
117 |
-
st.session_state.messages.append({"role": "assistant", "content": answer})
|
118 |
-
|
119 |
-
if st.button('🗑️ Reset All Chat', on_click=reset_conversation):
|
120 |
-
st.experimental_rerun()
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
# Define the CSS to style the footer
|
125 |
-
footer()
|
126 |
-
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Load the tokenizer and model from local files
|
5 |
@st.cache_resource
|
6 |
+
def load_model():
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained("./", config="config.json")
|
8 |
+
model = AutoModelForCausalLM.from_pretrained("./")
|
9 |
+
return tokenizer, model
|
10 |
+
|
11 |
+
# Initialize the model and tokenizer
|
12 |
+
tokenizer, model = load_model()
|
13 |
+
|
14 |
+
# Set up Streamlit page configuration
|
15 |
+
st.set_page_config(page_title="Legal AI Chatbot", layout="centered")
|
16 |
+
|
17 |
+
st.title("Legal AI Chatbot")
|
18 |
+
st.write("This chatbot provides responses based on a legal language model.")
|
19 |
+
|
20 |
+
# User input
|
21 |
+
user_input = st.text_input("Enter your query:")
|
22 |
+
|
23 |
+
if user_input:
|
24 |
+
# Tokenize and generate response
|
25 |
+
inputs = tokenizer.encode(user_input, return_tensors="pt")
|
26 |
+
outputs = model.generate(inputs, max_length=150, num_return_sequences=1)
|
27 |
+
|
28 |
+
# Decode and display the output
|
29 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
30 |
+
st.text_area("Response:", response, height=200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|