File size: 1,827 Bytes
d3ae497
a4689b0
d3ae497
 
 
 
 
 
 
 
 
 
 
 
 
5c0610c
ad4fb63
679bed8
 
 
5c0610c
d3ae497
ad4fb63
5c0610c
d3ae497
5c0610c
d3ae497
 
 
 
 
 
5c0610c
d3ae497
 
 
 
 
 
 
 
 
 
 
 
 
7ed228e
 
 
d3ae497
 
 
a4689b0
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

# !pip install transformers faiss-cpu gradio sentence-transformers nlpaug scikit-learn

# rag_module.py

import json
import faiss
import numpy as np
from sentence_transformers import SentenceTransformer, CrossEncoder
from sklearn.feature_extraction.text import TfidfVectorizer
from transformers import AutoTokenizer, T5ForConditionalGeneration
import gradio as gr

from rag_pipeline import RAGPipeline
from adversarial_framework import *
# Load all models and retrievers ONCE
rag = RAGPipeline(
    embedder_model = "infly/inf-retriever-v1-1.5b",
    reranker_model = "cross-encoder/ms-marco-MiniLM-L-6-v2",
    generator_model = "google/flan-t5-base"
)


adv_pipeline = AdversarialAttackPipeline(answer_generator=rag.generate_answer)

# Define the Gradio wrapper
def gradio_wrapper(query, method, k):
    stats_text, auc, fig, pert_q, pert_r, adv_r = adv_pipeline.evaluate_adversarial_robustness(
        query=query,
        method=method,
        k=k
    )
    return stats_text, f"{auc}", fig, pert_q, pert_r, adv_r


gr.Interface(
    fn=gradio_wrapper,
    inputs=[
        gr.Textbox(label="Enter a Question"),
        gr.Dropdown(choices=["synonym", "delete", "contextual"], label="Perturbation Method"),
        gr.Slider(1, 5, step=1, value=3, label="Top-K Retrieved Chunks")
    ],
    outputs=[
        gr.Textbox(label="πŸ“Š Summary Statistics"),
        gr.Textbox(label="πŸ”Ί PSC-AUC Score"),
        gr.Plot(label="πŸ“ˆ PSC Curve"),
        # gr.Textbox(label="🟠 Perturbed Query Example"),
        # gr.Textbox(label="🟒 Perturbed Response Example"),
        # gr.Textbox(label="πŸ”΄ Directly Perturbed Normal Response Example")
    ],
    title="Adversarial Testing on RAGiant System",
    description="Evaluate robustness against textual attacks and visualize degradation with ARI & PSC-AUC."
).launch()