Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,9 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
| 4 |
from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
|
| 5 |
-
from
|
| 6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
-
from
|
| 8 |
from langchain.prompts import PromptTemplate
|
| 9 |
from langchain.chains import LLMChain
|
| 10 |
|
|
@@ -19,35 +19,77 @@ genai.configure(api_key=google_api_key)
|
|
| 19 |
|
| 20 |
# Load PDF and create vector store
|
| 21 |
def initialize_retriever():
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Initialize LLM
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# RAG query function
|
| 42 |
def rag_query(query, retriever):
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
# Create context from retrieved documents
|
| 47 |
-
context = "\n".join([doc.page_content for doc in docs])
|
| 48 |
-
prompt = f"Context:\n{context}\n\nQuestion: {query}\nAnswer directly and concisely:"
|
| 49 |
-
|
| 50 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
response = llm.invoke(prompt)
|
| 52 |
return response.content
|
| 53 |
except Exception as e:
|
|
@@ -55,6 +97,9 @@ def rag_query(query, retriever):
|
|
| 55 |
|
| 56 |
# General query function
|
| 57 |
def general_query(query):
|
|
|
|
|
|
|
|
|
|
| 58 |
try:
|
| 59 |
# Define the prompt
|
| 60 |
prompt = PromptTemplate.from_template("Answer the following query: {query}")
|
|
@@ -67,11 +112,18 @@ def general_query(query):
|
|
| 67 |
return response
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
-
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# Query router function
|
| 73 |
def query_router(query, method, retriever):
|
| 74 |
if method == "Team Query":
|
|
|
|
|
|
|
| 75 |
return rag_query(query, retriever)
|
| 76 |
elif method == "General Query":
|
| 77 |
return general_query(query)
|
|
@@ -80,6 +132,7 @@ def query_router(query, method, retriever):
|
|
| 80 |
# Main function to create and launch the Gradio interface
|
| 81 |
def main():
|
| 82 |
# Initialize retriever
|
|
|
|
| 83 |
retriever = initialize_retriever()
|
| 84 |
|
| 85 |
# Custom CSS for styling
|
|
@@ -95,9 +148,16 @@ def main():
|
|
| 95 |
}
|
| 96 |
"""
|
| 97 |
|
|
|
|
|
|
|
|
|
|
| 98 |
# Create Gradio UI
|
| 99 |
with gr.Blocks(css=custom_css) as ui:
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
# Title & Description
|
| 103 |
gr.Markdown("<h1 style='text-align: center; color: black;'>Equinix Chatbot for Automation Team</h1>")
|
|
@@ -122,7 +182,7 @@ def main():
|
|
| 122 |
)
|
| 123 |
|
| 124 |
# Launch UI
|
| 125 |
-
ui.launch(
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
main()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import google.generativeai as genai
|
| 4 |
from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
|
| 5 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
+
from langchain_community.vectorstores import FAISS
|
| 8 |
from langchain.prompts import PromptTemplate
|
| 9 |
from langchain.chains import LLMChain
|
| 10 |
|
|
|
|
| 19 |
|
| 20 |
# Load PDF and create vector store
|
| 21 |
def initialize_retriever():
|
| 22 |
+
try:
|
| 23 |
+
# Get current directory
|
| 24 |
+
current_dir = os.getcwd()
|
| 25 |
+
print(f"Current working directory: {current_dir}")
|
| 26 |
+
|
| 27 |
+
# List files in current directory for debugging
|
| 28 |
+
print(f"Files in directory: {os.listdir(current_dir)}")
|
| 29 |
+
|
| 30 |
+
# Use absolute path for the PDF
|
| 31 |
+
pdf_path = os.path.join(current_dir, "Team1.pdf")
|
| 32 |
+
print(f"Attempting to load PDF from: {pdf_path}")
|
| 33 |
+
|
| 34 |
+
# Check if file exists
|
| 35 |
+
if not os.path.exists(pdf_path):
|
| 36 |
+
raise FileNotFoundError(f"The file {pdf_path} does not exist")
|
| 37 |
+
|
| 38 |
+
# Load PDF
|
| 39 |
+
loader = PyPDFLoader(pdf_path)
|
| 40 |
+
documents = loader.load()
|
| 41 |
+
|
| 42 |
+
print(f"Successfully loaded {len(documents)} pages from the PDF")
|
| 43 |
|
| 44 |
+
# Split text into chunks
|
| 45 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=10)
|
| 46 |
+
text_chunks = text_splitter.split_documents(documents)
|
| 47 |
+
print(f"Split into {len(text_chunks)} text chunks")
|
| 48 |
|
| 49 |
+
# Generate embeddings
|
| 50 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
| 51 |
|
| 52 |
+
# Store embeddings in FAISS index
|
| 53 |
+
vectorstore = FAISS.from_documents(text_chunks, embeddings)
|
| 54 |
+
print("Successfully created vector store")
|
| 55 |
+
return vectorstore.as_retriever(search_kwargs={"k": 4})
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"Error in initialize_retriever: {str(e)}")
|
| 59 |
+
# Return a dummy retriever for graceful failure
|
| 60 |
+
class DummyRetriever:
|
| 61 |
+
def get_relevant_documents(self, query):
|
| 62 |
+
return []
|
| 63 |
+
|
| 64 |
+
print("Returning dummy retriever due to error")
|
| 65 |
+
return DummyRetriever()
|
| 66 |
|
| 67 |
# Initialize LLM
|
| 68 |
+
def get_llm():
|
| 69 |
+
try:
|
| 70 |
+
return ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Error initializing LLM: {str(e)}")
|
| 73 |
+
return None
|
| 74 |
+
|
| 75 |
+
llm = get_llm()
|
| 76 |
|
| 77 |
# RAG query function
|
| 78 |
def rag_query(query, retriever):
|
| 79 |
+
if retriever is None:
|
| 80 |
+
return "Error: Could not initialize document retriever. Please check if Team1.pdf exists."
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
try:
|
| 83 |
+
# Retrieve relevant documents
|
| 84 |
+
docs = retriever.get_relevant_documents(query)
|
| 85 |
+
|
| 86 |
+
if not docs:
|
| 87 |
+
return "No relevant information found in the document. Try a general query instead."
|
| 88 |
+
|
| 89 |
+
# Create context from retrieved documents
|
| 90 |
+
context = "\n".join([doc.page_content for doc in docs])
|
| 91 |
+
prompt = f"Context:\n{context}\n\nQuestion: {query}\nAnswer directly and concisely:"
|
| 92 |
+
|
| 93 |
response = llm.invoke(prompt)
|
| 94 |
return response.content
|
| 95 |
except Exception as e:
|
|
|
|
| 97 |
|
| 98 |
# General query function
|
| 99 |
def general_query(query):
|
| 100 |
+
if llm is None:
|
| 101 |
+
return "Error: Could not initialize language model. Please check your API key."
|
| 102 |
+
|
| 103 |
try:
|
| 104 |
# Define the prompt
|
| 105 |
prompt = PromptTemplate.from_template("Answer the following query: {query}")
|
|
|
|
| 112 |
return response
|
| 113 |
|
| 114 |
except Exception as e:
|
| 115 |
+
return f"Error in general query: {str(e)}"
|
| 116 |
+
|
| 117 |
+
# Function to handle the case when no PDF is found
|
| 118 |
+
def file_not_found_message():
|
| 119 |
+
return ("The Team1.pdf file could not be found. Team Query mode will not work properly. "
|
| 120 |
+
"Please ensure the PDF is correctly uploaded to the Hugging Face Space.")
|
| 121 |
|
| 122 |
# Query router function
|
| 123 |
def query_router(query, method, retriever):
|
| 124 |
if method == "Team Query":
|
| 125 |
+
if isinstance(retriever, type) or retriever is None:
|
| 126 |
+
return file_not_found_message()
|
| 127 |
return rag_query(query, retriever)
|
| 128 |
elif method == "General Query":
|
| 129 |
return general_query(query)
|
|
|
|
| 132 |
# Main function to create and launch the Gradio interface
|
| 133 |
def main():
|
| 134 |
# Initialize retriever
|
| 135 |
+
print("Initializing retriever...")
|
| 136 |
retriever = initialize_retriever()
|
| 137 |
|
| 138 |
# Custom CSS for styling
|
|
|
|
| 148 |
}
|
| 149 |
"""
|
| 150 |
|
| 151 |
+
logo_path = "equinix-sign.jpg"
|
| 152 |
+
logo_exists = os.path.exists(logo_path)
|
| 153 |
+
|
| 154 |
# Create Gradio UI
|
| 155 |
with gr.Blocks(css=custom_css) as ui:
|
| 156 |
+
if logo_exists:
|
| 157 |
+
gr.Image(logo_path, elem_id="logo", show_label=False, height=100, width=200)
|
| 158 |
+
else:
|
| 159 |
+
gr.Markdown("<h2 style='text-align: center;'>Equinix</h2>")
|
| 160 |
+
print(f"Warning: Logo file {logo_path} not found")
|
| 161 |
|
| 162 |
# Title & Description
|
| 163 |
gr.Markdown("<h1 style='text-align: center; color: black;'>Equinix Chatbot for Automation Team</h1>")
|
|
|
|
| 182 |
)
|
| 183 |
|
| 184 |
# Launch UI
|
| 185 |
+
ui.launch()
|
| 186 |
|
| 187 |
if __name__ == "__main__":
|
| 188 |
main()
|