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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 langchain.vectorstores import FAISS
|
| 18 |
+
import gradio as gr
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
os.environ["MY_SECRET_KEY"] = "AIzaSyDRj3wAgqOCjc_D45W_u-G3y9dk5YDgxEo"
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# In[145]:
|
| 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 |
+
# In[152]:
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
import gradio as gr
|
| 104 |
+
from langchain.prompts import PromptTemplate
|
| 105 |
+
from langchain.chains import LLMChain
|
| 106 |
+
|
| 107 |
+
def rag_query(query):
|
| 108 |
+
# Retrieve relevant documents
|
| 109 |
+
docs = retriever.get_relevant_documents(query)
|
| 110 |
+
|
| 111 |
+
# Otherwise, use RAG
|
| 112 |
+
context = "\n".join([doc.page_content for doc in docs])
|
| 113 |
+
prompt = f"Context:\n{context}\n\nQuestion: {query}\nAnswer directly and concisely:"
|
| 114 |
+
|
| 115 |
+
try:
|
| 116 |
+
response = llm.invoke(prompt)
|
| 117 |
+
except Exception as e:
|
| 118 |
+
response = f"Error in RAG processing: {str(e)}"
|
| 119 |
+
|
| 120 |
+
return response.content
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
# In[153]:
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
import gradio as gr
|
| 129 |
+
from langchain.prompts import PromptTemplate
|
| 130 |
+
from langchain.chains import LLMChain
|
| 131 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 132 |
+
|
| 133 |
+
# Initialize LLM once (avoid repeated initialization)
|
| 134 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
| 135 |
+
|
| 136 |
+
# Define the general query function
|
| 137 |
+
def general_query(query):
|
| 138 |
+
try:
|
| 139 |
+
# Define the prompt correctly
|
| 140 |
+
prompt = PromptTemplate.from_template("Answer the following query: {query}")
|
| 141 |
+
|
| 142 |
+
# Create an LLM Chain
|
| 143 |
+
chain = LLMChain(llm=llm, prompt=prompt)
|
| 144 |
+
|
| 145 |
+
# Run chatbot and return response
|
| 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 |
+
# In[154]:
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
import gradio as gr
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
# Function to call the selected query method
|
| 162 |
+
def query_router(query, method):
|
| 163 |
+
if method == "Team Query": # Ensure exact match with dropdown options
|
| 164 |
+
return rag_query(query)
|
| 165 |
+
elif method == "General Query":
|
| 166 |
+
return general_query(query)
|
| 167 |
+
return "Invalid selection!"
|
| 168 |
+
|
| 169 |
+
# Define local image paths
|
| 170 |
+
logo_path = "Equinix-LOGO.jpeg" # Ensure this file exists
|
| 171 |
+
|
| 172 |
+
# Custom CSS for background styling
|
| 173 |
+
custom_css = """
|
| 174 |
+
.gradio-container {
|
| 175 |
+
background-color: #f0f0f0;
|
| 176 |
+
text-align: center;
|
| 177 |
+
}
|
| 178 |
+
#logo img {
|
| 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 |
+
# Title & Description
|
| 190 |
+
gr.Markdown("<h1 style='text-align: center; color: black;'>Equinix Chatbot for Automation Team</h1>")
|
| 191 |
+
gr.Markdown("<p style='text-align: center; color: black;'>Ask me anything!</p>")
|
| 192 |
+
|
| 193 |
+
# Input & Dropdown Section
|
| 194 |
+
with gr.Row():
|
| 195 |
+
query_input = gr.Textbox(label="Enter your query")
|
| 196 |
+
query_method = gr.Dropdown(["Team Query", "General Query"], label="Select Query Type")
|
| 197 |
+
|
| 198 |
+
# Button for submitting query
|
| 199 |
+
submit_button = gr.Button("Submit")
|
| 200 |
+
|
| 201 |
+
# Output Textbox
|
| 202 |
+
output_box = gr.Textbox(label="Response", interactive=False)
|
| 203 |
+
|
| 204 |
+
# Button Click Event
|
| 205 |
+
submit_button.click(query_router, inputs=[query_input, query_method], outputs=output_box)
|
| 206 |
+
|
| 207 |
+
# Launch UI
|
| 208 |
+
ui.launch(share=True)
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
# In[168]:
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
get_ipython().system('jupyter nbconvert --to script GenAI_1.ipynb')
|
| 215 |
+
|