Spaces:
Sleeping
Sleeping
File size: 1,475 Bytes
0ebeee2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# day3/gradio_rag.py
import gradio as gr
from dotenv import load_dotenv
from rag_system import RAGPipeline
import traceback
import os
load_dotenv() # ensure GROQ_API_KEY is loaded for ChatGroq
# Use a writable persistent dir
rag = RAGPipeline(persist_dir="./chroma_db_space", collection_name="pdf_docs")
def chat_with_pdf(pdf_path: str, question: str):
try:
if not pdf_path:
return "Please upload a PDF."
if not question or not question.strip():
return "Please enter a question."
# Index the uploaded PDF (path is a string because of type='filepath')
rag.index_document(pdf_path, doc_id_prefix="upload")
# Ask
out = rag.query(question, k=4)
return out["answer"]
except Exception as e:
# Surface the exact error to the UI for debugging
return f"Error: {e}\n\n{traceback.format_exc()}"
demo = gr.Interface(
fn=chat_with_pdf,
inputs=[
gr.File(label="Upload PDF", file_types=[".pdf"], type="filepath"),
gr.Textbox(label="Ask a question", placeholder="What does the PDF say?"),
],
outputs=gr.Textbox(label="Answer"),
title="PDF RAG (Chroma + Groq)",
description="Upload a PDF and ask a question. Uses Chroma for retrieval and Groq LLM for answers."
)
if __name__ == "__main__":
# Optional: show whether the env var is visible to this process
print("GROQ key present:", bool(os.getenv("GROQ_API_KEY")))
demo.launch()
|