RegRAGapp / app.py
zakinho00's picture
add GPU
da13056
raw
history blame
1.64 kB
# app.py
import gradio as gr
from rag_agent import prepare_index_and_chunks, load_model
from utils.retrieval import retrieve_relevant_chunks
from utils.generation import generate_answer
# β€”β€”β€” FIXED CONFIGURATION β€”β€”β€”
PDF_FOLDER = "./data" # your folder with all PDFs
EMBEDDER = "all-MiniLM-L6-v2"
CHUNK_SIZE = 500
OVERLAP = 100
INDEX_TYPE = "innerproduct"
MODEL_NAME = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
TOP_K = 5
# β€”β€”β€” PREPARE INDEX & MODEL ONCE β€”β€”β€”
faiss_index_path, chunks_path = prepare_index_and_chunks(
pdf_folder=PDF_FOLDER,
chunk_size=CHUNK_SIZE,
overlap=OVERLAP,
index_type=INDEX_TYPE,
embedder_name=EMBEDDER
)
model, tokenizer = load_model(MODEL_NAME)
# β€”β€”β€” INFERENCE FUNCTION β€”β€”β€”
@spaces.GPU()
def answer_query(query: str) -> str:
if not query.strip():
return "⚠️ Please enter a question."
# Retrieve top-K chunks
chunks = retrieve_relevant_chunks(
query=query,
embedder_name=EMBEDDER,
k=TOP_K,
faiss_index=faiss_index_path,
chunks_path=chunks_path
)
# Generate answer
return generate_answer(query, chunks, model, tokenizer)
# β€”β€”β€” GRADIO UI β€”β€”β€”
iface = gr.Interface(
fn=answer_query,
inputs=gr.Textbox(lines=2, placeholder="Type your telecom question here…", label="Question"),
outputs=gr.Textbox(label="Answer"),
title="πŸ“‘ Telecom RAG Assistant",
description=(
"Ask questions over the preloaded telecom regulation PDFs.\n\n"
)
)
if __name__ == "__main__":
iface.launch(share=True)