# app.py import os import gradio as gr from dotenv import load_dotenv # Load secret API keys globally load_dotenv() # Import the functional pipelines you built! from extraction import run_extraction_pipeline from summarize import run_summarization_pipeline from database import run_database_pipeline from pipeline import create_rag_chain # This global variable will hold our live pipeline once a file is processed global_rag_chain = None def process_uploaded_pdf(file_obj): """ Takes an uploaded file object from the UI, extracts the path, runs it through your ingestion pipeline, and updates the active chain. """ global global_rag_chain if file_obj is None: return "❌ Please upload a valid PDF file first." try: # Gradio saves the uploaded file in a temporary folder. # .name gives us the absolute string path to that file (e.g., '/tmp/xyz.pdf') uploaded_file_path = file_obj.name gr.Info("⏳ Step 1/3: Partitioning and chunking the document...") tables, text_chunks, images = run_extraction_pipeline(uploaded_file_path) gr.Info("⏳ Step 2/3: Summarizing layouts, text chunks, and images...") text_sums, table_sums, image_sums = run_summarization_pipeline(tables, text_chunks, images) gr.Info("⏳ Step 3/3: Indexing summaries into Multi-Vector store...") live_retriever = run_database_pipeline(text_chunks, text_sums, tables, table_sums, images, image_sums) # Build the final execution chain and update the global manager variable global_rag_chain = create_rag_chain(live_retriever) return "✅ Document fully processed and indexed! You can now start asking questions on the right." except Exception as e: return f"❌ Pipeline Ingestion Failed: {str(e)}" def predict(message, history): """ Handles user chat questions. Checks if a database is currently loaded before running inference. """ global global_rag_chain # Safeguard block if the user tries to chat before uploading a file if global_rag_chain is None: return "⚠️ No document database found. Please upload a PDF and click 'Process Document' on the left panel first." if not message.strip(): return "Please enter a valid question." try: # Run the production source chain using the user's question string result = global_rag_chain.invoke(message) return result["response"] except Exception as e: return f"❌ An error occurred during inference extraction: {str(e)}" # --- GRADIO INTERFACE ARCHITECTURE --- # We use Blocks here to design a professional two-column application with gr.Blocks() as demo: gr.Markdown("# 🤖 ChartTable RAG") gr.Markdown("Upload **any** document containing text, data tables, and diagrams. The pipeline will build an isolated vector database on the fly.") with gr.Row(): # --- LEFT PANEL: File Upload and Processing Management --- with gr.Column(scale=1): gr.Markdown("### 📥 Step 1: Ingestion Dashboard") file_input = gr.File(label="Upload your PDF file", file_types=[".pdf"]) process_btn = gr.Button("⚙️ Process Document", variant="primary") status_output = gr.Textbox(label="System Status Log", interactive=False, value="Awaiting file upload...") # --- RIGHT PANEL: Interactive Multi-Modal Chat Engine --- with gr.Column(scale=2): gr.Markdown("### 💬 Step 2: Ask Your Data") gr.ChatInterface( fn=predict, ) # Connect the left side click event button action to our processing function process_btn.click( fn=process_uploaded_pdf, inputs=[file_input], outputs=[status_output] ) # Launch the app environment container if __name__ == "__main__": demo.launch(theme = "soft")