sds-bosch
commited on
Commit
•
dab9fc0
1
Parent(s):
8eaa75f
Adding files
Browse files- app.py +105 -0
- requirements.txt +3 -0
- sewa.pdf +0 -0
- utilities_monthly.db +0 -0
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain.agents import initialize_agent, AgentType, Tool
|
3 |
+
from langchain.chains import LLMMathChain, RetrievalQA
|
4 |
+
from langchain.chat_models import ChatOpenAI
|
5 |
+
from langchain.embeddings import OpenAIEmbeddings
|
6 |
+
from langchain.text_splitter import CharacterTextSplitter
|
7 |
+
from langchain.utilities import SQLDatabase
|
8 |
+
from langchain.vectorstores import FAISS
|
9 |
+
from langchain_experimental.sql import SQLDatabaseChain
|
10 |
+
from langchain.document_loaders import PyPDFLoader
|
11 |
+
from langchain.schema import ChatMessage
|
12 |
+
from langchain.callbacks.manager import CallbackManager
|
13 |
+
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
|
14 |
+
|
15 |
+
|
16 |
+
# Dummy users and passwords
|
17 |
+
USERS = {
|
18 |
+
"johndoe": {"password": "Streamlit@12", "username": "John Doe"},
|
19 |
+
"janesmith": {"password": "Streamlit@12", "username": "Jane Smith"}
|
20 |
+
}
|
21 |
+
|
22 |
+
# Initialize the system
|
23 |
+
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")
|
24 |
+
llm_math_chain = LLMMathChain.from_llm(llm=llm, verbose=True)
|
25 |
+
db = SQLDatabase.from_uri("sqlite:///utilities_monthly.db")
|
26 |
+
db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)
|
27 |
+
pdf_loader = PyPDFLoader("sewa.pdf")
|
28 |
+
pages = pdf_loader.load_and_split()
|
29 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
30 |
+
docs = text_splitter.split_documents(pages)
|
31 |
+
embeddings = OpenAIEmbeddings()
|
32 |
+
retriever = FAISS.from_documents(docs, embeddings).as_retriever()
|
33 |
+
|
34 |
+
tools = [
|
35 |
+
Tool(
|
36 |
+
name="FAQ",
|
37 |
+
func=RetrievalQA.from_chain_type(llm=llm, retriever=retriever),
|
38 |
+
description="Useful for answering generic questions related to SEWA, bill, usage, etc."
|
39 |
+
),
|
40 |
+
Tool(
|
41 |
+
name="Electricity-DB",
|
42 |
+
func=db_chain.run,
|
43 |
+
description="Useful for answering questions about Customer Energy Usage.Plase use this username given in the prompt Input should be in the form of a question containing full context"
|
44 |
+
)
|
45 |
+
]
|
46 |
+
callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
|
47 |
+
agent_executor = initialize_agent(tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True,
|
48 |
+
callback_manager=callback_manager)
|
49 |
+
# Styling for sidebar
|
50 |
+
# Set the secondary text color
|
51 |
+
st.markdown("""
|
52 |
+
<style>
|
53 |
+
.stApp > div > div > div > div:first-child {
|
54 |
+
background-color: blue;
|
55 |
+
}
|
56 |
+
</style>
|
57 |
+
""", unsafe_allow_html=True)
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
with st.sidebar:
|
62 |
+
if "logged_in_user" not in st.session_state:
|
63 |
+
st.session_state["logged_in_user"] = None
|
64 |
+
|
65 |
+
if st.session_state["logged_in_user"]:
|
66 |
+
user_data = st.session_state["logged_in_user"]
|
67 |
+
st.title(f"Welcome {user_data['username']}!")
|
68 |
+
|
69 |
+
st.write("You can ask about energy usage, bill, due date, etc..")
|
70 |
+
# col1, col2, col3 = st.columns([1,18,1])
|
71 |
+
|
72 |
+
if st.button("Log Out"):
|
73 |
+
st.session_state["logged_in_user"] = None
|
74 |
+
st.session_state["messages"] = [ChatMessage(role="assistant", content="How can I help you?")] # Reset chat history
|
75 |
+
st.experimental_rerun() # Refresh the page to show the login options again
|
76 |
+
else:
|
77 |
+
st.title("SEWA - At your service")
|
78 |
+
user_id = st.text_input("User ID")
|
79 |
+
password = st.text_input("Password", type="password")
|
80 |
+
if st.button("Login"):
|
81 |
+
if user_id in USERS and USERS[user_id]['password'] == password:
|
82 |
+
st.session_state["logged_in_user"] = USERS[user_id]
|
83 |
+
st.success(f"Logged in as {USERS[user_id]['username']}!")
|
84 |
+
st.experimental_rerun() # Refresh the page immediately after a successful login
|
85 |
+
else:
|
86 |
+
st.error("Invalid user ID or password")
|
87 |
+
# Chat Interface
|
88 |
+
if "messages" not in st.session_state:
|
89 |
+
st.session_state["messages"] = [ChatMessage(role="assistant", content="How can I help you?")]
|
90 |
+
|
91 |
+
for msg in st.session_state.messages:
|
92 |
+
st.chat_message(msg.role).write(msg.content)
|
93 |
+
|
94 |
+
if prompt := st.chat_input():
|
95 |
+
# If the user is logged in, replace "my" with the user's name.
|
96 |
+
|
97 |
+
|
98 |
+
st.session_state.messages.append(ChatMessage(role="user", content=prompt))
|
99 |
+
st.chat_message("user").write(prompt)
|
100 |
+
if st.session_state["logged_in_user"]:
|
101 |
+
user_name = st.session_state["logged_in_user"]["username"]
|
102 |
+
prompt = prompt.replace("my", user_name)
|
103 |
+
response = agent_executor.invoke({"input": prompt})
|
104 |
+
st.session_state.messages.append(ChatMessage(role="assistant", content=response['output']))
|
105 |
+
st.chat_message("assistant").write(response['output'])
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
langchain
|
3 |
+
langchain_experimental
|
sewa.pdf
ADDED
Binary file (205 kB). View file
|
|
utilities_monthly.db
ADDED
Binary file (36.9 kB). View file
|
|