Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,253 +1,7 @@
|
|
| 1 |
-
import
|
| 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 |
-
|
| 15 |
-
google_api_key = os.environ.get("GOOGLE_API_KEY")
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 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(share=True)
|
| 251 |
-
|
| 252 |
-
if __name__ == "__main__":
|
| 253 |
-
main()
|
|
|
|
| 1 |
+
from diffusers import DiffusionPipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
pipe = DiffusionPipeline.from_pretrained("Yw22/BlobCtrl")
|
|
|
|
| 4 |
|
| 5 |
+
prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k"
|
| 6 |
+
image = pipe(prompt).images[0]
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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