File size: 1,699 Bytes
cfa9e5f 576c75f d1c0723 cfa9e5f ed31f0c cfa9e5f e959fc1 cfa9e5f 346de65 cfa9e5f ed31f0c cfa9e5f da13056 cfa9e5f ed31f0c cfa9e5f f95087f cfa9e5f f95087f cfa9e5f f95087f cfa9e5f da13056 |
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 50 51 52 53 54 55 56 57 58 59 |
# app.py
import gradio as gr
import spaces
from rag_agent import prepare_index_and_chunks, load_model
from sentence_transformers import SentenceTransformer
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 = "AliMaatouk/LLama-3-8B-Tele-it" #"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)
embedder = SentenceTransformer(EMBEDDER)
# βββ 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,
embedder,
TOP_K,
faiss_index_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 question hereβ¦", label="Question"),
outputs=gr.Textbox(label="Answer"),
title="π‘ SpectrumGPT",
description=(
"Answer questions on spectrum regulations.\n\n"
)
)
if __name__ == "__main__":
iface.launch(share=True) |