Delete app.py
Browse files
app.py
DELETED
|
@@ -1,190 +0,0 @@
|
|
| 1 |
-
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
|
| 2 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 3 |
-
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 4 |
-
from langchain.llms import OpenAI
|
| 5 |
-
from langchain.vectorstores import FAISS
|
| 6 |
-
from langchain import PromptTemplate
|
| 7 |
-
import streamlit as st
|
| 8 |
-
import os
|
| 9 |
-
from langchain.chains import RetrievalQA
|
| 10 |
-
import time
|
| 11 |
-
from datetime import datetime
|
| 12 |
-
import pytz
|
| 13 |
-
import streamlit as st
|
| 14 |
-
from PyPDF2 import PdfReader
|
| 15 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 16 |
-
import os
|
| 17 |
-
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 18 |
-
import google.generativeai as genai
|
| 19 |
-
from langchain.vectorstores import FAISS
|
| 20 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 21 |
-
from langchain.chains.question_answering import load_qa_chain
|
| 22 |
-
from langchain.prompts import PromptTemplate
|
| 23 |
-
|
| 24 |
-
os.environ["GOOGLE_API_KEY"] = "AIzaSyB9O6fAGbU0ptk5VirtkTrLfmMCmaZqFCA"
|
| 25 |
-
|
| 26 |
-
# Move this image display up
|
| 27 |
-
st.markdown(
|
| 28 |
-
'<div class = "center"> <img src = "https://solukraft.com/Home/Black%20BG%20PNG.png" style = "width : 200px;" alt = "Center Aligned Image"> </div>',
|
| 29 |
-
unsafe_allow_html=True,
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
st.markdown(
|
| 33 |
-
""" <style>
|
| 34 |
-
body {
|
| 35 |
-
background-size: cover;
|
| 36 |
-
}
|
| 37 |
-
.css-fg4pbf {
|
| 38 |
-
background:rgba(255,255,255,0.8) !important;
|
| 39 |
-
}
|
| 40 |
-
.css-18ni7ap {
|
| 41 |
-
background:none !important;
|
| 42 |
-
}
|
| 43 |
-
a.css-eczf16.e1nzilvr3 {
|
| 44 |
-
display: none;
|
| 45 |
-
}
|
| 46 |
-
.block-container {
|
| 47 |
-
padding: 2rem 0 10rem 0;
|
| 48 |
-
}
|
| 49 |
-
button[kind="primary"] {
|
| 50 |
-
position: fixed;
|
| 51 |
-
bottom: 8rem;
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
button[kind="secondary"] {
|
| 55 |
-
position: fixed;
|
| 56 |
-
bottom: 8rem;
|
| 57 |
-
transform: translateX(630px)
|
| 58 |
-
}
|
| 59 |
-
h1#private-bot-using-gpt-3-5 {
|
| 60 |
-
padding-top: 0;
|
| 61 |
-
}
|
| 62 |
-
.font {
|
| 63 |
-
font-size:22px ;
|
| 64 |
-
font-family: 'Segoe UI';
|
| 65 |
-
color:#B7D313;
|
| 66 |
-
font-weight:400;
|
| 67 |
-
text-align:center;
|
| 68 |
-
}
|
| 69 |
-
h1 {
|
| 70 |
-
font-size:30px ;
|
| 71 |
-
font-family: 'Segoe UI';
|
| 72 |
-
color:#002C84;
|
| 73 |
-
font-weight:bold;
|
| 74 |
-
text-align:center;
|
| 75 |
-
}
|
| 76 |
-
.center {
|
| 77 |
-
display: flex;
|
| 78 |
-
justify-content : center ;
|
| 79 |
-
}
|
| 80 |
-
</style> """,
|
| 81 |
-
unsafe_allow_html=True,
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
def get_conversational_chain():
|
| 85 |
-
|
| 86 |
-
prompt_template = """
|
| 87 |
-
Answer the question as detailed as possible from the provided context, make sure to provide all the details
|
| 88 |
-
Context:\n {context}?\n
|
| 89 |
-
Question: \n{question}\n
|
| 90 |
-
|
| 91 |
-
Answer:
|
| 92 |
-
"""
|
| 93 |
-
|
| 94 |
-
model = ChatGoogleGenerativeAI(model="gemini-pro",
|
| 95 |
-
temperature=0.3)
|
| 96 |
-
embeddings = GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
|
| 97 |
-
|
| 98 |
-
new_db = FAISS.load_local("faiss_index", embeddings)
|
| 99 |
-
prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
|
| 100 |
-
qa_chain = RetrievalQA.from_chain_type(llm=model,
|
| 101 |
-
chain_type='stuff',
|
| 102 |
-
retriever=new_db.as_retriever(search_kwargs={'k': 2}),
|
| 103 |
-
return_source_documents=True,
|
| 104 |
-
chain_type_kwargs={'prompt': prompt}
|
| 105 |
-
)
|
| 106 |
-
return qa_chain
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
def user_input(user_question):
|
| 111 |
-
chain = get_conversational_chain()
|
| 112 |
-
response = chain.invoke(user_question)
|
| 113 |
-
return response
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
st.title("SoluKraft PharmaGPT")
|
| 117 |
-
|
| 118 |
-
if "messages" not in st.session_state:
|
| 119 |
-
st.session_state.messages = []
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
# Display chat messages from history on app rerun
|
| 123 |
-
for message in st.session_state.messages:
|
| 124 |
-
with st.chat_message(message["role"]):
|
| 125 |
-
st.markdown(message["content"])
|
| 126 |
-
st.markdown(f"<div style='text-align: right'>{message['timestamp']}</div>", unsafe_allow_html=True)
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
if prompt := st.chat_input("send a message"):
|
| 130 |
-
# Display user message in chat message container
|
| 131 |
-
utc_now = datetime.utcnow()
|
| 132 |
-
# Convert UTC time to IST
|
| 133 |
-
ist_timezone = pytz.timezone('Asia/Kolkata')
|
| 134 |
-
current_time = utc_now.astimezone(ist_timezone).strftime("%Y-%m-%d %H:%M:%S")
|
| 135 |
-
with st.chat_message("user"):
|
| 136 |
-
st.markdown(prompt)
|
| 137 |
-
st.markdown(f"<div style='text-align: right'>{current_time}</div>", unsafe_allow_html=True,)
|
| 138 |
-
|
| 139 |
-
# Add user message to chat history with timestamp
|
| 140 |
-
st.session_state.messages.append({"role": "user", "content": prompt, "timestamp": current_time})
|
| 141 |
-
|
| 142 |
-
start_time = time.time()
|
| 143 |
-
response = user_input(prompt)
|
| 144 |
-
end_time = time.time()
|
| 145 |
-
utc_now = datetime.utcnow()
|
| 146 |
-
# Convert UTC time to IST
|
| 147 |
-
ist_timezone = pytz.timezone('Asia/Kolkata')
|
| 148 |
-
current_time = utc_now.astimezone(ist_timezone).strftime("%Y-%m-%d %H:%M:%S")
|
| 149 |
-
execution_time = end_time - start_time
|
| 150 |
-
#st.write(f"Execution time: {execution_time:.4f} seconds")
|
| 151 |
-
|
| 152 |
-
# Display assistant response in chat message container
|
| 153 |
-
with st.chat_message("assistant"):
|
| 154 |
-
st.markdown(response['result'])
|
| 155 |
-
st.markdown(f"<div style='text-align: right'>{current_time}</div>", unsafe_allow_html=True,)
|
| 156 |
-
st.session_state.messages.append({"role": "assistant", "content": response['result'], "timestamp": current_time})
|
| 157 |
-
|
| 158 |
-
if len(st.session_state.messages) != 0:
|
| 159 |
-
if st.button("Clear", type="primary"):
|
| 160 |
-
st.session_state.messages = []
|
| 161 |
-
|
| 162 |
-
if st.button("Regenerate", type="secondary"):
|
| 163 |
-
prompt = st.session_state.messages[0]['content']
|
| 164 |
-
utc_now = datetime.utcnow()
|
| 165 |
-
|
| 166 |
-
# Convert UTC time to IST
|
| 167 |
-
ist_timezone = pytz.timezone('Asia/Kolkata')
|
| 168 |
-
current_time = utc_now.astimezone(ist_timezone).strftime("%Y-%m-%d %H:%M:%S")
|
| 169 |
-
with st.chat_message("user"):
|
| 170 |
-
st.markdown(prompt)
|
| 171 |
-
st.markdown(f"<div style='text-align: right'>{current_time}</div>", unsafe_allow_html=True,)
|
| 172 |
-
# Add user message to chat history with timestamp
|
| 173 |
-
st.session_state.messages.append({"role": "user", "content": prompt, "timestamp": current_time})
|
| 174 |
-
start_time = time.time()
|
| 175 |
-
response = user_input(prompt)
|
| 176 |
-
end_time = time.time()
|
| 177 |
-
execution_time = end_time - start_time
|
| 178 |
-
st.write(f"Execution time: {execution_time:.4f} seconds")
|
| 179 |
-
with st.chat_message("assistant"):
|
| 180 |
-
st.markdown(response['result'])
|
| 181 |
-
utc_now = datetime.utcnow()
|
| 182 |
-
|
| 183 |
-
# Convert UTC time to IST
|
| 184 |
-
ist_timezone = pytz.timezone('Asia/Kolkata')
|
| 185 |
-
current_time = utc_now.astimezone(ist_timezone).strftime("%Y-%m-%d %H:%M:%S")
|
| 186 |
-
st.markdown(f"<div style='text-align: right'>{current_time}</div>", unsafe_allow_html=True,)
|
| 187 |
-
|
| 188 |
-
# Add assistant response to chat history with timestamp
|
| 189 |
-
st.session_state.messages.append({"role": "assistant", "content": response['result'], "timestamp": current_time,"metadata":response['source_documents']})
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|