Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
import faiss
|
|
@@ -9,28 +10,28 @@ from sentence_transformers import SentenceTransformer
|
|
| 9 |
from huggingface_hub import login
|
| 10 |
|
| 11 |
# ββ Auth βββββββββββββββββββββββββββββββββββββββββββββ
|
| 12 |
-
|
| 13 |
-
if
|
| 14 |
-
login(token=
|
| 15 |
|
| 16 |
# ββ Config βββββββββββββββββββββββββββββββββββββββββββ
|
| 17 |
-
CHECKPOINT
|
| 18 |
-
K_PASSAGES
|
| 19 |
MAX_NEW_TOKS = 120
|
| 20 |
W_REL = 1.0; W_SUP = 1.0; W_USE = 0.5
|
| 21 |
|
| 22 |
# ββ Reflection Tokens βββββββββββββββββββββββββββββββββ
|
| 23 |
class RT:
|
| 24 |
-
RETRIEVAL
|
| 25 |
-
RELEVANT
|
| 26 |
-
IRRELEVANT
|
| 27 |
-
FULLY
|
| 28 |
-
PARTIALLY
|
| 29 |
-
NO_SUPPORT
|
| 30 |
U5="[Utility:5]"; U4="[Utility:4]"; U3="[Utility:3]"
|
| 31 |
U2="[Utility:2]"; U1="[Utility:1]"
|
| 32 |
-
USE_SCORES
|
| 33 |
-
|
| 34 |
|
| 35 |
# ββ Documents βββββββββββββββββββββββββββββββββββββββββ
|
| 36 |
DOCUMENTS = [
|
|
@@ -76,7 +77,7 @@ def retrieve(query):
|
|
| 76 |
_, idxs = index.search(q, K_PASSAGES)
|
| 77 |
return [passages[i] for i in idxs[0]]
|
| 78 |
|
| 79 |
-
# ββ
|
| 80 |
tokenizer = AutoTokenizer.from_pretrained(CHECKPOINT)
|
| 81 |
|
| 82 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -87,7 +88,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 87 |
)
|
| 88 |
model.eval()
|
| 89 |
|
| 90 |
-
# ββ
|
| 91 |
def parse(raw):
|
| 92 |
r = {"isrel":None,"issup":None,"isuse":None,"answer":raw}
|
| 93 |
for t in [RT.RELEVANT, RT.IRRELEVANT]:
|
|
@@ -106,7 +107,6 @@ def calc_score(p):
|
|
| 106 |
if p["isuse"]: s+=W_USE*RT.USE_SCORES.get(p["isuse"],0)
|
| 107 |
return s
|
| 108 |
|
| 109 |
-
# ββ Generation βββββββββββββββββββββββββββββββββββββββ
|
| 110 |
def generate_candidates(query, retrieved):
|
| 111 |
results=[]
|
| 112 |
for p in retrieved:
|
|
@@ -140,83 +140,43 @@ def generate_candidates(query, retrieved):
|
|
| 140 |
def run_inference(query):
|
| 141 |
retrieved=retrieve(query)
|
| 142 |
candidates=generate_candidates(query,retrieved)
|
|
|
|
| 143 |
if not candidates:
|
| 144 |
return None,retrieved,True
|
|
|
|
| 145 |
candidates.sort(key=lambda x:x.score, reverse=True)
|
| 146 |
best=candidates[0]
|
|
|
|
| 147 |
abstained=(best.issup==RT.NO_SUPPORT)
|
| 148 |
return best,retrieved,abstained
|
| 149 |
|
| 150 |
-
# ββ
|
| 151 |
-
def render_output(best, retrieved, abstained):
|
| 152 |
-
if abstained or best is None:
|
| 153 |
-
return f"<div style='background:#FCEBEB;padding:16px;border-radius:10px;color:#A32D2D'>β οΈ ABSTAINED β Insufficient Evidence</div>"
|
| 154 |
-
|
| 155 |
-
return f"""
|
| 156 |
-
<div style='padding:16px;border-radius:10px;background:#F7F7F9'>
|
| 157 |
-
<div style='margin-bottom:8px'><b>Best passage:</b> [{best.passage.id}] {best.passage.title}</div>
|
| 158 |
-
<div style='margin-bottom:8px'>
|
| 159 |
-
<span>{best.isrel or ''}</span>
|
| 160 |
-
<span>{best.issup or ''}</span>
|
| 161 |
-
<span>{best.isuse or ''}</span>
|
| 162 |
-
</div>
|
| 163 |
-
<div>{best.text}</div>
|
| 164 |
-
</div>
|
| 165 |
-
"""
|
| 166 |
-
|
| 167 |
def answer(q):
|
| 168 |
best, retrieved, abstained = run_inference(q)
|
| 169 |
-
return
|
| 170 |
|
| 171 |
-
|
| 172 |
-
with gr.Blocks(title="SELF-RAG Demo β Agentic Self-Reflective RAG") as demo:
|
| 173 |
|
| 174 |
gr.Markdown("""
|
| 175 |
# SELF-RAG Phase 1 β Live Demo
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
> The system retrieves passages, generates an answer, and critiques its own output.
|
| 180 |
-
> If no passage supports the answer, the system **abstains**.
|
| 181 |
""")
|
| 182 |
|
| 183 |
-
gr.
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
| ID | Topic |
|
| 187 |
-
|---|---|
|
| 188 |
-
| doc_00 | RAG Overview |
|
| 189 |
-
| doc_01 | Vanilla RAG Limitations |
|
| 190 |
-
| doc_02 | SELF-RAG Framework |
|
| 191 |
-
| doc_03 | CRAG |
|
| 192 |
-
| doc_04 | RAGAs |
|
| 193 |
-
| doc_05 | Hallucination |
|
| 194 |
-
| doc_06 | Adaptive Retrieval |
|
| 195 |
-
| doc_07 | Beam Search |
|
| 196 |
-
|
| 197 |
-
Ask outside topics to trigger abstention.
|
| 198 |
-
""")
|
| 199 |
-
|
| 200 |
-
with gr.Row():
|
| 201 |
-
with gr.Column(scale=4):
|
| 202 |
-
question_box = gr.Textbox(label="Your Question", lines=2)
|
| 203 |
-
with gr.Column(scale=1):
|
| 204 |
-
submit_btn = gr.Button("Ask")
|
| 205 |
-
|
| 206 |
-
output_html = gr.HTML()
|
| 207 |
|
| 208 |
gr.Examples(
|
| 209 |
examples=[
|
| 210 |
-
["What is SELF-RAG
|
| 211 |
-
["What is hallucination
|
| 212 |
["What is the capital of France?"],
|
| 213 |
],
|
| 214 |
-
inputs=
|
| 215 |
-
outputs=
|
| 216 |
fn=answer
|
| 217 |
)
|
| 218 |
|
| 219 |
-
|
| 220 |
-
question_box.submit(answer, inputs=question_box, outputs=output_html)
|
| 221 |
|
| 222 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
| 5 |
import faiss
|
|
|
|
| 10 |
from huggingface_hub import login
|
| 11 |
|
| 12 |
# ββ Auth βββββββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
+
_token = os.environ.get("HF_TOKEN")
|
| 14 |
+
if _token:
|
| 15 |
+
login(token=_token)
|
| 16 |
|
| 17 |
# ββ Config βββββββββββββββββββββββββββββββββββββββββββ
|
| 18 |
+
CHECKPOINT = "selfrag/selfrag_llama2_7b"
|
| 19 |
+
K_PASSAGES = 5
|
| 20 |
MAX_NEW_TOKS = 120
|
| 21 |
W_REL = 1.0; W_SUP = 1.0; W_USE = 0.5
|
| 22 |
|
| 23 |
# ββ Reflection Tokens βββββββββββββββββββββββββββββββββ
|
| 24 |
class RT:
|
| 25 |
+
RETRIEVAL = "[Retrieval]"
|
| 26 |
+
RELEVANT = "[Relevant]"
|
| 27 |
+
IRRELEVANT = "[Irrelevant]"
|
| 28 |
+
FULLY = "[Fully supported]"
|
| 29 |
+
PARTIALLY = "[Partially supported]"
|
| 30 |
+
NO_SUPPORT = "[No support / Contradictory]"
|
| 31 |
U5="[Utility:5]"; U4="[Utility:4]"; U3="[Utility:3]"
|
| 32 |
U2="[Utility:2]"; U1="[Utility:1]"
|
| 33 |
+
USE_SCORES = {"[Utility:5]":1.0,"[Utility:4]":0.5,"[Utility:3]":0.0,
|
| 34 |
+
"[Utility:2]":-0.5,"[Utility:1]":-1.0}
|
| 35 |
|
| 36 |
# ββ Documents βββββββββββββββββββββββββββββββββββββββββ
|
| 37 |
DOCUMENTS = [
|
|
|
|
| 77 |
_, idxs = index.search(q, K_PASSAGES)
|
| 78 |
return [passages[i] for i in idxs[0]]
|
| 79 |
|
| 80 |
+
# ββ MODEL (ONLY FIXED PART) βββββββββββββββββββββββββββ
|
| 81 |
tokenizer = AutoTokenizer.from_pretrained(CHECKPOINT)
|
| 82 |
|
| 83 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 88 |
)
|
| 89 |
model.eval()
|
| 90 |
|
| 91 |
+
# ββ ORIGINAL LOGIC (UNCHANGED) ββββββββββββββββββββββββ
|
| 92 |
def parse(raw):
|
| 93 |
r = {"isrel":None,"issup":None,"isuse":None,"answer":raw}
|
| 94 |
for t in [RT.RELEVANT, RT.IRRELEVANT]:
|
|
|
|
| 107 |
if p["isuse"]: s+=W_USE*RT.USE_SCORES.get(p["isuse"],0)
|
| 108 |
return s
|
| 109 |
|
|
|
|
| 110 |
def generate_candidates(query, retrieved):
|
| 111 |
results=[]
|
| 112 |
for p in retrieved:
|
|
|
|
| 140 |
def run_inference(query):
|
| 141 |
retrieved=retrieve(query)
|
| 142 |
candidates=generate_candidates(query,retrieved)
|
| 143 |
+
|
| 144 |
if not candidates:
|
| 145 |
return None,retrieved,True
|
| 146 |
+
|
| 147 |
candidates.sort(key=lambda x:x.score, reverse=True)
|
| 148 |
best=candidates[0]
|
| 149 |
+
|
| 150 |
abstained=(best.issup==RT.NO_SUPPORT)
|
| 151 |
return best,retrieved,abstained
|
| 152 |
|
| 153 |
+
# ββ UI (UNCHANGED) ββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
def answer(q):
|
| 155 |
best, retrieved, abstained = run_inference(q)
|
| 156 |
+
return f"Best passage: [{best.passage.id}] {best.passage.title}\n\n{best.text}" if not abstained else "β οΈ ABSTAINED"
|
| 157 |
|
| 158 |
+
with gr.Blocks(title="SELF-RAG Demo") as demo:
|
|
|
|
| 159 |
|
| 160 |
gr.Markdown("""
|
| 161 |
# SELF-RAG Phase 1 β Live Demo
|
| 162 |
+
Aravind Kashyap | PES2PGE24DS068
|
| 163 |
+
Guide: Shankar DK | PES University
|
|
|
|
|
|
|
|
|
|
| 164 |
""")
|
| 165 |
|
| 166 |
+
qbox = gr.Textbox(label="Your Question")
|
| 167 |
+
out = gr.Textbox(label="Response")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
gr.Examples(
|
| 170 |
examples=[
|
| 171 |
+
["What is SELF-RAG?"],
|
| 172 |
+
["What is hallucination?"],
|
| 173 |
["What is the capital of France?"],
|
| 174 |
],
|
| 175 |
+
inputs=qbox,
|
| 176 |
+
outputs=out,
|
| 177 |
fn=answer
|
| 178 |
)
|
| 179 |
|
| 180 |
+
qbox.submit(answer, inputs=qbox, outputs=out)
|
|
|
|
| 181 |
|
| 182 |
demo.launch()
|