Spaces:
Sleeping
Sleeping
Redeploy all files after cleaning space
Browse files- Dockerfile +39 -0
- README.md +30 -0
- app.py +957 -0
- config.yaml +9 -0
- helpers_HF.py +123 -0
- helpers_LOCAL.py +165 -0
- helpers_SHARED.py +407 -0
- requirements.txt +17 -0
Dockerfile
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use Python 3.12 slim image
|
| 2 |
+
FROM python:3.12-slim
|
| 3 |
+
|
| 4 |
+
# Set environment variables
|
| 5 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 6 |
+
ENV PYTHONUNBUFFERED=1
|
| 7 |
+
ENV GRADIO_SERVER_NAME=0.0.0.0
|
| 8 |
+
ENV GRADIO_SERVER_PORT=7860
|
| 9 |
+
|
| 10 |
+
# Set working directory
|
| 11 |
+
WORKDIR /app
|
| 12 |
+
|
| 13 |
+
# Install system dependencies
|
| 14 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 15 |
+
build-essential \
|
| 16 |
+
git \
|
| 17 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 18 |
+
|
| 19 |
+
# Copy requirements first for better caching
|
| 20 |
+
COPY requirements.txt .
|
| 21 |
+
|
| 22 |
+
# Install Python dependencies
|
| 23 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 24 |
+
pip install --no-cache-dir -r requirements.txt
|
| 25 |
+
|
| 26 |
+
# Copy application code
|
| 27 |
+
COPY app.py .
|
| 28 |
+
COPY helpers_SHARED.py .
|
| 29 |
+
COPY helpers_HF.py .
|
| 30 |
+
COPY helpers_LOCAL.py .
|
| 31 |
+
|
| 32 |
+
# Create data directories
|
| 33 |
+
RUN mkdir -p data/embeddings
|
| 34 |
+
|
| 35 |
+
# Expose port
|
| 36 |
+
EXPOSE 7860
|
| 37 |
+
|
| 38 |
+
# Run the application
|
| 39 |
+
CMD ["python", "app.py"]
|
README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Agentic RAG 4
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: "4.12.0"
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# π€ Agentic RAG 3
|
| 14 |
+
|
| 15 |
+
An intelligent RAG (Retrieval-Augmented Generation) agent with document analysis capabilities.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
- Upload and process PDF documents
|
| 19 |
+
- Intelligent document search using FAISS
|
| 20 |
+
- Agent-powered question answering
|
| 21 |
+
- Multiple tool support (list, count, search documents)
|
| 22 |
+
|
| 23 |
+
## Usage
|
| 24 |
+
1. Upload your PDF documents
|
| 25 |
+
2. Ask questions about your documents
|
| 26 |
+
3. Get intelligent, context-aware answers
|
| 27 |
+
|
| 28 |
+
## Environment
|
| 29 |
+
- Local: Uses Ollama (llama3.2)
|
| 30 |
+
- HF Space: Uses Llama-3.2-3B-Instruct API
|
app.py
ADDED
|
@@ -0,0 +1,957 @@
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
uv init
|
| 3 |
+
uv venv --python 3.12
|
| 4 |
+
source .venv/bin/activate
|
| 5 |
+
uv pip install -r requirements.txt
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
# Note: HuggingFace Spaces reads configuration from the README.md frontmatter, not from a separate YAML file.
|
| 9 |
+
# The config.yaml is for your reference/organization, but the actual Space config must remain in README.md.
|
| 10 |
+
|
| 11 |
+
# The Space was created with Docker SDK and README.md frontmatter specifies sdk: docker:
|
| 12 |
+
# huggingface-cli repo create Agentic_Rag3_dep_space --type space --space_sdk docker
|
| 13 |
+
|
| 14 |
+
# Without Docker, we use the Gradio SDK option in README.md frontmatter:
|
| 15 |
+
# ---
|
| 16 |
+
# sdk: gradio
|
| 17 |
+
# sdk_version: "6.0.1"
|
| 18 |
+
# python_version: "3.12"
|
| 19 |
+
# app_file: app.py
|
| 20 |
+
# ---
|
| 21 |
+
|
| 22 |
+
# Or:
|
| 23 |
+
# huggingface-cli repo create Agentic_Rag3_dep_space --type space --space_sdk gradio
|
| 24 |
+
|
| 25 |
+
# AGENT DEPLOYMENT NOTES:
|
| 26 |
+
# =====================
|
| 27 |
+
# - Local Environment: Uses Ollama (llama3.2) for development
|
| 28 |
+
# - HF Space Environment: Uses Llama-3.2-3B-Instruct (cloud API) for production
|
| 29 |
+
# - Environment Auto-Detection: Via SPACE_ID environment variable
|
| 30 |
+
# - Agent Tools Available: Document listing, counting, RAG search
|
| 31 |
+
# - Storage: Temporary (files lost on restart) or persistent (paid plans)
|
| 32 |
+
# - UI Features: Tool-powered sample questions, environment indicators
|
| 33 |
+
# - Security: Token stored as Space secret (HF_token), not in code
|
| 34 |
+
# - Space URL: https://huggingface.co/spaces/irajkoohi/Agentic_Rag3_dep_space
|
| 35 |
+
|
| 36 |
+
# A) If you want to run app.py locally:
|
| 37 |
+
"""
|
| 38 |
+
cd /Users/ik/UVcodes/Deployed_Agents_4 && clear && lsof -ti:7860 | xargs kill -9 2>/dev/null; sleep 2 && source .venv/bin/activate && python app.py
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
# B) If you want to run app.py on Hugging Face Space:
|
| 42 |
+
"""
|
| 43 |
+
https://huggingface.co/spaces/irajkoohi/Agentic_Rag4_dep_space
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
# Create and Upload RAG Agent to HF Space Agentic_Rag4_dep_space (Docker SDK)
|
| 47 |
+
"""
|
| 48 |
+
# huggingface-cli repo create Agentic_Rag4_dep_space --type space --space_sdk docker 2>&1
|
| 49 |
+
Create new token with Write role at: https://huggingface.co/settings/tokens
|
| 50 |
+
Add token to Space secrets at: https://huggingface.co/spaces/irajkoohi/Agentic_Rag4_dep_space/settings
|
| 51 |
+
|
| 52 |
+
clear
|
| 53 |
+
rm -rf Agentic_Rag4_dep_space && git clone https://huggingface.co/spaces/irajkoohi/Agentic_Rag4_dep_space
|
| 54 |
+
cd /Users/ik/UVcodes/Deployed_Agents_4/Agentic_Rag4_dep_space && cp ../app.py . && cp ../helpers_SHARED.py . && cp ../helpers_HF.py . && cp ../helpers_LOCAL.py . && cp ../requirements.txt . && cp ../README.md . && cp ../Dockerfile . && cp ../config.yaml .
|
| 55 |
+
mkdir -p data/embeddings
|
| 56 |
+
git add . && git commit -m "Deploy RAG Agent with Dockerfile to HF space"
|
| 57 |
+
git push --force
|
| 58 |
+
https://huggingface.co/spaces/irajkoohi/Agentic_Rag4_dep_space
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
# if you want to upload all files:
|
| 62 |
+
"""
|
| 63 |
+
clear
|
| 64 |
+
cd /Users/ik/UVcodes/Deployed_Agents_4/Agentic_Rag4_dep_space
|
| 65 |
+
cp ../app.py .
|
| 66 |
+
cp ../helpers_SHARED.py .
|
| 67 |
+
cp ../helpers_HF.py .
|
| 68 |
+
cp ../helpers_LOCAL.py .
|
| 69 |
+
cp ../requirements.txt .
|
| 70 |
+
# cp ../README.md .
|
| 71 |
+
cp ../Dockerfile .
|
| 72 |
+
cp ../config.yaml .
|
| 73 |
+
git add .
|
| 74 |
+
git commit -m "Update all files"
|
| 75 |
+
git push
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
# If you want to delete all files on HF space
|
| 79 |
+
"""
|
| 80 |
+
cd /Users/ik/UVcodes/Deployed_Agents_4
|
| 81 |
+
rm -rf Agentic_Rag4_dep_space && git clone https://huggingface.co/spaces/irajkoohi/Agentic_Rag4_dep_space
|
| 82 |
+
cd Agentic_Rag4_dep_space && find . -maxdepth 1 -not -name '.git' -not -name '.' -delete
|
| 83 |
+
rm -rf data embeddings
|
| 84 |
+
git add -A && git commit -m "Remove all files to clean the space"
|
| 85 |
+
git push
|
| 86 |
+
ls -la && pwd
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
# If you want to delete a space on HF website
|
| 90 |
+
"""
|
| 91 |
+
1. Go to: https://huggingface.co/spaces/irajkoohi/Agentic_Rag4_dep_space/settings
|
| 92 |
+
2. Scroll down to "Delete this Space"
|
| 93 |
+
4. Type: irajkoohi/Agentic_Rag4_dep_space
|
| 94 |
+
4. Click "Delete"
|
| 95 |
+
"""
|
| 96 |
+
|
| 97 |
+
# if you want to sync changes of some files (like app.py and helpers_SHARED.py):
|
| 98 |
+
"""
|
| 99 |
+
cp ../app.py . && cp ../helpers_SHARED.py .
|
| 100 |
+
git add app.py helpers_SHARED.py
|
| 101 |
+
git commit -m "Sync app.py and helpers_SHARED.py with latest changes" && git push
|
| 102 |
+
"""
|
| 103 |
+
|
| 104 |
+
#%%
|
| 105 |
+
import os
|
| 106 |
+
import shutil
|
| 107 |
+
import warnings
|
| 108 |
+
from datetime import datetime
|
| 109 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 110 |
+
from pydantic import BaseModel
|
| 111 |
+
import gradio as gr
|
| 112 |
+
|
| 113 |
+
# Suppress warnings for cleaner output on HF Spaces
|
| 114 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
| 115 |
+
|
| 116 |
+
# Fix event loop issues on HF Spaces
|
| 117 |
+
if os.getenv("SPACE_ID") is not None:
|
| 118 |
+
try:
|
| 119 |
+
import nest_asyncio
|
| 120 |
+
nest_asyncio.apply()
|
| 121 |
+
except ImportError:
|
| 122 |
+
pass
|
| 123 |
+
|
| 124 |
+
# ============================================================================
|
| 125 |
+
# IMPORT FROM HELPER MODULES
|
| 126 |
+
# ============================================================================
|
| 127 |
+
|
| 128 |
+
from helpers_SHARED import (
|
| 129 |
+
# Configuration
|
| 130 |
+
CONFIG, IS_HF_SPACE, DATA_DIR, EMBEDDINGS_DIR,
|
| 131 |
+
HAS_PERSISTENT_STORAGE, STORAGE_WARNING,
|
| 132 |
+
# Memory functions
|
| 133 |
+
add_to_memory, get_memory_context, search_memory, clear_memory,
|
| 134 |
+
# Utility functions
|
| 135 |
+
get_timestamp, create_elapsed_timer, format_progress_bar,
|
| 136 |
+
# PDF helpers
|
| 137 |
+
get_pdf_list, get_pdf_list_ui, make_pdf_dropdown,
|
| 138 |
+
# Vectorstore
|
| 139 |
+
build_vectorstore, get_vectorstore, set_vectorstore, embeddings,
|
| 140 |
+
# Agent tools
|
| 141 |
+
AGENT_TOOLS, list_documents, count_documents, search_documents,
|
| 142 |
+
# Sample questions
|
| 143 |
+
SAMPLE_Q1, SAMPLE_Q2, SAMPLE_Q3, SAMPLE_Q4, SAMPLE_Q5, SAMPLE_Q6, SAMPLE_Q7,
|
| 144 |
+
)
|
| 145 |
+
from helpers_SHARED import floating_progress_bar_html
|
| 146 |
+
|
| 147 |
+
# Import environment-specific helpers
|
| 148 |
+
if IS_HF_SPACE:
|
| 149 |
+
from helpers_HF import (
|
| 150 |
+
init_hf_llm, hf_generate_chat_response, hf_generate_text_response,
|
| 151 |
+
get_hf_client, get_hf_llm_name
|
| 152 |
+
)
|
| 153 |
+
# Initialize HF LLM (default model from config)
|
| 154 |
+
hf_client, LLM_NAME = init_hf_llm(CONFIG["hf_model"] if "hf_model" in CONFIG else None)
|
| 155 |
+
ollama_llm = None
|
| 156 |
+
agent_executor = None
|
| 157 |
+
else:
|
| 158 |
+
from helpers_LOCAL import (
|
| 159 |
+
init_ollama_llm, ollama_generate_response, run_agent,
|
| 160 |
+
create_langchain_agent, get_ollama_llm, get_local_llm_name, get_agent_executor
|
| 161 |
+
)
|
| 162 |
+
# Initialize Ollama LLM
|
| 163 |
+
ollama_llm, LLM_NAME = init_ollama_llm()
|
| 164 |
+
hf_client = None
|
| 165 |
+
|
| 166 |
+
# Create directories
|
| 167 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 168 |
+
os.makedirs(EMBEDDINGS_DIR, exist_ok=True)
|
| 169 |
+
|
| 170 |
+
# Build initial vectorstore
|
| 171 |
+
vs = build_vectorstore()
|
| 172 |
+
|
| 173 |
+
# Create agent (local only)
|
| 174 |
+
if not IS_HF_SPACE and ollama_llm is not None:
|
| 175 |
+
agent_executor = create_langchain_agent()
|
| 176 |
+
else:
|
| 177 |
+
agent_executor = None
|
| 178 |
+
|
| 179 |
+
# Debug: Print initial state
|
| 180 |
+
print(f"π DEBUG: Initial vectorstore state: {vs is not None}")
|
| 181 |
+
print(f"π DEBUG: IS_HF_SPACE: {IS_HF_SPACE}")
|
| 182 |
+
print(f"π DEBUG: DATA_DIR: {DATA_DIR}")
|
| 183 |
+
print(f"π DEBUG: EMBEDDINGS_DIR: {EMBEDDINGS_DIR}")
|
| 184 |
+
if IS_HF_SPACE:
|
| 185 |
+
print(f"π DEBUG: /data exists: {os.path.exists('/data')}")
|
| 186 |
+
print(f"π DEBUG: HF token available: {os.getenv('HF_token') is not None}")
|
| 187 |
+
print(f"π DEBUG: LLM available: {(hf_client is not None) if IS_HF_SPACE else (ollama_llm is not None)}")
|
| 188 |
+
|
| 189 |
+
# ============================================================================
|
| 190 |
+
# FASTAPI APP (FastAPI is only used for local runs, not on HuggingFace Spaces)
|
| 191 |
+
# ============================================================================
|
| 192 |
+
|
| 193 |
+
app = FastAPI(title="RAG Chatbot API")
|
| 194 |
+
|
| 195 |
+
class Prompt(BaseModel):
|
| 196 |
+
prompt: str
|
| 197 |
+
|
| 198 |
+
@app.get("/pdfs")
|
| 199 |
+
def list_pdfs():
|
| 200 |
+
return {"pdfs": get_pdf_list()}
|
| 201 |
+
|
| 202 |
+
@app.post("/upload")
|
| 203 |
+
async def upload_pdf(file: UploadFile = File(...)):
|
| 204 |
+
if not file.filename or not file.filename.endswith(".pdf"):
|
| 205 |
+
raise HTTPException(status_code=400, detail="Only PDFs allowed.")
|
| 206 |
+
|
| 207 |
+
filepath = os.path.join(DATA_DIR, file.filename)
|
| 208 |
+
with open(filepath, "wb") as f:
|
| 209 |
+
f.write(await file.read())
|
| 210 |
+
|
| 211 |
+
build_vectorstore(force_rebuild=True)
|
| 212 |
+
return {"message": f"Added {file.filename}. Embeddings updated."}
|
| 213 |
+
|
| 214 |
+
@app.delete("/delete/{filename}")
|
| 215 |
+
def delete_pdf(filename: str):
|
| 216 |
+
if filename not in get_pdf_list():
|
| 217 |
+
raise HTTPException(status_code=404, detail="PDF not found.")
|
| 218 |
+
|
| 219 |
+
filepath = os.path.join(DATA_DIR, filename)
|
| 220 |
+
os.remove(filepath)
|
| 221 |
+
build_vectorstore(force_rebuild=True)
|
| 222 |
+
return {"message": f"Deleted {filename}. Embeddings updated."}
|
| 223 |
+
|
| 224 |
+
@app.post("/generate")
|
| 225 |
+
def generate_response(prompt: Prompt):
|
| 226 |
+
global vs
|
| 227 |
+
vs = get_vectorstore()
|
| 228 |
+
|
| 229 |
+
if vs is None:
|
| 230 |
+
raise HTTPException(status_code=400, detail="No PDFs loaded.")
|
| 231 |
+
|
| 232 |
+
# Retrieve relevant docs (limit context size)
|
| 233 |
+
retriever = vs.as_retriever(search_kwargs={"k": CONFIG["search_k"]})
|
| 234 |
+
docs = retriever.invoke(prompt.prompt)
|
| 235 |
+
# Use all retrieved chunks
|
| 236 |
+
context = "\n\n".join([doc.page_content for doc in docs])
|
| 237 |
+
|
| 238 |
+
# Augment prompt
|
| 239 |
+
full_prompt = (
|
| 240 |
+
"Answer the following question based ONLY on the context provided below.\n"
|
| 241 |
+
"If the answer is not present in the context, reply exactly with: 'I don't know.'\n"
|
| 242 |
+
"Do NOT make up or guess any information that is not explicitly in the context.\n\n"
|
| 243 |
+
"Your answer MUST be a concise summary, listing the main topics or key points found in the context.\n"
|
| 244 |
+
"If the question asks for a list, provide a bulleted or numbered list.\n\n"
|
| 245 |
+
f"Context:\n{context}\n\n"
|
| 246 |
+
f"Question: {prompt.prompt}\n\n"
|
| 247 |
+
"Answer:"
|
| 248 |
+
)
|
| 249 |
+
|
| 250 |
+
try:
|
| 251 |
+
if IS_HF_SPACE and hf_client is not None:
|
| 252 |
+
response = hf_generate_text_response(full_prompt, context, hf_client)
|
| 253 |
+
return {"response": response}
|
| 254 |
+
elif not IS_HF_SPACE and ollama_llm is not None:
|
| 255 |
+
print(f"Generating response with Ollama ({LLM_NAME})...")
|
| 256 |
+
try:
|
| 257 |
+
response = ollama_llm.invoke(full_prompt)
|
| 258 |
+
print(f"β Success! Response generated.")
|
| 259 |
+
return {"response": response}
|
| 260 |
+
except Exception as ollama_error:
|
| 261 |
+
print(f"β Ollama error: {str(ollama_error)}")
|
| 262 |
+
return {"response": f"I found relevant information in your documents:\n\n{context[:CONFIG['search_content_limit']]}..."}
|
| 263 |
+
else:
|
| 264 |
+
return {"response": f"I found relevant information in your documents:\n\n{context[:CONFIG['search_content_limit']]}..."}
|
| 265 |
+
except Exception as e:
|
| 266 |
+
print(f"LLM failed: {str(e)}")
|
| 267 |
+
return {"response": f"I found relevant information in your documents:\n\n{context[:CONFIG['search_content_limit']]}..."}
|
| 268 |
+
|
| 269 |
+
@app.get("/refresh")
|
| 270 |
+
def refresh_embeddings():
|
| 271 |
+
build_vectorstore(force_rebuild=True)
|
| 272 |
+
return {"message": "Embeddings refreshed."}
|
| 273 |
+
|
| 274 |
+
# ============================================================================
|
| 275 |
+
# GRADIO UI FUNCTIONS
|
| 276 |
+
# ============================================================================
|
| 277 |
+
|
| 278 |
+
def add_pdf(files):
|
| 279 |
+
if files is None or len(files) == 0:
|
| 280 |
+
return (
|
| 281 |
+
make_pdf_dropdown(),
|
| 282 |
+
"No files selected.",
|
| 283 |
+
"",
|
| 284 |
+
"\n".join(get_pdf_list())
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
start_time = datetime.now()
|
| 288 |
+
get_elapsed = create_elapsed_timer(start_time)
|
| 289 |
+
results = []
|
| 290 |
+
total_files = len(files)
|
| 291 |
+
upload_log = []
|
| 292 |
+
|
| 293 |
+
upload_log.append(f"[{get_timestamp()}] Starting upload process for {total_files} file(s)")
|
| 294 |
+
|
| 295 |
+
for i, file_obj in enumerate(files, 1):
|
| 296 |
+
filename = os.path.basename(file_obj.name)
|
| 297 |
+
|
| 298 |
+
progress_percent = int((i * 2 - 1) / (total_files * 2) * 100)
|
| 299 |
+
status_msg = f"π€ Uploading {i}/{total_files}: {filename}..."
|
| 300 |
+
progress_display = format_progress_bar(get_elapsed(), progress_percent, status_msg)
|
| 301 |
+
|
| 302 |
+
upload_log.append(f"[{get_timestamp()}] Uploading file {i}: {filename}")
|
| 303 |
+
|
| 304 |
+
# Show current embedded files (before upload)
|
| 305 |
+
yield (
|
| 306 |
+
make_pdf_dropdown(),
|
| 307 |
+
"\n".join(results) if results else "Starting upload...",
|
| 308 |
+
progress_display,
|
| 309 |
+
"\n".join(get_pdf_list())
|
| 310 |
+
)
|
| 311 |
+
|
| 312 |
+
try:
|
| 313 |
+
dest_path = os.path.join(DATA_DIR, filename)
|
| 314 |
+
shutil.copy2(file_obj.name, dest_path)
|
| 315 |
+
results.append(f"β {filename} uploaded")
|
| 316 |
+
|
| 317 |
+
upload_log.append(f"[{get_timestamp()}] Uploading file {i} completed")
|
| 318 |
+
|
| 319 |
+
progress_percent = int(((i * 2) - 1) / (total_files * 2) * 100)
|
| 320 |
+
status_msg = f"π§ Creating embeddings for {filename}..."
|
| 321 |
+
progress_display = format_progress_bar(get_elapsed(), progress_percent, status_msg)
|
| 322 |
+
upload_log.append(f"[{get_timestamp()}] Embedding file {i}: {filename}")
|
| 323 |
+
|
| 324 |
+
yield (
|
| 325 |
+
make_pdf_dropdown(),
|
| 326 |
+
"\n".join(results),
|
| 327 |
+
progress_display,
|
| 328 |
+
"\n".join(get_pdf_list())
|
| 329 |
+
)
|
| 330 |
+
|
| 331 |
+
try:
|
| 332 |
+
build_vectorstore(force_rebuild=True)
|
| 333 |
+
results[-1] = f"β
{filename} (uploaded & embedded)"
|
| 334 |
+
upload_log.append(f"[{get_timestamp()}] Embedding file {i} completed")
|
| 335 |
+
upload_log.append("")
|
| 336 |
+
# Show progress bar after embedding completes
|
| 337 |
+
progress_percent = int((i * 2) / (total_files * 2) * 100)
|
| 338 |
+
status_msg = f"β
Embedded {i}/{total_files}: {filename}"
|
| 339 |
+
progress_display = format_progress_bar(get_elapsed(), progress_percent, status_msg)
|
| 340 |
+
# Update embedded files to show the new file
|
| 341 |
+
yield (
|
| 342 |
+
make_pdf_dropdown(),
|
| 343 |
+
"\n".join(results),
|
| 344 |
+
progress_display,
|
| 345 |
+
"\n".join(get_pdf_list())
|
| 346 |
+
)
|
| 347 |
+
except Exception as embed_error:
|
| 348 |
+
results[-1] = f"β οΈ {filename} (uploaded, embedding error: {str(embed_error)})"
|
| 349 |
+
upload_log.append(f"[{get_timestamp()}] Embedding file {i} failed")
|
| 350 |
+
upload_log.append("")
|
| 351 |
+
|
| 352 |
+
completed_progress = int((i * 2) / (total_files * 2) * 100)
|
| 353 |
+
status_msg = f"β οΈ File {i}/{total_files} completed with error: {filename}"
|
| 354 |
+
progress_display = format_progress_bar(get_elapsed(), completed_progress, status_msg)
|
| 355 |
+
|
| 356 |
+
yield (
|
| 357 |
+
make_pdf_dropdown(),
|
| 358 |
+
"\n".join(results),
|
| 359 |
+
progress_display,
|
| 360 |
+
"\n".join(get_pdf_list())
|
| 361 |
+
)
|
| 362 |
+
except Exception as e:
|
| 363 |
+
results.append(f"β {filename}: {str(e)}")
|
| 364 |
+
upload_log.append(f"[{get_timestamp()}] Uploading file {i} failed")
|
| 365 |
+
|
| 366 |
+
final_message = "\n".join(results)
|
| 367 |
+
final_progress = format_progress_bar(get_elapsed(), 100, f"π All done! Processed {len(files)} file(s) successfully")
|
| 368 |
+
upload_log.append(f"[{get_timestamp()}] All {len(files)} file(s) completed")
|
| 369 |
+
|
| 370 |
+
# Only show fully embedded files in the Available Embedded Files window
|
| 371 |
+
# Reset the progress bar to its original empty state after completion (like delete)
|
| 372 |
+
yield (
|
| 373 |
+
make_pdf_dropdown(),
|
| 374 |
+
final_message,
|
| 375 |
+
"",
|
| 376 |
+
"\n".join(get_pdf_list())
|
| 377 |
+
)
|
| 378 |
+
|
| 379 |
+
def delete_pdf_ui(selected_pdf):
|
| 380 |
+
import time
|
| 381 |
+
if not selected_pdf:
|
| 382 |
+
# Hide overlay if nothing to delete
|
| 383 |
+
yield make_pdf_dropdown(), "\n".join(get_pdf_list()), ""
|
| 384 |
+
return
|
| 385 |
+
# Show progress bar immediately on click
|
| 386 |
+
bar = format_progress_bar("", 0, "Preparing to delete files...")
|
| 387 |
+
yield make_pdf_dropdown(), "\n".join(get_pdf_list()), bar
|
| 388 |
+
|
| 389 |
+
# Support both single and multiple selection
|
| 390 |
+
if isinstance(selected_pdf, str):
|
| 391 |
+
selected_files = [selected_pdf]
|
| 392 |
+
else:
|
| 393 |
+
selected_files = list(selected_pdf)
|
| 394 |
+
|
| 395 |
+
total_files = len(selected_files)
|
| 396 |
+
for idx, file_name in enumerate(selected_files, 1):
|
| 397 |
+
file_path = os.path.join(DATA_DIR, file_name)
|
| 398 |
+
# Remove file and all leftovers (e.g., embeddings) before advancing progress
|
| 399 |
+
deleted = False
|
| 400 |
+
leftovers_removed = False
|
| 401 |
+
# Remove file
|
| 402 |
+
if os.path.exists(file_path):
|
| 403 |
+
try:
|
| 404 |
+
os.remove(file_path)
|
| 405 |
+
deleted = True
|
| 406 |
+
except Exception:
|
| 407 |
+
deleted = False
|
| 408 |
+
# Remove leftovers (add your per-file embedding removal logic here if needed)
|
| 409 |
+
# Example: remove embedding file if it exists (customize as needed)
|
| 410 |
+
embedding_path = os.path.join(EMBEDDINGS_DIR, file_name + ".embedding")
|
| 411 |
+
if os.path.exists(embedding_path):
|
| 412 |
+
try:
|
| 413 |
+
os.remove(embedding_path)
|
| 414 |
+
leftovers_removed = True
|
| 415 |
+
except Exception:
|
| 416 |
+
leftovers_removed = False
|
| 417 |
+
else:
|
| 418 |
+
leftovers_removed = True # No leftovers to remove
|
| 419 |
+
# Only advance progress bar after both file and leftovers are deleted
|
| 420 |
+
if deleted and leftovers_removed:
|
| 421 |
+
build_vectorstore(force_rebuild=True)
|
| 422 |
+
percent = int(idx / total_files * 100) if total_files else 100
|
| 423 |
+
bar = format_progress_bar("", percent, f"Deleted {idx}/{total_files}: {file_name}")
|
| 424 |
+
yield make_pdf_dropdown(), "\n".join(get_pdf_list()), bar
|
| 425 |
+
else:
|
| 426 |
+
bar = format_progress_bar("", int(idx / total_files * 100) if total_files else 100, f"β οΈ Error deleting {file_name}")
|
| 427 |
+
yield make_pdf_dropdown(), "\n".join(get_pdf_list()), bar
|
| 428 |
+
time.sleep(0.2)
|
| 429 |
+
# Clear progress bar after all deletions
|
| 430 |
+
yield make_pdf_dropdown(), "\n".join(get_pdf_list()), ""
|
| 431 |
+
|
| 432 |
+
def toggle_delete_all_btn():
|
| 433 |
+
# Check if there is at least one file in Available Embedded Files
|
| 434 |
+
files = get_pdf_list()
|
| 435 |
+
return gr.update(interactive=bool(files))
|
| 436 |
+
|
| 437 |
+
def delete_all_files():
|
| 438 |
+
import time
|
| 439 |
+
all_files = get_pdf_list()
|
| 440 |
+
if not all_files:
|
| 441 |
+
bar = format_progress_bar("", 0, "No files to delete.")
|
| 442 |
+
yield make_pdf_dropdown(), "\n".join(get_pdf_list()), bar
|
| 443 |
+
return
|
| 444 |
+
bar = format_progress_bar("", 0, "Preparing to delete all files...")
|
| 445 |
+
yield make_pdf_dropdown(), "\n".join(get_pdf_list()), bar
|
| 446 |
+
total_files = len(all_files)
|
| 447 |
+
for idx, file_name in enumerate(all_files, 1):
|
| 448 |
+
file_path = os.path.join(DATA_DIR, file_name)
|
| 449 |
+
deleted = False
|
| 450 |
+
leftovers_removed = False
|
| 451 |
+
if os.path.exists(file_path):
|
| 452 |
+
try:
|
| 453 |
+
os.remove(file_path)
|
| 454 |
+
deleted = True
|
| 455 |
+
except Exception:
|
| 456 |
+
deleted = False
|
| 457 |
+
embedding_path = os.path.join(EMBEDDINGS_DIR, file_name + ".embedding")
|
| 458 |
+
if os.path.exists(embedding_path):
|
| 459 |
+
try:
|
| 460 |
+
os.remove(embedding_path)
|
| 461 |
+
leftovers_removed = True
|
| 462 |
+
except Exception:
|
| 463 |
+
leftovers_removed = False
|
| 464 |
+
else:
|
| 465 |
+
leftovers_removed = True
|
| 466 |
+
if deleted and leftovers_removed:
|
| 467 |
+
build_vectorstore(force_rebuild=True)
|
| 468 |
+
percent = int(idx / total_files * 100) if total_files else 100
|
| 469 |
+
bar = format_progress_bar("", percent, f"Deleted {idx}/{total_files}: {file_name}")
|
| 470 |
+
yield make_pdf_dropdown(), "\n".join(get_pdf_list()), bar
|
| 471 |
+
else:
|
| 472 |
+
bar = format_progress_bar("", int(idx / total_files * 100) if total_files else 100, f"β οΈ Error deleting {file_name}")
|
| 473 |
+
yield make_pdf_dropdown(), "\n".join(get_pdf_list()), bar
|
| 474 |
+
time.sleep(0.2)
|
| 475 |
+
yield make_pdf_dropdown(), "\n".join(get_pdf_list()), ""
|
| 476 |
+
|
| 477 |
+
def show_delete_all_warning():
|
| 478 |
+
return (
|
| 479 |
+
gr.Markdown("**β οΈ Are you sure you want to delete ALL files? This cannot be undone. Click 'Confirm Delete All' to proceed.**", visible=True),
|
| 480 |
+
gr.update(interactive=True, visible=True)
|
| 481 |
+
)
|
| 482 |
+
|
| 483 |
+
def hide_delete_all_warning():
|
| 484 |
+
return (
|
| 485 |
+
gr.Markdown(visible=False),
|
| 486 |
+
gr.update(interactive=False, visible=False)
|
| 487 |
+
)
|
| 488 |
+
def analyze_query_and_use_tools(query: str) -> str:
|
| 489 |
+
"""Analyze query and use appropriate tools to gather information."""
|
| 490 |
+
query_lower = query.lower()
|
| 491 |
+
results = []
|
| 492 |
+
|
| 493 |
+
# Check for memory-related queries first
|
| 494 |
+
memory_keywords = ["remember", "earlier", "before", "previous", "last time", "we discussed",
|
| 495 |
+
"you said", "i asked", "conversation", "history", "recall", "what did we"]
|
| 496 |
+
if any(word in query_lower for word in memory_keywords):
|
| 497 |
+
print(f"π§ Memory query detected, fetching conversation history...")
|
| 498 |
+
memory_result = get_memory_context(last_n=10)
|
| 499 |
+
if memory_result and "No previous conversation" not in memory_result:
|
| 500 |
+
results.append(f"π **Conversation History:**\n{memory_result}")
|
| 501 |
+
search_result = search_memory(query)
|
| 502 |
+
if search_result and "No conversation history" not in search_result:
|
| 503 |
+
results.append(f"π **Relevant Past Discussions:**\n{search_result}")
|
| 504 |
+
|
| 505 |
+
if results:
|
| 506 |
+
return "\n\n".join(results)
|
| 507 |
+
|
| 508 |
+
# Try using LangGraph agent (local only)
|
| 509 |
+
if not IS_HF_SPACE and agent_executor is not None:
|
| 510 |
+
agent_result = run_agent(query)
|
| 511 |
+
if agent_result:
|
| 512 |
+
return agent_result
|
| 513 |
+
|
| 514 |
+
# Fallback: Manual tool routing
|
| 515 |
+
try:
|
| 516 |
+
if any(word in query_lower for word in ["what documents", "list documents", "available documents", "what files", "documents do i have"]):
|
| 517 |
+
results.append(list_documents.invoke({}))
|
| 518 |
+
|
| 519 |
+
if any(word in query_lower for word in ["how many", "count", "number of documents"]):
|
| 520 |
+
results.append(count_documents.invoke({}))
|
| 521 |
+
|
| 522 |
+
results.append(search_documents.invoke({"query": query}))
|
| 523 |
+
|
| 524 |
+
return "\n\n".join(results) if results else "No relevant information found."
|
| 525 |
+
|
| 526 |
+
except Exception as e:
|
| 527 |
+
return f"Error analyzing query: {str(e)}"
|
| 528 |
+
|
| 529 |
+
def chat_response(message, history):
|
| 530 |
+
"""Agent-enhanced chat response function with visual progress tracking."""
|
| 531 |
+
global vs
|
| 532 |
+
if not message:
|
| 533 |
+
return history, "", "π¬ Ready for your question"
|
| 534 |
+
|
| 535 |
+
start_time = datetime.now()
|
| 536 |
+
get_elapsed = create_elapsed_timer(start_time)
|
| 537 |
+
|
| 538 |
+
if not isinstance(history, list):
|
| 539 |
+
history = []
|
| 540 |
+
|
| 541 |
+
history.append({"role": "user", "content": str(message)})
|
| 542 |
+
add_to_memory("user", message)
|
| 543 |
+
|
| 544 |
+
try:
|
| 545 |
+
yield (history, "", format_progress_bar(get_elapsed(), 33, "π Analyzing your question...", bar_length=15))
|
| 546 |
+
print(f"π€ Agent analyzing query: {message}")
|
| 547 |
+
|
| 548 |
+
try:
|
| 549 |
+
pdf_files = get_pdf_list()
|
| 550 |
+
print(f"π DEBUG: PDF files available: {len(pdf_files)} - {pdf_files}")
|
| 551 |
+
print(f"π DEBUG: Global vectorstore state: {get_vectorstore() is not None}")
|
| 552 |
+
except Exception as debug_error:
|
| 553 |
+
print(f"π DEBUG ERROR: {str(debug_error)}")
|
| 554 |
+
|
| 555 |
+
try:
|
| 556 |
+
tool_results = analyze_query_and_use_tools(message)
|
| 557 |
+
print(f"π§ Tool results: {tool_results[:100]}...")
|
| 558 |
+
except Exception as tool_error:
|
| 559 |
+
error_msg = f"β Tool execution failed: {str(tool_error)}"
|
| 560 |
+
print(error_msg)
|
| 561 |
+
history.append({"role": "assistant", "content": error_msg})
|
| 562 |
+
yield (history, "", f"{get_elapsed()} | [100%] β Error during tool execution")
|
| 563 |
+
return
|
| 564 |
+
|
| 565 |
+
yield (history, "", format_progress_bar(get_elapsed(), 66, "π§ Generating intelligent response...", bar_length=15))
|
| 566 |
+
|
| 567 |
+
try:
|
| 568 |
+
memory_context = get_memory_context(last_n=5)
|
| 569 |
+
llm_prompt = f"""
|
| 570 |
+
You are a helpful assistant with memory of past conversations. Answer the question based on the document excerpts and conversation history.
|
| 571 |
+
|
| 572 |
+
Recent Conversation History:
|
| 573 |
+
{memory_context}
|
| 574 |
+
|
| 575 |
+
Document excerpts:
|
| 576 |
+
{tool_results}
|
| 577 |
+
|
| 578 |
+
Question: {message}
|
| 579 |
+
|
| 580 |
+
Answer concisely and accurately. If the user refers to previous discussions, use the conversation history to provide context:
|
| 581 |
+
"""
|
| 582 |
+
|
| 583 |
+
if IS_HF_SPACE and hf_client is not None:
|
| 584 |
+
result = hf_generate_chat_response(llm_prompt, hf_client)
|
| 585 |
+
if result is None:
|
| 586 |
+
result = tool_results
|
| 587 |
+
elif not IS_HF_SPACE and ollama_llm is not None:
|
| 588 |
+
result = ollama_generate_response(llm_prompt, ollama_llm)
|
| 589 |
+
if result is None:
|
| 590 |
+
result = tool_results
|
| 591 |
+
else:
|
| 592 |
+
result = tool_results
|
| 593 |
+
print("βΉοΈ No LLM available, returning tool results")
|
| 594 |
+
except Exception as llm_error:
|
| 595 |
+
print(f"β LLM processing error: {str(llm_error)}")
|
| 596 |
+
result = tool_results
|
| 597 |
+
|
| 598 |
+
result_str = str(result.content) if hasattr(result, 'content') else str(result)
|
| 599 |
+
history.append({"role": "assistant", "content": result_str})
|
| 600 |
+
add_to_memory("assistant", result_str)
|
| 601 |
+
|
| 602 |
+
yield (history, "", format_progress_bar(get_elapsed(), 100, "β
Response generated successfully!", bar_length=15))
|
| 603 |
+
# Reset AI Processing Progress to original state
|
| 604 |
+
yield (history, "", "π¬ Ready for your question")
|
| 605 |
+
|
| 606 |
+
except Exception as e:
|
| 607 |
+
error_msg = f"π« System error: {str(e)}\n\nPlease try again or upload your documents again."
|
| 608 |
+
print(f"π₯ CRITICAL ERROR: {str(e)}")
|
| 609 |
+
import traceback
|
| 610 |
+
traceback.print_exc()
|
| 611 |
+
history.append({"role": "assistant", "content": error_msg})
|
| 612 |
+
yield (history, "", f"{get_elapsed()} | [100%] β System error occurred")
|
| 613 |
+
|
| 614 |
+
|
| 615 |
+
def refresh_embeddings_ui():
|
| 616 |
+
"""Refresh embeddings directly"""
|
| 617 |
+
try:
|
| 618 |
+
build_vectorstore(force_rebuild=True)
|
| 619 |
+
return make_pdf_dropdown(), "Embeddings refreshed."
|
| 620 |
+
except Exception as e:
|
| 621 |
+
return make_pdf_dropdown(), f"Error refreshing embeddings: {str(e)}"
|
| 622 |
+
|
| 623 |
+
def clear_chat_and_memory():
|
| 624 |
+
"""Clear chat history and conversation memory."""
|
| 625 |
+
clear_memory()
|
| 626 |
+
return [], "", "π¬ Chat and memory cleared. Ready for your question"
|
| 627 |
+
|
| 628 |
+
# ============================================================================
|
| 629 |
+
# GRADIO UI
|
| 630 |
+
# ============================================================================
|
| 631 |
+
|
| 632 |
+
ENV_NAME = "π HuggingFace Space" if IS_HF_SPACE else "π» Local Environment"
|
| 633 |
+
ENV_COLOR = "#FF6B6B" if IS_HF_SPACE else "#4ECDC4"
|
| 634 |
+
|
| 635 |
+
with gr.Blocks(title="RAG Agent Chatbot") as demo:
|
| 636 |
+
gr.Markdown(f"# π€ RAG Agent - AI Assistant with Tools\nUpload PDFs and interact with an intelligent agent that can search, analyze, and answer questions about your documents.")
|
| 637 |
+
if not IS_HF_SPACE:
|
| 638 |
+
from helpers_LOCAL import get_installed_llms, init_ollama_llm, create_langchain_agent
|
| 639 |
+
llm_choices = get_installed_llms()
|
| 640 |
+
if llm_choices:
|
| 641 |
+
llm_dropdown = gr.Dropdown(
|
| 642 |
+
label="Select Local LLM",
|
| 643 |
+
choices=llm_choices,
|
| 644 |
+
value=LLM_NAME if LLM_NAME in llm_choices else (llm_choices[0] if llm_choices else None),
|
| 645 |
+
interactive=True,
|
| 646 |
+
visible=True
|
| 647 |
+
)
|
| 648 |
+
current_llm_display = gr.Markdown(f"**Current LLM:** {LLM_NAME if LLM_NAME else ''}", elem_id="current-llm-display", visible=True)
|
| 649 |
+
top_banner = gr.Markdown(
|
| 650 |
+
f"<div style='background-color: {ENV_COLOR}; padding: 10px; border-radius: 5px; text-align: center; color: white; font-weight: bold;'>Running on: {ENV_NAME} | LLM: <span id='llm-name'>{LLM_NAME if LLM_NAME else 'None'}</span> | Agent: β
Active</div>",
|
| 651 |
+
elem_id="top-llm-banner"
|
| 652 |
+
)
|
| 653 |
+
|
| 654 |
+
def update_llm(selected_label):
|
| 655 |
+
global ollama_llm, LLM_NAME, agent_executor
|
| 656 |
+
if selected_label:
|
| 657 |
+
try:
|
| 658 |
+
ollama_llm, LLM_NAME = init_ollama_llm(selected_label)
|
| 659 |
+
agent_executor = create_langchain_agent()
|
| 660 |
+
banner_html = f"<div style='background-color: {ENV_COLOR}; padding: 10px; border-radius: 5px; text-align: center; color: white; font-weight: bold;'>Running on: {ENV_NAME} | LLM: <span id='llm-name'>{selected_label}</span> | Agent: β
Active</div>"
|
| 661 |
+
return (
|
| 662 |
+
gr.Markdown(f"**Current LLM:** {selected_label}", elem_id="current-llm-display"),
|
| 663 |
+
banner_html
|
| 664 |
+
)
|
| 665 |
+
except Exception as e:
|
| 666 |
+
ollama_llm = None
|
| 667 |
+
LLM_NAME = None
|
| 668 |
+
agent_executor = None
|
| 669 |
+
banner_html = f"<div style='background-color: {ENV_COLOR}; padding: 10px; border-radius: 5px; text-align: center; color: white; font-weight: bold;'>Running on: {ENV_NAME} | LLM: <span id='llm-name'>None</span> | Agent: β Inactive</div>"
|
| 670 |
+
return (
|
| 671 |
+
gr.Markdown(f"**Current LLM:** (Error initializing {selected_label})", elem_id="current-llm-display"),
|
| 672 |
+
banner_html
|
| 673 |
+
)
|
| 674 |
+
banner_html = f"<div style='background-color: {ENV_COLOR}; padding: 10px; border-radius: 5px; text-align: center; color: white; font-weight: bold;'>Running on: {ENV_NAME} | LLM: <span id='llm-name'>None</span> | Agent: β Inactive</div>"
|
| 675 |
+
return gr.Markdown("", elem_id="current-llm-display"), banner_html
|
| 676 |
+
|
| 677 |
+
llm_dropdown.change(
|
| 678 |
+
fn=lambda label: update_llm(label),
|
| 679 |
+
inputs=[llm_dropdown],
|
| 680 |
+
outputs=[current_llm_display, top_banner]
|
| 681 |
+
)
|
| 682 |
+
else:
|
| 683 |
+
gr.Markdown(
|
| 684 |
+
"<div style='background-color: #ffcccc; padding: 10px; border-radius: 5px; text-align: center; color: #b30000; font-weight: bold;'>⚠ <b>No local LLMs are installed.</b> Please install an Ollama model to enable LLM selection and chat capabilities.</div>"
|
| 685 |
+
)
|
| 686 |
+
llm_dropdown = gr.Dropdown(
|
| 687 |
+
label="Select Local LLM",
|
| 688 |
+
choices=[],
|
| 689 |
+
value=None,
|
| 690 |
+
interactive=False,
|
| 691 |
+
visible=True
|
| 692 |
+
)
|
| 693 |
+
current_llm_display = gr.Markdown(f"**Current LLM:** None", elem_id="current-llm-display", visible=True)
|
| 694 |
+
gr.Markdown(f"<div style='background-color: {ENV_COLOR}; padding: 10px; border-radius: 5px; text-align: center; color: white; font-weight: bold;'>Running on: {ENV_NAME} | LLM: <span id='llm-name'>None</span> | Agent: β Inactive</div>")
|
| 695 |
+
else:
|
| 696 |
+
# --- Hugging Face Space: dynamic LLM selection ---
|
| 697 |
+
# Static list of free, popular LLMs on HF Inference API (can be expanded)
|
| 698 |
+
hf_llm_choices = [
|
| 699 |
+
"Llama-3.2-3B-Instruct",
|
| 700 |
+
"meta-llama/Meta-Llama-3-8B-Instruct",
|
| 701 |
+
"mistralai/Mistral-7B-Instruct-v0.2",
|
| 702 |
+
"google/gemma-7b-it",
|
| 703 |
+
"HuggingFaceH4/zephyr-7b-beta",
|
| 704 |
+
"Qwen/Qwen1.5-7B-Chat",
|
| 705 |
+
"tiiuae/falcon-7b-instruct"
|
| 706 |
+
]
|
| 707 |
+
default_llm = "Llama-3.2-3B-Instruct"
|
| 708 |
+
llm_dropdown = gr.Dropdown(
|
| 709 |
+
label="Select HF LLM",
|
| 710 |
+
choices=hf_llm_choices,
|
| 711 |
+
value=LLM_NAME if LLM_NAME in hf_llm_choices else default_llm,
|
| 712 |
+
interactive=True,
|
| 713 |
+
visible=True
|
| 714 |
+
)
|
| 715 |
+
current_llm_display = gr.Markdown(f"**Current LLM:** {LLM_NAME if LLM_NAME else default_llm}", elem_id="current-llm-display", visible=True)
|
| 716 |
+
top_banner = gr.Markdown(
|
| 717 |
+
f"<div style='background-color: {ENV_COLOR}; padding: 10px; border-radius: 5px; text-align: center; color: white; font-weight: bold;'>Running on: {ENV_NAME} | LLM: <span id='llm-name'>{LLM_NAME if LLM_NAME else default_llm}</span> | Agent: β
Active</div>",
|
| 718 |
+
elem_id="top-llm-banner"
|
| 719 |
+
)
|
| 720 |
+
|
| 721 |
+
def update_hf_llm(selected_label):
|
| 722 |
+
global hf_client, LLM_NAME
|
| 723 |
+
from helpers_HF import init_hf_llm
|
| 724 |
+
if selected_label:
|
| 725 |
+
try:
|
| 726 |
+
hf_client, LLM_NAME = init_hf_llm(selected_label)
|
| 727 |
+
banner_html = f"<div style='background-color: {ENV_COLOR}; padding: 10px; border-radius: 5px; text-align: center; color: white; font-weight: bold;'>Running on: {ENV_NAME} | LLM: <span id='llm-name'>{selected_label}</span> | Agent: β
Active</div>"
|
| 728 |
+
return (
|
| 729 |
+
gr.Markdown(f"**Current LLM:** {selected_label}", elem_id="current-llm-display"),
|
| 730 |
+
banner_html
|
| 731 |
+
)
|
| 732 |
+
except Exception as e:
|
| 733 |
+
hf_client = None
|
| 734 |
+
LLM_NAME = None
|
| 735 |
+
banner_html = f"<div style='background-color: {ENV_COLOR}; padding: 10px; border-radius: 5px; text-align: center; color: white; font-weight: bold;'>Running on: {ENV_NAME} | LLM: <span id='llm-name'>None</span> | Agent: β Inactive</div>"
|
| 736 |
+
return (
|
| 737 |
+
gr.Markdown(f"**Current LLM:** (Error initializing {selected_label})", elem_id="current-llm-display"),
|
| 738 |
+
banner_html
|
| 739 |
+
)
|
| 740 |
+
banner_html = f"<div style='background-color: {ENV_COLOR}; padding: 10px; border-radius: 5px; text-align: center; color: white; font-weight: bold;'>Running on: {ENV_NAME} | LLM: <span id='llm-name'>None</span> | Agent: β Inactive</div>"
|
| 741 |
+
return gr.Markdown("", elem_id="current-llm-display"), banner_html
|
| 742 |
+
|
| 743 |
+
llm_dropdown.change(
|
| 744 |
+
fn=lambda label: update_hf_llm(label),
|
| 745 |
+
inputs=[llm_dropdown],
|
| 746 |
+
outputs=[current_llm_display, top_banner]
|
| 747 |
+
)
|
| 748 |
+
|
| 749 |
+
with gr.Row():
|
| 750 |
+
with gr.Column(scale=1):
|
| 751 |
+
gr.Markdown("### π File Management")
|
| 752 |
+
if IS_HF_SPACE and STORAGE_WARNING:
|
| 753 |
+
gr.Markdown(f"**Storage Status:** {STORAGE_WARNING}")
|
| 754 |
+
file_upload = gr.File(
|
| 755 |
+
label="Upload Files (Multiple files supported)",
|
| 756 |
+
file_types=[".pdf"],
|
| 757 |
+
file_count="multiple"
|
| 758 |
+
)
|
| 759 |
+
|
| 760 |
+
upload_status = gr.Textbox(
|
| 761 |
+
label="Upload Status",
|
| 762 |
+
value="",
|
| 763 |
+
interactive=False,
|
| 764 |
+
lines=8,
|
| 765 |
+
max_lines=8,
|
| 766 |
+
autoscroll=True
|
| 767 |
+
)
|
| 768 |
+
with gr.Row():
|
| 769 |
+
progress_bar = gr.Textbox(
|
| 770 |
+
label="Uploading Progress",
|
| 771 |
+
value="",
|
| 772 |
+
interactive=False,
|
| 773 |
+
lines=1,
|
| 774 |
+
max_lines=1,
|
| 775 |
+
autoscroll=True
|
| 776 |
+
)
|
| 777 |
+
delete_progress_bar = gr.Textbox(
|
| 778 |
+
label="Deleting Progress",
|
| 779 |
+
value="",
|
| 780 |
+
interactive=False,
|
| 781 |
+
lines=1,
|
| 782 |
+
max_lines=1,
|
| 783 |
+
autoscroll=True
|
| 784 |
+
)
|
| 785 |
+
embedded_files = gr.Textbox(
|
| 786 |
+
label="Available Embedded Files",
|
| 787 |
+
value="\n".join(get_pdf_list()),
|
| 788 |
+
interactive=False,
|
| 789 |
+
lines=8,
|
| 790 |
+
max_lines=8,
|
| 791 |
+
autoscroll=True
|
| 792 |
+
)
|
| 793 |
+
|
| 794 |
+
with gr.Row():
|
| 795 |
+
pdf_dropdown = gr.Dropdown(
|
| 796 |
+
label="Select Files to Delete",
|
| 797 |
+
choices=get_pdf_list_ui(),
|
| 798 |
+
interactive=True,
|
| 799 |
+
allow_custom_value=False,
|
| 800 |
+
multiselect=True
|
| 801 |
+
)
|
| 802 |
+
delete_btn = gr.Button("ποΈ Delete Files", variant="stop", interactive=False)
|
| 803 |
+
delete_all_btn = gr.Button("ποΈ Delete All", variant="stop", interactive=bool(get_pdf_list()))
|
| 804 |
+
delete_all_warning = gr.Markdown(visible=False)
|
| 805 |
+
confirm_delete_all_btn = gr.Button("Confirm Delete All", variant="stop", interactive=True, visible=False)
|
| 806 |
+
delete_progress_overlay = gr.HTML(floating_progress_bar_html())
|
| 807 |
+
|
| 808 |
+
with gr.Column(scale=4):
|
| 809 |
+
gr.Markdown("### π€ AI Agent Chat")
|
| 810 |
+
gr.Markdown("**Agent Capabilities:** Search documents, list files, count documents, intelligent reasoning")
|
| 811 |
+
chatbot = gr.Chatbot(height=CONFIG["chatbot_height"], layout="bubble")
|
| 812 |
+
|
| 813 |
+
if IS_HF_SPACE and not HAS_PERSISTENT_STORAGE:
|
| 814 |
+
gr.Markdown("β οΈ **Storage Notice:** Files are temporary and will be lost when Space restarts. To enable persistent storage, upgrade to a paid plan in Settings β Hardware.")
|
| 815 |
+
gr.Markdown("**π οΈ Agent Commands - Try these tool-powered queries:**")
|
| 816 |
+
with gr.Row():
|
| 817 |
+
sample1 = gr.Button(f"π {SAMPLE_Q1}", size="sm")
|
| 818 |
+
sample2 = gr.Button(f"π {SAMPLE_Q2}", size="sm")
|
| 819 |
+
sample3 = gr.Button(f"π {SAMPLE_Q3}", size="sm")
|
| 820 |
+
with gr.Row():
|
| 821 |
+
sample4 = gr.Button(f"π§ {SAMPLE_Q4}", size="sm")
|
| 822 |
+
sample5 = gr.Button(f"π³ {SAMPLE_Q5}", size="sm")
|
| 823 |
+
sample6 = gr.Button(f"π§ {SAMPLE_Q6}", size="sm")
|
| 824 |
+
sample7 = gr.Button(f"π {SAMPLE_Q7}", size="sm")
|
| 825 |
+
|
| 826 |
+
msg_input = gr.Textbox(
|
| 827 |
+
placeholder="Ask a question about your PDFs...",
|
| 828 |
+
label="Ask about your PDFs",
|
| 829 |
+
show_label=False
|
| 830 |
+
)
|
| 831 |
+
|
| 832 |
+
ai_status = gr.Textbox(
|
| 833 |
+
label="π AI Processing Progress",
|
| 834 |
+
value="π¬ Ready for your question",
|
| 835 |
+
interactive=False,
|
| 836 |
+
placeholder="AI processing status with progress tracking..."
|
| 837 |
+
)
|
| 838 |
+
|
| 839 |
+
with gr.Row():
|
| 840 |
+
submit_btn = gr.Button("Send", variant="primary", scale=1, interactive=False)
|
| 841 |
+
clear_btn = gr.Button("Clear", scale=1, interactive=False)
|
| 842 |
+
|
| 843 |
+
# Event handlers
|
| 844 |
+
file_upload.upload(
|
| 845 |
+
fn=add_pdf,
|
| 846 |
+
inputs=[file_upload],
|
| 847 |
+
outputs=[pdf_dropdown, upload_status, progress_bar, embedded_files]
|
| 848 |
+
)
|
| 849 |
+
delete_btn.click(
|
| 850 |
+
fn=delete_pdf_ui,
|
| 851 |
+
inputs=[pdf_dropdown],
|
| 852 |
+
outputs=[pdf_dropdown, embedded_files, delete_progress_bar]
|
| 853 |
+
)
|
| 854 |
+
delete_all_btn.click(
|
| 855 |
+
fn=show_delete_all_warning,
|
| 856 |
+
inputs=[],
|
| 857 |
+
outputs=[delete_all_warning, confirm_delete_all_btn]
|
| 858 |
+
)
|
| 859 |
+
# Update Delete All button state when files change
|
| 860 |
+
demo.load(fn=toggle_delete_all_btn, outputs=[delete_all_btn])
|
| 861 |
+
pdf_dropdown.change(fn=toggle_delete_all_btn, outputs=[delete_all_btn])
|
| 862 |
+
embedded_files.change(fn=toggle_delete_all_btn, outputs=[delete_all_btn])
|
| 863 |
+
confirm_delete_all_btn.click(
|
| 864 |
+
fn=delete_all_files,
|
| 865 |
+
inputs=[],
|
| 866 |
+
outputs=[pdf_dropdown, embedded_files, delete_progress_bar]
|
| 867 |
+
).then(
|
| 868 |
+
fn=hide_delete_all_warning,
|
| 869 |
+
inputs=[],
|
| 870 |
+
outputs=[delete_all_warning, confirm_delete_all_btn]
|
| 871 |
+
)
|
| 872 |
+
# Enable/disable delete button based on selection
|
| 873 |
+
def toggle_delete_btn(selected):
|
| 874 |
+
return gr.update(interactive=bool(selected))
|
| 875 |
+
pdf_dropdown.change(
|
| 876 |
+
fn=toggle_delete_btn,
|
| 877 |
+
inputs=[pdf_dropdown],
|
| 878 |
+
outputs=[delete_btn]
|
| 879 |
+
)
|
| 880 |
+
demo.load(fn=lambda: "\n".join(get_pdf_list()), outputs=[embedded_files])
|
| 881 |
+
# Ensure embedded_files is updated on app start
|
| 882 |
+
demo.load(fn=lambda: "\n".join(get_pdf_list()), outputs=[embedded_files])
|
| 883 |
+
|
| 884 |
+
|
| 885 |
+
# Sample question handlers
|
| 886 |
+
sample_buttons = [sample1, sample2, sample3, sample4, sample5, sample6, sample7]
|
| 887 |
+
sample_questions = [SAMPLE_Q1, SAMPLE_Q2, SAMPLE_Q3, SAMPLE_Q4, SAMPLE_Q5, SAMPLE_Q6, SAMPLE_Q7]
|
| 888 |
+
for btn, question in zip(sample_buttons, sample_questions):
|
| 889 |
+
btn.click(fn=lambda q=question: q, outputs=[msg_input])
|
| 890 |
+
|
| 891 |
+
msg_input.submit(
|
| 892 |
+
fn=chat_response,
|
| 893 |
+
inputs=[msg_input, chatbot],
|
| 894 |
+
outputs=[chatbot, msg_input, ai_status]
|
| 895 |
+
)
|
| 896 |
+
submit_btn.click(
|
| 897 |
+
fn=chat_response,
|
| 898 |
+
inputs=[msg_input, chatbot],
|
| 899 |
+
outputs=[chatbot, msg_input, ai_status]
|
| 900 |
+
)
|
| 901 |
+
# Enable/disable send button based on input
|
| 902 |
+
def toggle_send_btn(text):
|
| 903 |
+
return gr.update(interactive=bool(text and text.strip()))
|
| 904 |
+
msg_input.change(
|
| 905 |
+
fn=toggle_send_btn,
|
| 906 |
+
inputs=[msg_input],
|
| 907 |
+
outputs=[submit_btn]
|
| 908 |
+
)
|
| 909 |
+
|
| 910 |
+
clear_btn.click(
|
| 911 |
+
fn=clear_chat_and_memory,
|
| 912 |
+
outputs=[chatbot, msg_input, ai_status]
|
| 913 |
+
)
|
| 914 |
+
# Enable/disable clear button based on input or chat
|
| 915 |
+
def toggle_clear_btn(text, chat):
|
| 916 |
+
return gr.update(interactive=bool((text and text.strip()) or (chat and len(chat) > 0)))
|
| 917 |
+
msg_input.change(
|
| 918 |
+
fn=lambda text: toggle_clear_btn(text, chatbot.value if hasattr(chatbot, 'value') else []),
|
| 919 |
+
inputs=[msg_input],
|
| 920 |
+
outputs=[clear_btn]
|
| 921 |
+
)
|
| 922 |
+
chatbot.change(
|
| 923 |
+
fn=lambda chat: toggle_clear_btn(msg_input.value if hasattr(msg_input, 'value') else '', chat),
|
| 924 |
+
inputs=[chatbot],
|
| 925 |
+
outputs=[clear_btn]
|
| 926 |
+
)
|
| 927 |
+
|
| 928 |
+
demo.load(fn=make_pdf_dropdown, outputs=[pdf_dropdown])
|
| 929 |
+
|
| 930 |
+
# ============================================================================
|
| 931 |
+
# LAUNCH application
|
| 932 |
+
# ============================================================================
|
| 933 |
+
|
| 934 |
+
if IS_HF_SPACE:
|
| 935 |
+
try:
|
| 936 |
+
demo.launch(
|
| 937 |
+
server_name=CONFIG["server_host"],
|
| 938 |
+
server_port=CONFIG["server_port"],
|
| 939 |
+
share=True,
|
| 940 |
+
show_error=True,
|
| 941 |
+
quiet=False
|
| 942 |
+
)
|
| 943 |
+
except Exception as launch_error:
|
| 944 |
+
print(f"Launch error: {launch_error}")
|
| 945 |
+
demo.launch(server_name=CONFIG["server_host"], server_port=CONFIG["server_port"])
|
| 946 |
+
else:
|
| 947 |
+
app_with_gradio = gr.mount_gradio_app(app, demo, path="/")
|
| 948 |
+
|
| 949 |
+
if __name__ == "__main__":
|
| 950 |
+
import uvicorn
|
| 951 |
+
import webbrowser
|
| 952 |
+
from threading import Timer
|
| 953 |
+
|
| 954 |
+
Timer(3, lambda: webbrowser.open(f"http://127.0.0.1:{CONFIG['server_port']}")).start()
|
| 955 |
+
|
| 956 |
+
print("Starting server... Browser will open automatically in 3 seconds.")
|
| 957 |
+
uvicorn.run(app_with_gradio, host=CONFIG["server_host"], port=CONFIG["server_port"])
|
config.yaml
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
title: Agentic RAG 3
|
| 2 |
+
emoji: π€
|
| 3 |
+
colorFrom: blue
|
| 4 |
+
colorTo: purple
|
| 5 |
+
sdk: docker
|
| 6 |
+
app_file: app.py
|
| 7 |
+
pinned: false
|
| 8 |
+
license: mit
|
| 9 |
+
short_description: RAG Agent with document analysis
|
helpers_HF.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
HuggingFace Space specific helpers for LLM operations.
|
| 3 |
+
Contains: HF InferenceClient initialization and response generation.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
from typing import Optional
|
| 8 |
+
from huggingface_hub import InferenceClient
|
| 9 |
+
|
| 10 |
+
from helpers_SHARED import CONFIG, IS_HF_SPACE
|
| 11 |
+
|
| 12 |
+
# ============================================================================
|
| 13 |
+
# HUGGINGFACE LLM INITIALIZATION
|
| 14 |
+
# ============================================================================
|
| 15 |
+
|
| 16 |
+
hf_client = None
|
| 17 |
+
LLM_NAME = None
|
| 18 |
+
|
| 19 |
+
def init_hf_llm(model_name=None):
|
| 20 |
+
"""Initialize HuggingFace InferenceClient for cloud LLM. Optionally specify model_name."""
|
| 21 |
+
global hf_client, LLM_NAME
|
| 22 |
+
|
| 23 |
+
if not IS_HF_SPACE:
|
| 24 |
+
print("βΉοΈ Not running on HF Space, skipping HF client initialization")
|
| 25 |
+
return None, None
|
| 26 |
+
|
| 27 |
+
# Use provided model_name or fallback to config
|
| 28 |
+
if model_name is not None:
|
| 29 |
+
LLM_NAME = model_name.split("/")[-1]
|
| 30 |
+
# Patch CONFIG for downstream use
|
| 31 |
+
CONFIG["hf_model"] = model_name
|
| 32 |
+
else:
|
| 33 |
+
LLM_NAME = CONFIG["hf_model"].split("/")[-1]
|
| 34 |
+
try:
|
| 35 |
+
hf_client = InferenceClient(token=os.getenv("HF_token"))
|
| 36 |
+
print(f"β HuggingFace InferenceClient initialized successfully for {LLM_NAME}")
|
| 37 |
+
return hf_client, LLM_NAME
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"β Warning: HuggingFace InferenceClient not available: {e}")
|
| 40 |
+
hf_client = None
|
| 41 |
+
return None, LLM_NAME
|
| 42 |
+
|
| 43 |
+
# ============================================================================
|
| 44 |
+
# HUGGINGFACE LLM RESPONSE GENERATION
|
| 45 |
+
# ============================================================================
|
| 46 |
+
|
| 47 |
+
def hf_generate_chat_response(prompt: str, hf_client_instance=None) -> Optional[str]:
|
| 48 |
+
"""Generate a chat response using HuggingFace InferenceClient.
|
| 49 |
+
|
| 50 |
+
Args:
|
| 51 |
+
prompt: The prompt to send to the model
|
| 52 |
+
hf_client_instance: Optional HF client instance, uses global if not provided
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
Generated response string or None if failed
|
| 56 |
+
"""
|
| 57 |
+
client = hf_client_instance or hf_client
|
| 58 |
+
|
| 59 |
+
if client is None:
|
| 60 |
+
print("β HF client not available")
|
| 61 |
+
return None
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
print(f"π§ Generating response with {LLM_NAME}...")
|
| 65 |
+
response = client.chat_completion(
|
| 66 |
+
messages=[{"role": "user", "content": prompt}],
|
| 67 |
+
model=CONFIG["hf_model"],
|
| 68 |
+
max_tokens=CONFIG["max_tokens"],
|
| 69 |
+
temperature=CONFIG["temperature"]
|
| 70 |
+
)
|
| 71 |
+
result = response.choices[0].message.content
|
| 72 |
+
if result:
|
| 73 |
+
result = result.strip()
|
| 74 |
+
print(f"β LLM response: {result[:100]}...")
|
| 75 |
+
return result
|
| 76 |
+
else:
|
| 77 |
+
print(f"β οΈ Empty LLM response")
|
| 78 |
+
return None
|
| 79 |
+
except Exception as hf_error:
|
| 80 |
+
print(f"β HF chat_completion error: {type(hf_error).__name__}: {str(hf_error)}")
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
+
def hf_generate_text_response(prompt: str, context: str, hf_client_instance=None) -> str:
|
| 84 |
+
"""Generate a text response using HuggingFace text_generation API.
|
| 85 |
+
|
| 86 |
+
Used as fallback for simpler generation tasks.
|
| 87 |
+
|
| 88 |
+
Args:
|
| 89 |
+
prompt: The full prompt to send
|
| 90 |
+
context: Document context for fallback response
|
| 91 |
+
hf_client_instance: Optional HF client instance
|
| 92 |
+
|
| 93 |
+
Returns:
|
| 94 |
+
Generated response string
|
| 95 |
+
"""
|
| 96 |
+
client = hf_client_instance or hf_client
|
| 97 |
+
|
| 98 |
+
if client is None:
|
| 99 |
+
print("β HF client not available")
|
| 100 |
+
return f"I found relevant information in your documents:\n\n{context[:CONFIG['search_content_limit']]}..."
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
print(f"Generating response with {LLM_NAME}...")
|
| 104 |
+
response = client.text_generation(
|
| 105 |
+
prompt,
|
| 106 |
+
model=CONFIG["hf_model"],
|
| 107 |
+
max_new_tokens=CONFIG["max_tokens"],
|
| 108 |
+
temperature=CONFIG["temperature_fallback"],
|
| 109 |
+
return_full_text=False
|
| 110 |
+
)
|
| 111 |
+
print(f"β Success! Response generated.")
|
| 112 |
+
return response
|
| 113 |
+
except Exception as hf_error:
|
| 114 |
+
print(f"β HF API error: {str(hf_error)}")
|
| 115 |
+
return f"I found relevant information in your documents:\n\n{context[:CONFIG['search_content_limit']]}..."
|
| 116 |
+
|
| 117 |
+
def get_hf_client():
|
| 118 |
+
"""Get the HF client instance."""
|
| 119 |
+
return hf_client
|
| 120 |
+
|
| 121 |
+
def get_hf_llm_name():
|
| 122 |
+
"""Get the HF LLM name."""
|
| 123 |
+
return LLM_NAME
|
helpers_LOCAL.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Utility: Get installed Ollama LLMs as a list (for UI)
|
| 2 |
+
def get_installed_llms():
|
| 3 |
+
"""Returns a list of locally installed Ollama LLMs (model names)."""
|
| 4 |
+
import subprocess
|
| 5 |
+
try:
|
| 6 |
+
result = subprocess.run(["ollama", "list"], capture_output=True, text=True)
|
| 7 |
+
lines = result.stdout.splitlines()
|
| 8 |
+
models = []
|
| 9 |
+
for line in lines:
|
| 10 |
+
if line.strip() and not line.startswith("NAME"):
|
| 11 |
+
name = line.split()[0]
|
| 12 |
+
models.append(name)
|
| 13 |
+
return models
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"Error listing local LLMs: {e}")
|
| 16 |
+
return []
|
| 17 |
+
|
| 18 |
+
# Utility: Display installed Ollama LLMs in terminal (for CLI use)
|
| 19 |
+
def display_installed_llms():
|
| 20 |
+
"""Prints a list of locally installed Ollama LLMs to the terminal."""
|
| 21 |
+
models = get_installed_llms()
|
| 22 |
+
if models:
|
| 23 |
+
print("Available local LLMs:")
|
| 24 |
+
for m in models:
|
| 25 |
+
print(f"- {m}")
|
| 26 |
+
else:
|
| 27 |
+
print("No local LLMs found.")
|
| 28 |
+
"""
|
| 29 |
+
Local environment (Ollama) specific helpers for LLM operations.
|
| 30 |
+
Contains: Ollama/ChatOllama initialization, agent creation, and response generation.
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
from helpers_SHARED import CONFIG, IS_HF_SPACE, AGENT_TOOLS
|
| 34 |
+
|
| 35 |
+
# ============================================================================
|
| 36 |
+
# OLLAMA LLM INITIALIZATION
|
| 37 |
+
# ============================================================================
|
| 38 |
+
|
| 39 |
+
ollama_llm = None
|
| 40 |
+
LLM_NAME = None
|
| 41 |
+
|
| 42 |
+
def init_ollama_llm(model_name=None):
|
| 43 |
+
"""Initialize Ollama (ChatOllama) for local LLM. Accepts a model name for dynamic selection."""
|
| 44 |
+
global ollama_llm, LLM_NAME
|
| 45 |
+
|
| 46 |
+
if IS_HF_SPACE:
|
| 47 |
+
print("βΉοΈ Running on HF Space, skipping Ollama initialization")
|
| 48 |
+
return None, None
|
| 49 |
+
|
| 50 |
+
from langchain_ollama import ChatOllama
|
| 51 |
+
|
| 52 |
+
if model_name is None:
|
| 53 |
+
model_name = CONFIG["ollama_model"]
|
| 54 |
+
LLM_NAME = model_name
|
| 55 |
+
try:
|
| 56 |
+
ollama_llm = ChatOllama(model=model_name, base_url=CONFIG["ollama_base_url"])
|
| 57 |
+
print(f"β Ollama (ChatOllama) initialized successfully with {LLM_NAME}")
|
| 58 |
+
return ollama_llm, LLM_NAME
|
| 59 |
+
except Exception as e:
|
| 60 |
+
print(f"β Warning: Ollama not available: {e}")
|
| 61 |
+
ollama_llm = None
|
| 62 |
+
return None, LLM_NAME
|
| 63 |
+
|
| 64 |
+
# ============================================================================
|
| 65 |
+
# LANGCHAIN AGENT (LOCAL ONLY)
|
| 66 |
+
# ============================================================================
|
| 67 |
+
|
| 68 |
+
agent_executor = None
|
| 69 |
+
|
| 70 |
+
def create_langchain_agent():
|
| 71 |
+
"""Create a LangGraph ReAct agent with the available tools.
|
| 72 |
+
|
| 73 |
+
Only works in local environment with Ollama.
|
| 74 |
+
Returns None on HF Spaces.
|
| 75 |
+
"""
|
| 76 |
+
global agent_executor, ollama_llm
|
| 77 |
+
|
| 78 |
+
if IS_HF_SPACE:
|
| 79 |
+
print("βΉοΈ HF Space detected - using manual tool routing (HF InferenceClient doesn't support LangChain agents)")
|
| 80 |
+
return None
|
| 81 |
+
|
| 82 |
+
if ollama_llm is None:
|
| 83 |
+
print("β Ollama LLM not initialized, cannot create agent")
|
| 84 |
+
return None
|
| 85 |
+
|
| 86 |
+
from langgraph.prebuilt import create_react_agent # type: ignore
|
| 87 |
+
|
| 88 |
+
try:
|
| 89 |
+
agent_executor = create_react_agent(
|
| 90 |
+
model=ollama_llm,
|
| 91 |
+
tools=AGENT_TOOLS,
|
| 92 |
+
)
|
| 93 |
+
print("β
LangGraph ReAct Agent created successfully with Ollama")
|
| 94 |
+
return agent_executor
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print(f"β Failed to create LangGraph agent: {e}")
|
| 97 |
+
return None
|
| 98 |
+
|
| 99 |
+
# ============================================================================
|
| 100 |
+
# OLLAMA RESPONSE GENERATION
|
| 101 |
+
# ============================================================================
|
| 102 |
+
|
| 103 |
+
def ollama_generate_response(prompt: str, ollama_instance=None) -> str:
|
| 104 |
+
"""Generate a response using Ollama.
|
| 105 |
+
|
| 106 |
+
Args:
|
| 107 |
+
prompt: The prompt to send to the model
|
| 108 |
+
ollama_instance: Optional Ollama instance, uses global if not provided
|
| 109 |
+
|
| 110 |
+
Returns:
|
| 111 |
+
Generated response string or None if failed
|
| 112 |
+
"""
|
| 113 |
+
llm = ollama_instance or ollama_llm
|
| 114 |
+
|
| 115 |
+
if llm is None:
|
| 116 |
+
print("β Ollama not available")
|
| 117 |
+
return None
|
| 118 |
+
|
| 119 |
+
try:
|
| 120 |
+
print(f"π§ Generating response with Ollama ({LLM_NAME})...")
|
| 121 |
+
response = llm.invoke(prompt)
|
| 122 |
+
print(f"β Agent response generated.")
|
| 123 |
+
return response
|
| 124 |
+
except Exception as ollama_error:
|
| 125 |
+
print(f"β Ollama error: {str(ollama_error)}")
|
| 126 |
+
return None
|
| 127 |
+
|
| 128 |
+
def run_agent(query: str) -> str:
|
| 129 |
+
"""Run the LangGraph agent on a query.
|
| 130 |
+
|
| 131 |
+
Args:
|
| 132 |
+
query: User query to process
|
| 133 |
+
|
| 134 |
+
Returns:
|
| 135 |
+
Agent response string or None if agent unavailable
|
| 136 |
+
"""
|
| 137 |
+
global agent_executor
|
| 138 |
+
|
| 139 |
+
if agent_executor is None:
|
| 140 |
+
return None
|
| 141 |
+
|
| 142 |
+
try:
|
| 143 |
+
print(f"π€ Using LangGraph Agent to process: {query}")
|
| 144 |
+
result = agent_executor.invoke({"messages": [{"role": "user", "content": query}]})
|
| 145 |
+
# Extract the last AI message
|
| 146 |
+
messages = result.get("messages", [])
|
| 147 |
+
for msg in reversed(messages):
|
| 148 |
+
if hasattr(msg, 'content') and msg.type == "ai":
|
| 149 |
+
return msg.content
|
| 150 |
+
return "No response from agent"
|
| 151 |
+
except Exception as e:
|
| 152 |
+
print(f"β Agent execution failed: {e}, falling back to manual routing")
|
| 153 |
+
return None
|
| 154 |
+
|
| 155 |
+
def get_ollama_llm():
|
| 156 |
+
"""Get the Ollama LLM instance."""
|
| 157 |
+
return ollama_llm
|
| 158 |
+
|
| 159 |
+
def get_local_llm_name():
|
| 160 |
+
"""Get the local LLM name."""
|
| 161 |
+
return LLM_NAME
|
| 162 |
+
|
| 163 |
+
def get_agent_executor():
|
| 164 |
+
"""Get the agent executor instance."""
|
| 165 |
+
return agent_executor
|
helpers_SHARED.py
ADDED
|
@@ -0,0 +1,407 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Shared helper functions used by both HuggingFace Space and Local environments.
|
| 3 |
+
Contains: configuration, memory management, vectorstore operations, PDF helpers, and UI utilities.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
from typing import List, Optional
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
from collections import deque
|
| 10 |
+
import gradio as gr
|
| 11 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 12 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 13 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 14 |
+
from langchain_community.vectorstores import FAISS
|
| 15 |
+
from langchain_core.documents import Document
|
| 16 |
+
from langchain_core.tools import tool
|
| 17 |
+
|
| 18 |
+
# ============================================================================
|
| 19 |
+
# CONFIGURATION - All settings in one place
|
| 20 |
+
# ============================================================================
|
| 21 |
+
|
| 22 |
+
def setup():
|
| 23 |
+
"""
|
| 24 |
+
Central configuration for the RAG Agent application.
|
| 25 |
+
Modify these values to customize the application behavior.
|
| 26 |
+
Returns a config dictionary with all settings.
|
| 27 |
+
"""
|
| 28 |
+
return {
|
| 29 |
+
# Model Configuration
|
| 30 |
+
"embedding_model": "sentence-transformers/all-MiniLM-L6-v2",
|
| 31 |
+
"ollama_model": "qwen2m:latest", # Local Ollama model
|
| 32 |
+
"hf_model": "Llama-3.2-3B-Instruct", # HuggingFace cloud model
|
| 33 |
+
"ollama_base_url": "http://localhost:11434",
|
| 34 |
+
|
| 35 |
+
# Text Splitting Configuration
|
| 36 |
+
"chunk_size": 1000,
|
| 37 |
+
"chunk_overlap": 200,
|
| 38 |
+
|
| 39 |
+
# Search Configuration
|
| 40 |
+
"search_k": 5, # Number of documents to retrieve
|
| 41 |
+
"search_content_limit": 500, # Max chars to show per chunk
|
| 42 |
+
|
| 43 |
+
# LLM Generation Configuration
|
| 44 |
+
"max_tokens": 512,
|
| 45 |
+
"temperature": 0.1, # Lower = more deterministic
|
| 46 |
+
"temperature_fallback": 0.7, # For text_generation fallback
|
| 47 |
+
|
| 48 |
+
# Memory Configuration
|
| 49 |
+
"max_memory_turns": 50, # Max conversation turns to store
|
| 50 |
+
"memory_context_limit": 500, # Max chars per memory entry
|
| 51 |
+
|
| 52 |
+
# Server Configuration
|
| 53 |
+
"server_port": 7860,
|
| 54 |
+
"server_host": "0.0.0.0",
|
| 55 |
+
|
| 56 |
+
# UI Configuration
|
| 57 |
+
"chatbot_height": 600,
|
| 58 |
+
"progress_bar_length": 20,
|
| 59 |
+
"chat_progress_bar_length": 15,
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
# Initialize configuration
|
| 63 |
+
CONFIG = setup()
|
| 64 |
+
|
| 65 |
+
# ============================================================================
|
| 66 |
+
# ENVIRONMENT DETECTION
|
| 67 |
+
# ============================================================================
|
| 68 |
+
|
| 69 |
+
IS_HF_SPACE = os.getenv("SPACE_ID") is not None
|
| 70 |
+
|
| 71 |
+
# Directories - use persistent storage on HF Spaces if available
|
| 72 |
+
DATA_DIR = "/data" if (IS_HF_SPACE and os.path.exists("/data")) else "data"
|
| 73 |
+
EMBEDDINGS_DIR = os.path.join(DATA_DIR, "embeddings")
|
| 74 |
+
|
| 75 |
+
# Check storage persistence status
|
| 76 |
+
HAS_PERSISTENT_STORAGE = IS_HF_SPACE and os.path.exists("/data")
|
| 77 |
+
STORAGE_WARNING = "" if not IS_HF_SPACE else (
|
| 78 |
+
"β
Persistent storage enabled - files will survive restarts" if HAS_PERSISTENT_STORAGE else
|
| 79 |
+
"β οΈ Temporary storage - uploaded files will be lost when Space restarts"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Initialize embeddings (shared across environments)
|
| 83 |
+
embeddings = HuggingFaceEmbeddings(model_name=CONFIG["embedding_model"])
|
| 84 |
+
|
| 85 |
+
# Global vectorstore (will be set by build_vectorstore)
|
| 86 |
+
vs = None
|
| 87 |
+
|
| 88 |
+
# ============================================================================
|
| 89 |
+
# CONVERSATION MEMORY
|
| 90 |
+
# ============================================================================
|
| 91 |
+
|
| 92 |
+
conversation_memory: deque = deque(maxlen=CONFIG["max_memory_turns"])
|
| 93 |
+
|
| 94 |
+
def add_to_memory(role: str, content: str):
|
| 95 |
+
"""Add a message to conversation memory with timestamp."""
|
| 96 |
+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 97 |
+
conversation_memory.append({
|
| 98 |
+
"timestamp": timestamp,
|
| 99 |
+
"role": role,
|
| 100 |
+
"content": content
|
| 101 |
+
})
|
| 102 |
+
print(f"πΎ Memory updated: {role} message added (total: {len(conversation_memory)} turns)")
|
| 103 |
+
|
| 104 |
+
def get_memory_context(last_n: int = 10) -> str:
|
| 105 |
+
"""Get the last N conversation turns as context."""
|
| 106 |
+
if not conversation_memory:
|
| 107 |
+
return "No previous conversation history."
|
| 108 |
+
|
| 109 |
+
recent = list(conversation_memory)[-last_n:]
|
| 110 |
+
context_parts = []
|
| 111 |
+
for msg in recent:
|
| 112 |
+
role_emoji = "π€" if msg["role"] == "user" else "π€"
|
| 113 |
+
context_parts.append(f"[{msg['timestamp']}] {role_emoji} {msg['role'].upper()}: {msg['content'][:CONFIG['memory_context_limit']]}")
|
| 114 |
+
|
| 115 |
+
return "\n\n".join(context_parts)
|
| 116 |
+
|
| 117 |
+
def search_memory(query: str) -> str:
|
| 118 |
+
"""Search conversation memory for relevant past discussions."""
|
| 119 |
+
if not conversation_memory:
|
| 120 |
+
return "No conversation history to search."
|
| 121 |
+
|
| 122 |
+
query_lower = query.lower()
|
| 123 |
+
matches = []
|
| 124 |
+
|
| 125 |
+
for msg in conversation_memory:
|
| 126 |
+
content_lower = msg["content"].lower()
|
| 127 |
+
# Simple keyword matching
|
| 128 |
+
if any(word in content_lower for word in query_lower.split()):
|
| 129 |
+
role_emoji = "π€" if msg["role"] == "user" else "π€"
|
| 130 |
+
matches.append(f"[{msg['timestamp']}] {role_emoji} {msg['role'].upper()}: {msg['content'][:CONFIG['memory_context_limit'] - 200]}...")
|
| 131 |
+
|
| 132 |
+
if matches:
|
| 133 |
+
return f"Found {len(matches)} relevant conversation(s):\n\n" + "\n\n---\n\n".join(matches[:5])
|
| 134 |
+
else:
|
| 135 |
+
return f"No conversations found matching '{query}'."
|
| 136 |
+
|
| 137 |
+
def clear_memory():
|
| 138 |
+
"""Clear all conversation memory."""
|
| 139 |
+
conversation_memory.clear()
|
| 140 |
+
print("π§Ή Conversation memory cleared")
|
| 141 |
+
|
| 142 |
+
# ============================================================================
|
| 143 |
+
# UTILITY FUNCTIONS
|
| 144 |
+
# ============================================================================
|
| 145 |
+
|
| 146 |
+
def get_timestamp() -> str:
|
| 147 |
+
"""Get current timestamp in HH:MM:SS format."""
|
| 148 |
+
return datetime.now().strftime("%H:%M:%S")
|
| 149 |
+
|
| 150 |
+
def create_elapsed_timer(start_time: datetime):
|
| 151 |
+
"""Create an elapsed time function for tracking duration."""
|
| 152 |
+
def get_elapsed() -> str:
|
| 153 |
+
elapsed = datetime.now() - start_time
|
| 154 |
+
return f"β±οΈ {elapsed.total_seconds():.1f}s"
|
| 155 |
+
return get_elapsed
|
| 156 |
+
|
| 157 |
+
def format_progress_bar(elapsed_time: str, percentage: int, message: str, bar_length: int = 20) -> str:
|
| 158 |
+
"""Format progress with visual progress bar using Unicode blocks."""
|
| 159 |
+
filled_length = int(bar_length * percentage / 100)
|
| 160 |
+
bar = 'β' * filled_length + 'β' * (bar_length - filled_length)
|
| 161 |
+
return f"{elapsed_time} | [{percentage:3d}%] {bar} {message}"
|
| 162 |
+
|
| 163 |
+
# =========================================================================
|
| 164 |
+
# FLOATING PROGRESS BAR HTML/JS (for Gradio UI)
|
| 165 |
+
# =========================================================================
|
| 166 |
+
def floating_progress_bar_html():
|
| 167 |
+
"""Return HTML+JS for a floating, borderless, fit-content progress bar overlay."""
|
| 168 |
+
return '''
|
| 169 |
+
<div id="floating-progress" style="
|
| 170 |
+
display: none;
|
| 171 |
+
position: fixed;
|
| 172 |
+
top: 20px; left: 50%; transform: translateX(-50%);
|
| 173 |
+
background: #222; color: #fff; padding: 8px 0; border-radius: 8px; z-index: 9999;
|
| 174 |
+
font-family: monospace; font-size: 1.2em; box-shadow: none; border: none;
|
| 175 |
+
width: fit-content; min-width: 0; max-width: none;
|
| 176 |
+
">
|
| 177 |
+
[....................................................................................................]
|
| 178 |
+
</div>
|
| 179 |
+
<script>
|
| 180 |
+
function showProgressBar(barText) {
|
| 181 |
+
var el = document.getElementById('floating-progress');
|
| 182 |
+
el.innerText = barText;
|
| 183 |
+
el.style.display = 'block';
|
| 184 |
+
}
|
| 185 |
+
function hideProgressBar() {
|
| 186 |
+
document.getElementById('floating-progress').style.display = 'none';
|
| 187 |
+
}
|
| 188 |
+
// Example usage (remove or replace with Python/Gradio event):
|
| 189 |
+
// showProgressBar('[|||||||||||||.............]');
|
| 190 |
+
// setTimeout(hideProgressBar, 2000);
|
| 191 |
+
</script>
|
| 192 |
+
'''
|
| 193 |
+
|
| 194 |
+
# ============================================================================
|
| 195 |
+
# PDF HELPERS
|
| 196 |
+
# ============================================================================
|
| 197 |
+
|
| 198 |
+
def get_pdf_list() -> List[str]:
|
| 199 |
+
"""Get list of PDF files in data folder."""
|
| 200 |
+
return [f for f in os.listdir(DATA_DIR) if f.endswith(".pdf")]
|
| 201 |
+
|
| 202 |
+
def get_pdf_list_ui() -> List[str]:
|
| 203 |
+
"""Get PDF list for UI dropdown (with error handling)."""
|
| 204 |
+
try:
|
| 205 |
+
return get_pdf_list()
|
| 206 |
+
except Exception as e:
|
| 207 |
+
print(f"Error getting PDF list: {e}")
|
| 208 |
+
return []
|
| 209 |
+
|
| 210 |
+
def make_pdf_dropdown(value=None):
|
| 211 |
+
"""Create a PDF dropdown with current file list."""
|
| 212 |
+
return gr.Dropdown(choices=get_pdf_list_ui(), value=value)
|
| 213 |
+
|
| 214 |
+
# ============================================================================
|
| 215 |
+
# VECTORSTORE OPERATIONS
|
| 216 |
+
# ============================================================================
|
| 217 |
+
|
| 218 |
+
def build_vectorstore(force_rebuild: bool = False) -> Optional[FAISS]:
|
| 219 |
+
"""Build or load FAISS vectorstore from PDFs.
|
| 220 |
+
|
| 221 |
+
Args:
|
| 222 |
+
force_rebuild: If True, rebuild from scratch even if existing vectorstore found
|
| 223 |
+
"""
|
| 224 |
+
global vs
|
| 225 |
+
|
| 226 |
+
# Check if we should load existing vectorstore
|
| 227 |
+
if not force_rebuild and os.path.exists(os.path.join(EMBEDDINGS_DIR, "index.faiss")):
|
| 228 |
+
try:
|
| 229 |
+
print("π Loading existing vectorstore...")
|
| 230 |
+
vectorstore = FAISS.load_local(EMBEDDINGS_DIR, embeddings, allow_dangerous_deserialization=True)
|
| 231 |
+
print("β
Vectorstore loaded successfully")
|
| 232 |
+
vs = vectorstore
|
| 233 |
+
return vectorstore
|
| 234 |
+
except Exception as e:
|
| 235 |
+
print(f"β Error loading vectorstore: {e}, rebuilding...")
|
| 236 |
+
|
| 237 |
+
# Build new vectorstore from PDFs
|
| 238 |
+
pdf_files = get_pdf_list()
|
| 239 |
+
if not pdf_files:
|
| 240 |
+
print("No PDF files found to build embeddings")
|
| 241 |
+
vs = None
|
| 242 |
+
return None
|
| 243 |
+
|
| 244 |
+
print(f"π¨ Building vectorstore from {len(pdf_files)} PDF(s): {pdf_files}")
|
| 245 |
+
|
| 246 |
+
docs: List[Document] = []
|
| 247 |
+
for filename in pdf_files:
|
| 248 |
+
try:
|
| 249 |
+
filepath = os.path.join(DATA_DIR, filename)
|
| 250 |
+
print(f"π Loading {filename}...")
|
| 251 |
+
loader = PyPDFLoader(filepath)
|
| 252 |
+
file_docs = loader.load()
|
| 253 |
+
docs.extend(file_docs)
|
| 254 |
+
print(f"β
Loaded {len(file_docs)} pages from {filename}")
|
| 255 |
+
except Exception as e:
|
| 256 |
+
print(f"β Error loading {filename}: {e}")
|
| 257 |
+
continue
|
| 258 |
+
|
| 259 |
+
if not docs:
|
| 260 |
+
print("β οΈ No documents could be loaded")
|
| 261 |
+
vs = None
|
| 262 |
+
return None
|
| 263 |
+
|
| 264 |
+
print(f"βοΈ Splitting {len(docs)} pages into chunks...")
|
| 265 |
+
splitter = RecursiveCharacterTextSplitter(
|
| 266 |
+
chunk_size=CONFIG["chunk_size"],
|
| 267 |
+
chunk_overlap=CONFIG["chunk_overlap"]
|
| 268 |
+
)
|
| 269 |
+
splits = splitter.split_documents(docs)
|
| 270 |
+
print(f"π§© Created {len(splits)} text chunks")
|
| 271 |
+
|
| 272 |
+
print("π€ Creating FAISS embeddings...")
|
| 273 |
+
try:
|
| 274 |
+
vectorstore = FAISS.from_documents(splits, embeddings)
|
| 275 |
+
|
| 276 |
+
print(f"πΎ Saving vectorstore to {EMBEDDINGS_DIR}...")
|
| 277 |
+
vectorstore.save_local(EMBEDDINGS_DIR)
|
| 278 |
+
|
| 279 |
+
vs = vectorstore
|
| 280 |
+
print("β
Vectorstore built and saved successfully")
|
| 281 |
+
return vectorstore
|
| 282 |
+
|
| 283 |
+
except Exception as e:
|
| 284 |
+
print(f"β Failed to build vectorstore: {e}")
|
| 285 |
+
import traceback
|
| 286 |
+
traceback.print_exc()
|
| 287 |
+
vs = None
|
| 288 |
+
return None
|
| 289 |
+
|
| 290 |
+
def get_vectorstore():
|
| 291 |
+
"""Get the current vectorstore instance."""
|
| 292 |
+
global vs
|
| 293 |
+
return vs
|
| 294 |
+
|
| 295 |
+
def set_vectorstore(vectorstore):
|
| 296 |
+
"""Set the vectorstore instance."""
|
| 297 |
+
global vs
|
| 298 |
+
vs = vectorstore
|
| 299 |
+
|
| 300 |
+
# ============================================================================
|
| 301 |
+
# RAG AGENT TOOLS (LangChain @tool decorator pattern)
|
| 302 |
+
# ============================================================================
|
| 303 |
+
|
| 304 |
+
@tool
|
| 305 |
+
def list_documents() -> str:
|
| 306 |
+
"""List all available PDF documents in the system. Use this tool when the user asks what documents are available, what files they have, or wants to see the document list."""
|
| 307 |
+
pdfs = get_pdf_list()
|
| 308 |
+
if pdfs:
|
| 309 |
+
return f"π Available documents: {', '.join(pdfs)}"
|
| 310 |
+
else:
|
| 311 |
+
return "π No documents are currently uploaded."
|
| 312 |
+
|
| 313 |
+
@tool
|
| 314 |
+
def count_documents() -> str:
|
| 315 |
+
"""Count the total number of uploaded PDF documents. Use this tool when the user asks how many documents they have or wants a document count."""
|
| 316 |
+
count = len(get_pdf_list())
|
| 317 |
+
return f"π Total documents: {count}"
|
| 318 |
+
|
| 319 |
+
@tool
|
| 320 |
+
def search_documents(query: str) -> str:
|
| 321 |
+
"""Search document content using RAG (Retrieval Augmented Generation). Use this tool to find information within the uploaded PDF documents based on a search query."""
|
| 322 |
+
global vs
|
| 323 |
+
|
| 324 |
+
# Check if we have any PDF files first
|
| 325 |
+
pdf_files = get_pdf_list()
|
| 326 |
+
if not pdf_files:
|
| 327 |
+
return "π No documents are currently uploaded. Please upload PDF files first."
|
| 328 |
+
|
| 329 |
+
# Force reload vectorstore from disk if files exist
|
| 330 |
+
print(f"π Checking vectorstore for {len(pdf_files)} PDF files...")
|
| 331 |
+
|
| 332 |
+
# Check if FAISS files exist on disk
|
| 333 |
+
faiss_path = os.path.join(EMBEDDINGS_DIR, "index.faiss")
|
| 334 |
+
pkl_path = os.path.join(EMBEDDINGS_DIR, "index.pkl")
|
| 335 |
+
|
| 336 |
+
if os.path.exists(faiss_path) and os.path.exists(pkl_path):
|
| 337 |
+
print(f"π Found vectorstore files, loading...")
|
| 338 |
+
try:
|
| 339 |
+
# Force reload from disk
|
| 340 |
+
vs = FAISS.load_local(EMBEDDINGS_DIR, embeddings, allow_dangerous_deserialization=True)
|
| 341 |
+
print(f"β
Vectorstore loaded successfully from disk")
|
| 342 |
+
except Exception as e:
|
| 343 |
+
print(f"β Error loading vectorstore: {e}")
|
| 344 |
+
vs = None
|
| 345 |
+
else:
|
| 346 |
+
print(f"π No vectorstore files found, attempting to build...")
|
| 347 |
+
vs = build_vectorstore()
|
| 348 |
+
|
| 349 |
+
if vs is None:
|
| 350 |
+
return f"π Found {len(pdf_files)} document(s) but search index could not be created. Please try re-uploading your files."
|
| 351 |
+
|
| 352 |
+
try:
|
| 353 |
+
# Extract key search terms from query (remove common words)
|
| 354 |
+
search_query = query
|
| 355 |
+
print(f"π Searching vectorstore for: {search_query}")
|
| 356 |
+
|
| 357 |
+
# Use similarity_search_with_score to filter by relevance
|
| 358 |
+
docs_with_scores = vs.similarity_search_with_score(search_query, k=CONFIG["search_k"])
|
| 359 |
+
|
| 360 |
+
if docs_with_scores:
|
| 361 |
+
# Filter by score (lower is better for L2 distance) - adjust threshold as needed
|
| 362 |
+
# Show more content from each chunk for better context
|
| 363 |
+
context_parts = []
|
| 364 |
+
for doc, score in docs_with_scores:
|
| 365 |
+
# Get source file from metadata
|
| 366 |
+
source = doc.metadata.get('source', 'Unknown').split('/')[-1]
|
| 367 |
+
page = doc.metadata.get('page', '?')
|
| 368 |
+
# Include score and source in debug output
|
| 369 |
+
print(f" π Score: {score:.3f} | Source: {source} pg{page} - {doc.page_content[:50]}...")
|
| 370 |
+
# Show more content with source info
|
| 371 |
+
context_parts.append(f"[Source: {source}, Page: {page}, Relevance: {score:.2f}]\n{doc.page_content[:CONFIG['search_content_limit']]}")
|
| 372 |
+
|
| 373 |
+
context = "\n\n---\n\n".join(context_parts)
|
| 374 |
+
print(f"β Found {len(docs_with_scores)} document chunks")
|
| 375 |
+
return f"π Search results for '{query}':\n\n{context}"
|
| 376 |
+
else:
|
| 377 |
+
print(f"β οΈ No relevant documents found for query: {query}")
|
| 378 |
+
return f"π No relevant information found for '{query}' in your {len(pdf_files)} document(s). Try different keywords or check if your documents contain relevant content."
|
| 379 |
+
|
| 380 |
+
except Exception as e:
|
| 381 |
+
error_msg = f"π Search error: {str(e)}. You have {len(pdf_files)} documents available."
|
| 382 |
+
print(f"β Search error: {str(e)}")
|
| 383 |
+
import traceback
|
| 384 |
+
traceback.print_exc()
|
| 385 |
+
return error_msg
|
| 386 |
+
|
| 387 |
+
@tool
|
| 388 |
+
def search_conversation_history(query: str) -> str:
|
| 389 |
+
"""Search through previous conversation history to find past discussions. Use this tool when the user asks about something they discussed before, wants to recall previous answers, or references past conversations."""
|
| 390 |
+
return search_memory(query)
|
| 391 |
+
|
| 392 |
+
@tool
|
| 393 |
+
def get_recent_conversation(turns: int = 5) -> str:
|
| 394 |
+
"""Get the most recent conversation turns. Use this tool when the user asks what they were discussing, wants a summary of recent chat, or needs context from earlier in the conversation."""
|
| 395 |
+
return get_memory_context(last_n=turns)
|
| 396 |
+
|
| 397 |
+
# List of all available tools
|
| 398 |
+
AGENT_TOOLS = [list_documents, count_documents, search_documents, search_conversation_history, get_recent_conversation]
|
| 399 |
+
|
| 400 |
+
# Sample question texts - Enhanced for agent capabilities
|
| 401 |
+
SAMPLE_Q1 = "How many documents are loaded? List their names and types?"
|
| 402 |
+
SAMPLE_Q2 = "Summarize the key points of each document in 5 bullet points."
|
| 403 |
+
SAMPLE_Q3 = "What is the attention mechanism? list the main topics."
|
| 404 |
+
SAMPLE_Q4 = "How can I cook chicken breast with Phillips air fryer recipes?"
|
| 405 |
+
SAMPLE_Q5 = "Summarize each document in max 10 bullet points."
|
| 406 |
+
SAMPLE_Q6 = "What did we discuss earlier?"
|
| 407 |
+
SAMPLE_Q7 = "Summarize it in 50 words."
|
requirements.txt
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi>=0.104.0
|
| 2 |
+
uvicorn>=0.24.0
|
| 3 |
+
langchain>=0.1.0
|
| 4 |
+
langchain-community>=0.0.10
|
| 5 |
+
langchain-core>=0.1.0
|
| 6 |
+
langchain-text-splitters>=0.0.1
|
| 7 |
+
langchain-huggingface>=0.0.1
|
| 8 |
+
langchain-ollama>=0.1.0
|
| 9 |
+
langgraph>=0.2.0
|
| 10 |
+
huggingface_hub>=0.19.0
|
| 11 |
+
gradio==6.0.1
|
| 12 |
+
pypdf>=3.17.0
|
| 13 |
+
sentence-transformers>=2.2.2
|
| 14 |
+
faiss-cpu>=1.7.4
|
| 15 |
+
requests>=2.31.0
|
| 16 |
+
pydantic>=2.4.0
|
| 17 |
+
python-multipart>=0.0.6
|