|
|
|
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 |
|
|
|
|
|
PDF_FOLDER = "./data" |
|
EMBEDDER = "all-MiniLM-L6-v2" |
|
CHUNK_SIZE = 500 |
|
OVERLAP = 100 |
|
INDEX_TYPE = "innerproduct" |
|
MODEL_NAME = "AliMaatouk/LLama-3-8B-Tele-it" |
|
TOP_K = 5 |
|
|
|
|
|
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) |
|
|
|
|
|
@spaces.GPU() |
|
def answer_query(query: str) -> str: |
|
if not query.strip(): |
|
return "β οΈ Please enter a question." |
|
|
|
chunks = retrieve_relevant_chunks( |
|
query, |
|
embedder, |
|
TOP_K, |
|
faiss_index_path, |
|
chunks_path |
|
) |
|
|
|
return generate_answer(query, chunks, model, tokenizer) |
|
|
|
|
|
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) |