Spaces:
Sleeping
Sleeping
File size: 1,563 Bytes
f2ec379 5d796ae f2ec379 5d796ae f2ec379 5d796ae f2ec379 5d796ae f2ec379 | 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 46 47 48 49 | import os
import gradio as gr
from config import DATA_DIR, VECTOR_DIR, GROQ_API_KEY
from ingestion import ingest_pdf
from rag import answer, reset_memory
# ---- Runtime folders ----
os.makedirs(DATA_DIR, exist_ok=True)
os.makedirs(VECTOR_DIR, exist_ok=True)
def do_ingest(pdf_file):
if GROQ_API_KEY.strip() == "":
return "⚠️ GROQ_API_KEY is missing. Add it in your environment / HF Space secrets."
if pdf_file is None:
return "Please upload a PDF."
# pdf_file is a path string from Gradio
msg = ingest_pdf(pdf_file)
reset_memory()
return f"✅ {msg}"
def chat_fn(user_message, history):
if not os.path.exists(VECTOR_DIR) or not os.listdir(VECTOR_DIR):
return "Please upload a PDF and click 'Ingest PDF' first.", history
ans = answer(user_message)
history.append((user_message, ans))
return "", history
with gr.Blocks() as demo:
gr.Markdown("## 📚 Chat with Your PDF — FAISS + Groq + Memory (LangChain)")
with gr.Row():
pdf = gr.File(label="Upload PDF", file_types=[".pdf"], type="filepath")
ingest_btn = gr.Button("Ingest PDF")
status = gr.Textbox(label="Status", interactive=False)
chatbot = gr.Chatbot(label="Chat")
msg = gr.Textbox(label="Ask a question about the PDF and press Enter")
clear = gr.Button("Clear Chat")
ingest_btn.click(do_ingest, inputs=pdf, outputs=status)
msg.submit(chat_fn, [msg, chatbot], [msg, chatbot])
clear.click(lambda: None, None, chatbot, queue=False)
if __name__ == "__main__":
demo.launch()
|