Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import faiss
|
| 3 |
import json
|
|
@@ -5,45 +55,58 @@ import numpy as np
|
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
from transformers import pipeline
|
| 7 |
|
|
|
|
| 8 |
embed_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 9 |
-
index = faiss.read_index("faiss_index.bin")
|
| 10 |
with open("processed_chunks.json", "r") as f:
|
| 11 |
chunks = json.load(f)
|
| 12 |
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
def
|
| 20 |
-
# Retrieval
|
| 21 |
query_vec = embed_model.encode([question])
|
| 22 |
distances, indices = index.search(np.array(query_vec).astype('float32'), k=1)
|
| 23 |
|
| 24 |
-
# out-of-scope questions
|
| 25 |
if distances[0][0] > 1.5:
|
| 26 |
-
return "
|
| 27 |
|
| 28 |
-
# Context Retrieval
|
| 29 |
relevant_chunk = chunks[indices[0][0]]
|
| 30 |
context = relevant_chunk['text']
|
| 31 |
source = relevant_chunk['doc_name']
|
| 32 |
-
|
| 33 |
-
# Grounded Generation
|
| 34 |
prompt = f"Context: {context}\nQuestion: {question}\nAnswer only from context. Cite source: {source}\nAnswer:"
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
return answer, source
|
| 39 |
|
| 40 |
|
| 41 |
interface = gr.Interface(
|
| 42 |
-
fn=
|
| 43 |
inputs=gr.Textbox(label="Ask an HR Question"),
|
| 44 |
-
outputs=[
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
interface.launch()
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
# import faiss
|
| 3 |
+
# import json
|
| 4 |
+
# import numpy as np
|
| 5 |
+
# from sentence_transformers import SentenceTransformer
|
| 6 |
+
# from transformers import pipeline
|
| 7 |
+
|
| 8 |
+
# embed_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 9 |
+
# index = faiss.read_index("faiss_index.bin")
|
| 10 |
+
# with open("processed_chunks.json", "r") as f:
|
| 11 |
+
# chunks = json.load(f)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# #weights load automatically to the Space
|
| 15 |
+
# pipe = pipeline("text-generation", model="ibm-granite/granite-3.1-2b-instruct")
|
| 16 |
+
# # pipe = pipeline("text-generation", model="ibm-granite/granite-3.3-8b-instruct")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# def ask_hr_bot(question):
|
| 20 |
+
# # Retrieval
|
| 21 |
+
# query_vec = embed_model.encode([question])
|
| 22 |
+
# distances, indices = index.search(np.array(query_vec).astype('float32'), k=1)
|
| 23 |
+
|
| 24 |
+
# # out-of-scope questions
|
| 25 |
+
# if distances[0][0] > 1.5:
|
| 26 |
+
# return "I could not find information on this in the HR documents.", "N/A"
|
| 27 |
+
|
| 28 |
+
# # Context Retrieval
|
| 29 |
+
# relevant_chunk = chunks[indices[0][0]]
|
| 30 |
+
# context = relevant_chunk['text']
|
| 31 |
+
# source = relevant_chunk['doc_name']
|
| 32 |
+
|
| 33 |
+
# # Grounded Generation
|
| 34 |
+
# prompt = f"Context: {context}\nQuestion: {question}\nAnswer only from context. Cite source: {source}\nAnswer:"
|
| 35 |
+
# res = pipe(prompt, max_new_tokens=60, do_sample=False)
|
| 36 |
+
|
| 37 |
+
# answer = res[0]['generated_text'].split("Answer:")[-1].strip()
|
| 38 |
+
# return answer, source
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# interface = gr.Interface(
|
| 42 |
+
# fn=ask_hr_bot,
|
| 43 |
+
# inputs=gr.Textbox(label="Ask an HR Question"),
|
| 44 |
+
# outputs=[gr.Textbox(label="Bot Answer"), gr.Textbox(label="Source Used")],
|
| 45 |
+
# title="HR Knowledge Assistant",
|
| 46 |
+
# description="Enterprise RAG Prototype using IBM Granite Instruct Family."
|
| 47 |
+
# )
|
| 48 |
+
|
| 49 |
+
# interface.launch()
|
| 50 |
+
|
| 51 |
import gradio as gr
|
| 52 |
import faiss
|
| 53 |
import json
|
|
|
|
| 55 |
from sentence_transformers import SentenceTransformer
|
| 56 |
from transformers import pipeline
|
| 57 |
|
| 58 |
+
|
| 59 |
embed_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 60 |
+
index = faiss.read_index("faiss_index.bin")
|
| 61 |
with open("processed_chunks.json", "r") as f:
|
| 62 |
chunks = json.load(f)
|
| 63 |
|
| 64 |
|
| 65 |
+
MODELS = {
|
| 66 |
+
"Granite 3.1 2B": "ibm-granite/granite-3.1-2b-instruct",
|
| 67 |
+
"Llama 3.2 1B": "meta-llama/Llama-3.2-1B-Instruct",
|
| 68 |
+
"Phi 3.5 Mini": "microsoft/Phi-3.5-mini-instruct",
|
| 69 |
+
"Gemma 2 2B": "google/gemma-2-2b-it"
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
|
| 73 |
+
pipes = {name: pipeline("text-generation", model=id, device_map="auto")
|
| 74 |
+
for name, id in MODELS.items()}
|
| 75 |
|
| 76 |
+
def compare_hr_bots(question):
|
| 77 |
+
# Retrieval (Same for all models to ensure fair comparison)
|
| 78 |
query_vec = embed_model.encode([question])
|
| 79 |
distances, indices = index.search(np.array(query_vec).astype('float32'), k=1)
|
| 80 |
|
|
|
|
| 81 |
if distances[0][0] > 1.5:
|
| 82 |
+
return ["Out of scope"] * 4, "N/A"
|
| 83 |
|
|
|
|
| 84 |
relevant_chunk = chunks[indices[0][0]]
|
| 85 |
context = relevant_chunk['text']
|
| 86 |
source = relevant_chunk['doc_name']
|
|
|
|
|
|
|
| 87 |
prompt = f"Context: {context}\nQuestion: {question}\nAnswer only from context. Cite source: {source}\nAnswer:"
|
| 88 |
+
|
| 89 |
+
answers = []
|
| 90 |
+
for name in MODELS.keys():
|
| 91 |
+
res = pipes[name](prompt, max_new_tokens=60, do_sample=False)
|
| 92 |
+
ans = res[0]['generated_text'].split("Answer:")[-1].strip()
|
| 93 |
+
answers.append(ans)
|
| 94 |
|
| 95 |
+
return answers[0], answers[1], answers[2], answers[3], source
|
|
|
|
| 96 |
|
| 97 |
|
| 98 |
interface = gr.Interface(
|
| 99 |
+
fn=compare_hr_bots,
|
| 100 |
inputs=gr.Textbox(label="Ask an HR Question"),
|
| 101 |
+
outputs=[
|
| 102 |
+
gr.Textbox(label="IBM Granite 3.1"),
|
| 103 |
+
gr.Textbox(label="Meta Llama 3.2"),
|
| 104 |
+
gr.Textbox(label="Microsoft Phi 3.5"),
|
| 105 |
+
gr.Textbox(label="Google Gemma 2"),
|
| 106 |
+
gr.Textbox(label="Source Used")
|
| 107 |
+
],
|
| 108 |
+
title="RAG Model Benchmarking: HR Assistant",
|
| 109 |
+
description="Comparing grounding quality across 4 open-source LLMs using ADU HR Policies."
|
| 110 |
)
|
| 111 |
|
| 112 |
interface.launch()
|