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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
# --- RAG Libraries ---
|
| 6 |
+
from langchain.document_loaders import PyPDFLoader, TextLoader, Docx2txtLoader
|
| 7 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 8 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 9 |
+
from langchain.vectorstores import FAISS
|
| 10 |
+
from langchain.chat_models import ChatOpenAI
|
| 11 |
+
from langchain.chains import RetrievalQA
|
| 12 |
+
from langchain.prompts import PromptTemplate
|
| 13 |
+
from langchain.schema import Document # Needed for creating custom documents from Excel
|
| 14 |
+
|
| 15 |
+
# For Excel file handling
|
| 16 |
+
import pandas as pd
|
| 17 |
+
|
| 18 |
+
# Load environment variables (for API keys when running locally)
|
| 19 |
+
# On Hugging Face Spaces, secrets are automatically loaded as environment variables.
|
| 20 |
+
load_dotenv()
|
| 21 |
+
|
| 22 |
+
# --- RAG Setup ---
|
| 23 |
+
# Initialize embeddings and LLM globally for efficiency.
|
| 24 |
+
# This assumes OPENAI_API_KEY is available as an environment variable.
|
| 25 |
+
rag_initialized = False
|
| 26 |
+
try:
|
| 27 |
+
embeddings = OpenAIEmbeddings()
|
| 28 |
+
llm = ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo")
|
| 29 |
+
rag_initialized = True
|
| 30 |
+
print("OpenAI embeddings and LLM initialized successfully.")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Error initializing OpenAI components: {e}")
|
| 33 |
+
print("Please ensure OPENAI_API_KEY is correctly set in your environment or Hugging Face Space secrets.")
|
| 34 |
+
print("RAG functionality will not work without a valid OpenAI API key.")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def get_document_content(file_path, file_extension, filename):
|
| 38 |
+
"""
|
| 39 |
+
Loads and returns text from various document types, including Excel, using Langchain loaders.
|
| 40 |
+
For Excel, it converts each row into a Langchain Document object.
|
| 41 |
+
"""
|
| 42 |
+
print(f"Attempting to load document: {filename} with extension {file_extension}")
|
| 43 |
+
documents = []
|
| 44 |
+
|
| 45 |
+
if file_extension in ['.xlsx', '.xls']:
|
| 46 |
+
try:
|
| 47 |
+
df = pd.read_excel(file_path)
|
| 48 |
+
# Convert DataFrame rows to text chunks for RAG
|
| 49 |
+
for index, row in df.iterrows():
|
| 50 |
+
# Customize how you want each row to be represented as text for the LLM
|
| 51 |
+
# Example: F"Row {index+1}: {row.to_json()}" provides all column data as JSON.
|
| 52 |
+
# Or for more readable context, pick specific columns:
|
| 53 |
+
# doc_content = f"Row {index+1} from '{filename}': " + ", ".join([f"{col}: {val}" for col, val in row.items()])
|
| 54 |
+
doc_content = f"Row {index+1} from '{filename}': {row.to_json()}"
|
| 55 |
+
|
| 56 |
+
documents.append(Document(page_content=doc_content, metadata={"source": filename, "row_number": index+1}))
|
| 57 |
+
print(f"Excel file '{filename}' processed into {len(documents)} Langchain Documents.")
|
| 58 |
+
except Exception as e:
|
| 59 |
+
raise ValueError(f"Could not process Excel file '{filename}': {e}")
|
| 60 |
+
elif file_extension == '.pdf':
|
| 61 |
+
loader = PyPDFLoader(file_path)
|
| 62 |
+
documents = loader.load()
|
| 63 |
+
elif file_extension == '.txt':
|
| 64 |
+
loader = TextLoader(file_path)
|
| 65 |
+
documents = loader.load()
|
| 66 |
+
elif file_extension == '.docx':
|
| 67 |
+
loader = Docx2txtLoader(file_path)
|
| 68 |
+
documents = loader.load()
|
| 69 |
+
else:
|
| 70 |
+
raise ValueError(f"Unsupported file type: {file_extension}. Please upload .pdf, .txt, .docx, .xlsx, or .xls files.")
|
| 71 |
+
|
| 72 |
+
if not documents:
|
| 73 |
+
raise ValueError("Could not load any content from the document. It might be empty, corrupted, or an unrecognized format.")
|
| 74 |
+
return documents
|
| 75 |
+
|
| 76 |
+
def analyze_document_with_rag(file, query):
|
| 77 |
+
"""
|
| 78 |
+
Core RAG function that processes the uploaded file and answers the query.
|
| 79 |
+
This function is directly exposed by Gradio.
|
| 80 |
+
"""
|
| 81 |
+
if not rag_initialized:
|
| 82 |
+
return "RAG system not initialized. Please ensure your **OPENAI_API_KEY** is configured correctly in Hugging Face Space secrets."
|
| 83 |
+
|
| 84 |
+
if file is None:
|
| 85 |
+
return "Please upload a file to analyze."
|
| 86 |
+
if not query.strip():
|
| 87 |
+
return "Please enter a question or analysis request."
|
| 88 |
+
|
| 89 |
+
# Gradio provides the file object, use .name to get the path to the temporary file
|
| 90 |
+
file_path = file.name
|
| 91 |
+
filename = os.path.basename(file_path) # Extract just the filename from the path
|
| 92 |
+
|
| 93 |
+
file_extension = os.path.splitext(filename)[1].lower()
|
| 94 |
+
|
| 95 |
+
try:
|
| 96 |
+
# 1. Load Document
|
| 97 |
+
documents = get_document_content(file_path, file_extension, filename)
|
| 98 |
+
print(f"Document content loaded. Total {len(documents)} parts/pages/rows identified.")
|
| 99 |
+
|
| 100 |
+
# 2. Split Text (even Excel rows can be split further if they are long)
|
| 101 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 102 |
+
texts = text_splitter.split_documents(documents)
|
| 103 |
+
print(f"Text split into {len(texts)} chunks for embedding.")
|
| 104 |
+
|
| 105 |
+
if not texts:
|
| 106 |
+
return "No usable text extracted from the document after splitting. It might be too short or unparsable."
|
| 107 |
+
|
| 108 |
+
# 3. Create Embeddings and Vector Store
|
| 109 |
+
print("Creating embeddings and vector store (FAISS)...")
|
| 110 |
+
vectorstore = FAISS.from_documents(texts, embeddings)
|
| 111 |
+
print("Vector store created successfully.")
|
| 112 |
+
|
| 113 |
+
# 4. Set up RetrievalQA Chain with Custom Prompt for desired output
|
| 114 |
+
template = """
|
| 115 |
+
You are an expert document analyst. Use the following context to answer the user's question.
|
| 116 |
+
Be thorough, precise, and directly address all parts of the question.
|
| 117 |
+
If the question cannot be answered from the provided context, please state that explicitly
|
| 118 |
+
and explain why (e.g., "The document does not contain information about...").
|
| 119 |
+
Do not make up information.
|
| 120 |
+
The context might contain information from an Excel file, where each entry represents a row.
|
| 121 |
+
|
| 122 |
+
Context:
|
| 123 |
+
{context}
|
| 124 |
+
|
| 125 |
+
Question: {question}
|
| 126 |
+
|
| 127 |
+
Your detailed analysis and answer:
|
| 128 |
+
"""
|
| 129 |
+
QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
|
| 130 |
+
|
| 131 |
+
qa_chain = RetrievalQA.from_chain_type(
|
| 132 |
+
llm=llm,
|
| 133 |
+
chain_type="stuff", # "stuff" combines all retrieved docs into one prompt
|
| 134 |
+
retriever=vectorstore.as_retriever(),
|
| 135 |
+
chain_type_kwargs={"prompt": QA_CHAIN_PROMPT} # Pass your custom prompt
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# 5. Get Answer
|
| 139 |
+
print(f"Running RAG query for: '{query}'")
|
| 140 |
+
result = qa_chain.run(query)
|
| 141 |
+
print(f"Analysis complete. Result length: {len(result)} characters.")
|
| 142 |
+
|
| 143 |
+
return result
|
| 144 |
+
|
| 145 |
+
except ValueError as e:
|
| 146 |
+
print(f"Value error during analysis: {e}")
|
| 147 |
+
return f"Analysis Error: {e}"
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print(f"An unexpected error occurred during analysis: {e}")
|
| 150 |
+
# In a production app, log the full traceback here for debugging.
|
| 151 |
+
return "An internal server error occurred during analysis. Please try again or contact support. Check the Space logs for more details."
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
# --- Gradio Interface Setup ---
|
| 155 |
+
iface = gr.Interface(
|
| 156 |
+
fn=analyze_document_with_rag, # The Python function to wrap
|
| 157 |
+
inputs=[
|
| 158 |
+
gr.File(label="Upload a Document", file_types=[".txt", ".pdf", ".docx", ".xlsx", ".xls"], type="file"),
|
| 159 |
+
gr.Textbox(label="Your Question/Analysis Request", placeholder="e.g., 'Summarize this document.' or 'What are the key findings?'")
|
| 160 |
+
],
|
| 161 |
+
outputs=gr.Textbox(label="Analysis Output", show_copy_button=True), # Output for the result
|
| 162 |
+
title="Versatile Document Analysis with RAG",
|
| 163 |
+
description=(
|
| 164 |
+
"Upload a document (.txt, .pdf, .docx, .xlsx, or .xls) and ask a question to get an AI-powered analysis. "
|
| 165 |
+
"For Excel files, each row is processed as a context unit."
|
| 166 |
+
),
|
| 167 |
+
allow_flagging="never", # Disable flagging system for simple demos
|
| 168 |
+
theme=gr.themes.Soft(), # A modern, soft theme
|
| 169 |
+
examples=[
|
| 170 |
+
# You can add example usage here for users
|
| 171 |
+
# For files, you'd need to provide paths to example files
|
| 172 |
+
# ["path/to/example.pdf", "What is the main topic?"],
|
| 173 |
+
# ["path/to/example.xlsx", "What is the average price of product X?"]
|
| 174 |
+
]
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# This block is for running the Gradio app locally.
|
| 178 |
+
# On Hugging Face Spaces, the 'app.py' file is automatically run by the Gradio SDK.
|
| 179 |
+
if __name__ == "__main__":
|
| 180 |
+
if not os.getenv("OPENAI_API_KEY"):
|
| 181 |
+
print("\n" + "="*80)
|
| 182 |
+
print("WARNING: OPENAI_API_KEY environment variable not found for local execution.")
|
| 183 |
+
print("RAG functionality will not work locally without it. Please set it in a .env file or your environment.")
|
| 184 |
+
print("Example: OPENAI_API_KEY='sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'")
|
| 185 |
+
print("="*80 + "\n")
|
| 186 |
+
iface.launch()
|