Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,13 +8,16 @@ from langchain.vectorstores import FAISS
|
|
| 8 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 9 |
from langchain.chains.question_answering import load_qa_chain
|
| 10 |
from langchain.prompts import PromptTemplate
|
|
|
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
from google.api_core.exceptions import GoogleAPIError, InvalidArgument
|
| 13 |
|
| 14 |
# Load environment variables
|
| 15 |
load_dotenv()
|
| 16 |
-
api_key
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Function to read all PDF files and return text
|
| 20 |
def get_pdf_text(pdf_docs):
|
|
@@ -32,18 +35,23 @@ def get_text_chunks(text):
|
|
| 32 |
chunks = splitter.split_text(text)
|
| 33 |
return chunks # list of strings
|
| 34 |
|
| 35 |
-
# Function to get embeddings for each chunk and save to vector store
|
| 36 |
-
def get_vector_store(chunks):
|
| 37 |
try:
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
vector_store = FAISS.from_texts(chunks, embedding=embeddings)
|
| 41 |
vector_store.save_local("faiss_index")
|
| 42 |
except (GoogleAPIError, InvalidArgument):
|
| 43 |
raise RuntimeError("Error processing embeddings. Please try again in a minute.")
|
| 44 |
|
| 45 |
-
# Function to get conversational chain
|
| 46 |
-
def get_conversational_chain():
|
| 47 |
prompt_template = """
|
| 48 |
Answer the question as detailed as possible from the provided context.
|
| 49 |
If the answer is not in the provided context, just say, "answer is not available in the context".
|
|
@@ -53,9 +61,13 @@ def get_conversational_chain():
|
|
| 53 |
Answer:
|
| 54 |
"""
|
| 55 |
try:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
prompt = PromptTemplate(template=prompt_template,
|
| 60 |
input_variables=["context", "question"])
|
| 61 |
chain = load_qa_chain(llm=model, chain_type="stuff", prompt=prompt)
|
|
@@ -68,14 +80,19 @@ def clear_chat_history():
|
|
| 68 |
st.session_state.messages = [
|
| 69 |
{"role": "assistant", "content": "Upload some PDFs and ask me a question"}]
|
| 70 |
|
| 71 |
-
# Function to handle user input
|
| 72 |
-
def user_input(user_question):
|
| 73 |
try:
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
| 77 |
docs = new_db.similarity_search(user_question, k=4)
|
| 78 |
-
chain = get_conversational_chain()
|
| 79 |
response = chain(
|
| 80 |
{"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
| 81 |
return response
|
|
@@ -85,7 +102,8 @@ def user_input(user_question):
|
|
| 85 |
# Main function to run the Streamlit app
|
| 86 |
def main():
|
| 87 |
st.set_page_config(
|
| 88 |
-
page_title="
|
|
|
|
| 89 |
layout="wide",
|
| 90 |
initial_sidebar_state="expanded"
|
| 91 |
)
|
|
@@ -142,48 +160,6 @@ def main():
|
|
| 142 |
try:
|
| 143 |
with st.spinner("Processing..."):
|
| 144 |
raw_text = get_pdf_text(pdf_docs)
|
|
|
|
| 145 |
text_chunks = get_text_chunks(raw_text)
|
| 146 |
-
get_vector_store(text_chunks
|
| 147 |
-
st.success("Processing completed!")
|
| 148 |
-
except RuntimeError as e:
|
| 149 |
-
st.error(str(e))
|
| 150 |
-
else:
|
| 151 |
-
st.error("Please upload at least one PDF file.")
|
| 152 |
-
|
| 153 |
-
# Main content area for displaying chat messages
|
| 154 |
-
st.title("Gemini PDF Chatbot")
|
| 155 |
-
st.write("Welcome to the Gemini PDF Chatbot! Upload your PDFs and ask questions.")
|
| 156 |
-
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
| 157 |
-
|
| 158 |
-
# Initialize chat history
|
| 159 |
-
if "messages" not in st.session_state:
|
| 160 |
-
st.session_state.messages = [
|
| 161 |
-
{"role": "assistant", "content": "Upload some PDFs and ask me a question"}]
|
| 162 |
-
|
| 163 |
-
# Display chat messages
|
| 164 |
-
for message in st.session_state.messages:
|
| 165 |
-
with st.chat_message(message["role"]):
|
| 166 |
-
st.write(message["content"])
|
| 167 |
-
|
| 168 |
-
# Chat input
|
| 169 |
-
if prompt := st.chat_input():
|
| 170 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 171 |
-
with st.chat_message("user"):
|
| 172 |
-
st.write(prompt)
|
| 173 |
-
|
| 174 |
-
# Generate bot response
|
| 175 |
-
if st.session_state.messages[-1]["role"] != "assistant":
|
| 176 |
-
try:
|
| 177 |
-
with st.chat_message("assistant"):
|
| 178 |
-
with st.spinner("Thinking..."):
|
| 179 |
-
response = user_input(prompt)
|
| 180 |
-
if response:
|
| 181 |
-
full_response = ''.join(response['output_text'])
|
| 182 |
-
st.write(full_response)
|
| 183 |
-
message = {"role": "assistant", "content": full_response}
|
| 184 |
-
st.session_state.messages.append(message)
|
| 185 |
-
except RuntimeError as e:
|
| 186 |
-
st.error(str(e))
|
| 187 |
-
|
| 188 |
-
if __name__ == "__main__":
|
| 189 |
-
main()
|
|
|
|
| 8 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 9 |
from langchain.chains.question_answering import load_qa_chain
|
| 10 |
from langchain.prompts import PromptTemplate
|
| 11 |
+
from langdetect import detect
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
from google.api_core.exceptions import GoogleAPIError, InvalidArgument
|
| 14 |
|
| 15 |
# Load environment variables
|
| 16 |
load_dotenv()
|
| 17 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 18 |
+
# Mock configuration for PersianLLaMA (replace with actual configuration)
|
| 19 |
+
persian_api_key = os.getenv("PERSIAN_LLAMA_API_KEY")
|
| 20 |
+
# persianllama.configure(api_key=persian_api_key)
|
| 21 |
|
| 22 |
# Function to read all PDF files and return text
|
| 23 |
def get_pdf_text(pdf_docs):
|
|
|
|
| 35 |
chunks = splitter.split_text(text)
|
| 36 |
return chunks # list of strings
|
| 37 |
|
| 38 |
+
# Function to get embeddings for each chunk and save to vector store (language-specific)
|
| 39 |
+
def get_vector_store(chunks, language):
|
| 40 |
try:
|
| 41 |
+
if language == 'fa':
|
| 42 |
+
embeddings = PersianLLaMAEmbeddings(
|
| 43 |
+
model="persianllama-embedding") # Mock PersianLLaMA embeddings
|
| 44 |
+
else:
|
| 45 |
+
embeddings = GoogleGenerativeAIEmbeddings(
|
| 46 |
+
model="models/embedding-001") # Gemini embeddings
|
| 47 |
+
|
| 48 |
vector_store = FAISS.from_texts(chunks, embedding=embeddings)
|
| 49 |
vector_store.save_local("faiss_index")
|
| 50 |
except (GoogleAPIError, InvalidArgument):
|
| 51 |
raise RuntimeError("Error processing embeddings. Please try again in a minute.")
|
| 52 |
|
| 53 |
+
# Function to get conversational chain (language-specific)
|
| 54 |
+
def get_conversational_chain(language):
|
| 55 |
prompt_template = """
|
| 56 |
Answer the question as detailed as possible from the provided context.
|
| 57 |
If the answer is not in the provided context, just say, "answer is not available in the context".
|
|
|
|
| 61 |
Answer:
|
| 62 |
"""
|
| 63 |
try:
|
| 64 |
+
if language == 'fa':
|
| 65 |
+
model = PersianLLaMAChat(model="persianllama-chat") # Mock PersianLLaMA chat
|
| 66 |
+
else:
|
| 67 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro",
|
| 68 |
+
client=genai,
|
| 69 |
+
temperature=0.3)
|
| 70 |
+
|
| 71 |
prompt = PromptTemplate(template=prompt_template,
|
| 72 |
input_variables=["context", "question"])
|
| 73 |
chain = load_qa_chain(llm=model, chain_type="stuff", prompt=prompt)
|
|
|
|
| 80 |
st.session_state.messages = [
|
| 81 |
{"role": "assistant", "content": "Upload some PDFs and ask me a question"}]
|
| 82 |
|
| 83 |
+
# Function to handle user input (language-specific)
|
| 84 |
+
def user_input(user_question, language):
|
| 85 |
try:
|
| 86 |
+
if language == 'fa':
|
| 87 |
+
embeddings = PersianLLaMAEmbeddings(
|
| 88 |
+
model="persianllama-embedding") # Mock PersianLLaMA embeddings
|
| 89 |
+
else:
|
| 90 |
+
embeddings = GoogleGenerativeAIEmbeddings(
|
| 91 |
+
model="models/embedding-001") # Gemini embeddings
|
| 92 |
+
|
| 93 |
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
| 94 |
docs = new_db.similarity_search(user_question, k=4)
|
| 95 |
+
chain = get_conversational_chain(language)
|
| 96 |
response = chain(
|
| 97 |
{"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
| 98 |
return response
|
|
|
|
| 102 |
# Main function to run the Streamlit app
|
| 103 |
def main():
|
| 104 |
st.set_page_config(
|
| 105 |
+
page_title="Gemini PDF Chatbot",
|
| 106 |
+
page_icon="🤖",
|
| 107 |
layout="wide",
|
| 108 |
initial_sidebar_state="expanded"
|
| 109 |
)
|
|
|
|
| 160 |
try:
|
| 161 |
with st.spinner("Processing..."):
|
| 162 |
raw_text = get_pdf_text(pdf_docs)
|
| 163 |
+
language = detect(raw_text)
|
| 164 |
text_chunks = get_text_chunks(raw_text)
|
| 165 |
+
get_vector_store(text_chunks, language
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|