Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,215 +1,253 @@
|
|
| 1 |
-
#!/usr/bin/env python
|
| 2 |
-
# coding: utf-8
|
| 3 |
-
|
| 4 |
-
# In[144]:
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 8 |
-
from langchain.prompts import PromptTemplate
|
| 9 |
-
from langchain.chains import LLMChain
|
| 10 |
-
|
| 11 |
import os
|
| 12 |
-
|
| 13 |
import google.generativeai as genai
|
| 14 |
-
from langchain.document_loaders import PyPDFLoader
|
| 15 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 16 |
from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
|
| 17 |
-
from
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
#pip install pypdf
|
| 28 |
-
#!pip install faiss-cpu
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# In[146]:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
google_api_key = os.environ["MY_SECRET_KEY"]
|
| 35 |
-
|
| 36 |
-
# Check if the API key was found
|
| 37 |
-
if google_api_key:
|
| 38 |
-
# Set the environment variable if the API key was found
|
| 39 |
-
os.environ["GOOGLE_API_KEY"] = google_api_key
|
| 40 |
-
|
| 41 |
-
llm = ChatGoogleGenerativeAI(
|
| 42 |
-
model="gemini-pro", # Specify the model name
|
| 43 |
-
google_api_key=os.environ["GOOGLE_API_KEY"]
|
| 44 |
-
)
|
| 45 |
-
else:
|
| 46 |
-
print("Error: GOOGLE_API_KEY not found in Colab secrets. Please store your API key.")
|
| 47 |
|
|
|
|
|
|
|
| 48 |
|
|
|
|
|
|
|
| 49 |
|
|
|
|
| 50 |
genai.configure(api_key=google_api_key)
|
| 51 |
-
model = genai.GenerativeModel("gemini-pro")
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
# In[147]:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
work_dir=os.getcwd()
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
# In[148]:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
# Verify file existence
|
| 64 |
-
assert "RAG.pdf" in os.listdir(work_dir), "RAG.pdf not found in the specified directory!"
|
| 65 |
-
print(f"Current Working Directory: {os.getcwd()}")
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# In[149]:
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
# Load PDF and split text
|
| 72 |
-
pdf_path = "RAG.pdf" # Ensure this file is uploaded to Colab
|
| 73 |
-
loader = PyPDFLoader(pdf_path)
|
| 74 |
-
documents = loader.load()
|
| 75 |
-
|
| 76 |
-
# Split text into chunks
|
| 77 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=10)
|
| 78 |
-
text_chunks = text_splitter.split_documents(documents)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
# In[150]:
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
# Generate embeddings
|
| 85 |
-
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
| 86 |
-
|
| 87 |
-
# Store embeddings in FAISS index
|
| 88 |
-
vectorstore = FAISS.from_documents(text_chunks, embeddings)
|
| 89 |
-
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
# In[151]:
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
# Set up Gemini model
|
| 96 |
-
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-001", temperature=0)
|
| 97 |
-
#llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0)
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
from langchain.chains import LLMChain
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
|
|
|
|
|
|
| 115 |
try:
|
| 116 |
-
|
| 117 |
except Exception as e:
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
return response.content
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
|
| 125 |
-
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
# Initialize LLM once (avoid repeated initialization)
|
| 134 |
-
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
| 135 |
|
| 136 |
-
#
|
| 137 |
def general_query(query):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
try:
|
| 139 |
-
# Define the prompt
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
# Create an LLM Chain
|
| 143 |
chain = LLMChain(llm=llm, prompt=prompt)
|
| 144 |
|
| 145 |
-
# Run chatbot and
|
| 146 |
-
response = chain.run(query=query)
|
| 147 |
-
|
| 148 |
-
return response # Return response directly (not response.content)
|
| 149 |
|
| 150 |
except Exception as e:
|
| 151 |
-
return f"Error: {str(e)}"
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
return rag_query(query)
|
| 165 |
elif method == "General Query":
|
| 166 |
return general_query(query)
|
| 167 |
return "Invalid selection!"
|
| 168 |
|
| 169 |
-
#
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
display: block;
|
| 180 |
-
margin: 0 auto;
|
| 181 |
-
max-width: 200px; /* Adjust size */
|
| 182 |
-
}
|
| 183 |
-
"""
|
| 184 |
-
|
| 185 |
-
# Create Gradio UI
|
| 186 |
-
with gr.Blocks(css=custom_css) as ui:
|
| 187 |
-
gr.Image(logo_path, elem_id="logo", show_label=False, height=100, width=200) # Display Logo
|
| 188 |
|
| 189 |
-
#
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
#
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 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 |
+
from datetime import datetime
|
| 11 |
+
import pytz
|
| 12 |
+
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Get API key from Hugging Face Spaces secrets
|
| 15 |
+
google_api_key = os.environ.get("GOOGLE_API_KEY")
|
| 16 |
|
| 17 |
+
if not google_api_key:
|
| 18 |
+
raise ValueError("GOOGLE_API_KEY not found in environment variables. Please add it to Hugging Face Space secrets.")
|
| 19 |
|
| 20 |
+
# Configure Google Generative AI
|
| 21 |
genai.configure(api_key=google_api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Function to get current date and time
|
| 24 |
+
def get_current_datetime():
|
| 25 |
+
# Using UTC as default, but you can change to any timezone
|
| 26 |
+
utc_now = datetime.now(pytz.UTC)
|
| 27 |
+
|
| 28 |
+
# Convert to IST (Indian Standard Time) - modify as needed
|
| 29 |
+
ist_timezone = pytz.timezone('Asia/Kolkata')
|
| 30 |
+
ist_now = utc_now.astimezone(ist_timezone)
|
| 31 |
+
|
| 32 |
+
# Format the datetime
|
| 33 |
+
formatted_date = ist_now.strftime("%B %d, %Y")
|
| 34 |
+
formatted_time = ist_now.strftime("%I:%M:%S %p")
|
| 35 |
+
|
| 36 |
+
return formatted_date, formatted_time
|
| 37 |
|
| 38 |
+
# Load PDF and create vector store
|
| 39 |
+
def initialize_retriever():
|
| 40 |
+
try:
|
| 41 |
+
# Get current directory
|
| 42 |
+
current_dir = os.getcwd()
|
| 43 |
+
print(f"Current working directory: {current_dir}")
|
| 44 |
+
|
| 45 |
+
# List files in current directory for debugging
|
| 46 |
+
print(f"Files in directory: {os.listdir(current_dir)}")
|
| 47 |
+
|
| 48 |
+
# Use absolute path for the PDF
|
| 49 |
+
pdf_path = os.path.join(current_dir, "Team1.pdf")
|
| 50 |
+
print(f"Attempting to load PDF from: {pdf_path}")
|
| 51 |
+
|
| 52 |
+
# Check if file exists
|
| 53 |
+
if not os.path.exists(pdf_path):
|
| 54 |
+
raise FileNotFoundError(f"The file {pdf_path} does not exist")
|
| 55 |
+
|
| 56 |
+
# Load PDF
|
| 57 |
+
loader = PyPDFLoader(pdf_path)
|
| 58 |
+
documents = loader.load()
|
| 59 |
+
|
| 60 |
+
print(f"Successfully loaded {len(documents)} pages from the PDF")
|
| 61 |
|
| 62 |
+
# Split text into chunks
|
| 63 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=10)
|
| 64 |
+
text_chunks = text_splitter.split_documents(documents)
|
| 65 |
+
print(f"Split into {len(text_chunks)} text chunks")
|
| 66 |
|
| 67 |
+
# Generate embeddings
|
| 68 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
|
|
|
| 69 |
|
| 70 |
+
# Store embeddings in FAISS index
|
| 71 |
+
vectorstore = FAISS.from_documents(text_chunks, embeddings)
|
| 72 |
+
print("Successfully created vector store")
|
| 73 |
+
return vectorstore.as_retriever(search_kwargs={"k": 4})
|
| 74 |
|
| 75 |
+
except Exception as e:
|
| 76 |
+
print(f"Error in initialize_retriever: {str(e)}")
|
| 77 |
+
# Return a dummy retriever for graceful failure
|
| 78 |
+
class DummyRetriever:
|
| 79 |
+
def get_relevant_documents(self, query):
|
| 80 |
+
return []
|
| 81 |
+
|
| 82 |
+
print("Returning dummy retriever due to error")
|
| 83 |
+
return DummyRetriever()
|
| 84 |
|
| 85 |
+
# Initialize LLM
|
| 86 |
+
def get_llm():
|
| 87 |
try:
|
| 88 |
+
return ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
| 89 |
except Exception as e:
|
| 90 |
+
print(f"Error initializing LLM: {str(e)}")
|
| 91 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
llm = get_llm()
|
| 94 |
|
| 95 |
+
# RAG query function
|
| 96 |
+
def rag_query(query, retriever):
|
| 97 |
+
if retriever is None:
|
| 98 |
+
return "Error: Could not initialize document retriever. Please check if Team1.pdf exists."
|
| 99 |
+
|
| 100 |
+
# Get current date and time for context
|
| 101 |
+
current_date, current_time = get_current_datetime()
|
| 102 |
+
|
| 103 |
+
try:
|
| 104 |
+
# Retrieve relevant documents
|
| 105 |
+
docs = retriever.get_relevant_documents(query)
|
| 106 |
+
|
| 107 |
+
if not docs:
|
| 108 |
+
return "No relevant information found in the document. Try a general query instead."
|
| 109 |
+
|
| 110 |
+
# Create context from retrieved documents
|
| 111 |
+
context = "\n".join([doc.page_content for doc in docs])
|
| 112 |
+
prompt = f"""Context:\n{context}
|
| 113 |
+
Current Date: {current_date}
|
| 114 |
+
Current Time: {current_time}
|
| 115 |
+
Question: {query}
|
| 116 |
+
Answer directly and concisely, using the current date and time information if relevant:"""
|
| 117 |
|
| 118 |
+
response = llm.invoke(prompt)
|
| 119 |
+
return response.content
|
| 120 |
+
except Exception as e:
|
| 121 |
+
return f"Error in RAG processing: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
# General query function
|
| 124 |
def general_query(query):
|
| 125 |
+
if llm is None:
|
| 126 |
+
return "Error: Could not initialize language model. Please check your API key."
|
| 127 |
+
|
| 128 |
+
# Get current date and time for context
|
| 129 |
+
current_date, current_time = get_current_datetime()
|
| 130 |
+
|
| 131 |
try:
|
| 132 |
+
# Define the prompt with date and time context
|
| 133 |
+
prompt_template = """Current Date: {date}
|
| 134 |
+
Current Time: {time}
|
| 135 |
+
Answer the following query, using the current date and time information if relevant: {query}"""
|
| 136 |
+
|
| 137 |
+
prompt = PromptTemplate.from_template(prompt_template)
|
| 138 |
|
| 139 |
# Create an LLM Chain
|
| 140 |
chain = LLMChain(llm=llm, prompt=prompt)
|
| 141 |
|
| 142 |
+
# Run chatbot and get response
|
| 143 |
+
response = chain.run(date=current_date, time=current_time, query=query)
|
| 144 |
+
return response
|
|
|
|
| 145 |
|
| 146 |
except Exception as e:
|
| 147 |
+
return f"Error in general query: {str(e)}"
|
| 148 |
+
|
| 149 |
+
# Function to handle the case when no PDF is found
|
| 150 |
+
def file_not_found_message():
|
| 151 |
+
return ("The Team1.pdf file could not be found. Team Query mode will not work properly. "
|
| 152 |
+
"Please ensure the PDF is correctly uploaded to the Hugging Face Space.")
|
| 153 |
+
|
| 154 |
+
# Query router function
|
| 155 |
+
def query_router(query, method, retriever):
|
| 156 |
+
if method == "Team Query":
|
| 157 |
+
if isinstance(retriever, type) or retriever is None:
|
| 158 |
+
return file_not_found_message()
|
| 159 |
+
return rag_query(query, retriever)
|
|
|
|
| 160 |
elif method == "General Query":
|
| 161 |
return general_query(query)
|
| 162 |
return "Invalid selection!"
|
| 163 |
|
| 164 |
+
# Function to update the clock
|
| 165 |
+
def update_datetime():
|
| 166 |
+
date, time = get_current_datetime()
|
| 167 |
+
return date, time
|
| 168 |
+
|
| 169 |
+
# Main function to create and launch the Gradio interface
|
| 170 |
+
def main():
|
| 171 |
+
# Initialize retriever
|
| 172 |
+
print("Initializing retriever...")
|
| 173 |
+
retriever = initialize_retriever()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
+
# Custom CSS for styling
|
| 176 |
+
custom_css = """
|
| 177 |
+
.gradio-container {
|
| 178 |
+
background-color: #f0f0f0;
|
| 179 |
+
text-align: center;
|
| 180 |
+
}
|
| 181 |
+
#logo img {
|
| 182 |
+
display: block;
|
| 183 |
+
margin: 0 auto;
|
| 184 |
+
max-width: 200px;
|
| 185 |
+
}
|
| 186 |
+
.datetime-display {
|
| 187 |
+
text-align: center;
|
| 188 |
+
margin-bottom: 20px;
|
| 189 |
+
font-size: 18px;
|
| 190 |
+
font-weight: bold;
|
| 191 |
+
}
|
| 192 |
+
"""
|
| 193 |
|
| 194 |
+
logo_path = "equinix-sign.jpg"
|
| 195 |
+
logo_exists = os.path.exists(logo_path)
|
| 196 |
+
|
| 197 |
+
# Create Gradio UI
|
| 198 |
+
with gr.Blocks(css=custom_css) as ui:
|
| 199 |
+
if logo_exists:
|
| 200 |
+
gr.Image(logo_path, elem_id="logo", show_label=False, height=100, width=200)
|
| 201 |
+
else:
|
| 202 |
+
gr.Markdown("<h2 style='text-align: center;'>Equinix</h2>")
|
| 203 |
+
print(f"Warning: Logo file {logo_path} not found")
|
| 204 |
+
|
| 205 |
+
# Title & Description
|
| 206 |
+
gr.Markdown("<h1 style='text-align: center; color: black;'>Equinix Chatbot for Automation Team</h1>")
|
| 207 |
+
|
| 208 |
+
# Date and Time Display
|
| 209 |
+
with gr.Row(elem_classes="datetime-display"):
|
| 210 |
+
date_display = gr.Textbox(label="Date", interactive=False)
|
| 211 |
+
time_display = gr.Textbox(label="Time", interactive=False)
|
| 212 |
+
|
| 213 |
+
# Update date and time using Gradio's interval functionality
|
| 214 |
+
date_val, time_val = get_current_datetime()
|
| 215 |
+
date_display.value = date_val
|
| 216 |
+
time_display.value = time_val
|
| 217 |
+
|
| 218 |
+
# Add refresh button for time
|
| 219 |
+
refresh_btn = gr.Button("Update Date & Time")
|
| 220 |
+
refresh_btn.click(fn=update_datetime, inputs=[], outputs=[date_display, time_display])
|
| 221 |
+
|
| 222 |
+
gr.Markdown("<p style='text-align: center; color: black;'>Ask me anything!</p>")
|
| 223 |
|
| 224 |
+
# Input & Dropdown Section
|
| 225 |
+
with gr.Row():
|
| 226 |
+
query_input = gr.Textbox(label="Enter your query")
|
| 227 |
+
query_method = gr.Dropdown(["Team Query", "General Query"], label="Select Query Type", value="Team Query")
|
| 228 |
+
|
| 229 |
+
# Button for submitting query
|
| 230 |
+
submit_button = gr.Button("Submit")
|
| 231 |
+
|
| 232 |
+
# Output Textbox
|
| 233 |
+
output_box = gr.Textbox(label="Response", interactive=False)
|
| 234 |
+
|
| 235 |
+
# Button Click Events
|
| 236 |
+
submit_button.click(
|
| 237 |
+
lambda query, method: query_router(query, method, retriever),
|
| 238 |
+
inputs=[query_input, query_method],
|
| 239 |
+
outputs=output_box
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
# This callback will update the date and time whenever the user submits a query
|
| 243 |
+
submit_button.click(
|
| 244 |
+
fn=update_datetime,
|
| 245 |
+
inputs=[],
|
| 246 |
+
outputs=[date_display, time_display]
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
# Launch UI
|
| 250 |
+
ui.launch()
|
| 251 |
|
| 252 |
+
if __name__ == "__main__":
|
| 253 |
+
main()
|