maya / app.py
VishalMysore's picture
Update app.py
364b156 verified
raw
history blame
7.14 kB
import gradio as gr
import pandas as pd
import evaluate
import theme
default_css = """\
<style type="text/css">
.diff {
border: 1px solid #cccccc;
background: none repeat scroll 0 0 #f8f8f8;
font-family: 'Bitstream Vera Sans Mono','Courier',monospace;
font-size: 12px;
line-height: 1.4;
white-space: normal;
word-wrap: break-word;
}
.diff div:hover {
background-color:#ffc;
}
.diff .control {
background-color: #eaf2f5;
color: #999999;
}
.diff .insert {
background-color: #ddffdd;
color: #000000;
}
.diff .insert .highlight {
background-color: #aaffaa;
color: #000000;
}
.diff .delete {
background-color: #ffdddd;
color: #000000;
}
.diff .delete .highlight {
background-color: #ffaaaa;
color: #000000;
}
</style>
"""
df = pd.read_csv("./wiki_bio_gpt3_hallucination.csv")
title = "<h1 style='text-align: center; color: #333333; font-size: 40px;'> πŸ”Ž Automatic Hallucination detection with SelfCheckGPT NLI </h1>"
description = """
We show in this demo how metrics to measure inconsistency in the LLM, such as [SelfCheckGPT NLI](https://arxiv.org/abs/2303.08896), can be powerful unsupervised predictors of hallucinations of LLMs.
We evaluate SelfCheckGPT NLI on samples from [Wiki Bio](https://huggingface.co/datasets/potsawee/wiki_bio_gpt3_hallucination) and explore the hallucinations detected by SelfCheckGPT NLI, sentence by sentence.
We explore in depth heuristics about how hallucinations happen, why inconsistency metrics are powerful predictors of hallucinations and how well calibrated they are to detect hallucinations in our [notebook](https://colab.research.google.com/drive/1Qhq2FO4FFX_MKN5IEgia_PrBEttxCQG4?usp=sharing).
**About us**: At [Mithril Security](https://www.mithrilsecurity.io/) we focus on Confidential and Trustworthy Conversational AI. We have developed [BlindChat](https://chat.mithrilsecurity.io/), a privacy-first Conversational AI that ensures your prompts remain confidential, even from us.
While the hallucination detection feature is not yet available in BlindChat, if you are interested in it, you can register here to show your interest in it so we know how to prioritize it and notify you when it is available.
"""
style = theme.Style()
import numpy as np
import pandas as pd
import ast
df = pd.read_csv("./wiki_bio_gpt3_hallucination.csv")
def compute_score_per_document(scores):
scores = ast.literal_eval(scores)
scores = np.array(scores)
return scores.mean()
df["average_score"] = df["sent_scores_nli"].apply(compute_score_per_document)
sorted_df = df.sort_values(by=['average_score'], ascending=False)
THRESHOLD = 0.5
examples = {}
for i in range(3):
sample = sorted_df.iloc[[i]]
examples[f"High hallucination sample {i+1}"] = (sample.index[0] , sample["gpt3_text"].values[0])
sample = sorted_df.iloc[[-(i+1)]]
examples[f"Low hallucination sample {i+1}"] = (sample.index[0] , sample["gpt3_text"].values[0])
def mirror(example):
return examples[example][1]
def evaluate(example, treshold):
index = examples[example][0]
row = sorted_df.loc[index]
scores = ast.literal_eval(row["sent_scores_nli"])
sentences = ast.literal_eval(row["gpt3_sentences"])
annotations = ast.literal_eval(row["annotation"])
predictions = []
labels = []
n = len(sentences)
average_score_predicted = 0.0
average_score_truth = 0.0
for score, sentence, annotation in zip(scores, sentences, annotations):
if score > treshold:
prediction = "hallucination"
average_score_predicted += 1.0
else:
prediction = "factual"
if annotation == "accurate":
annotation = "factual"
else:
annotation = "hallucination"
average_score_truth += 1.0
predictions.append((sentence, prediction))
labels.append((sentence, annotation))
average_score_predicted /= n
average_score_predicted = "{:.0%}".format(average_score_predicted)
average_score_truth /= n
average_score_truth = "{:.0%}".format(average_score_truth)
return average_score_predicted, predictions, labels, average_score_truth
with gr.Blocks(theme=style) as demo:
with gr.Tab("Maya"):
gr.Markdown(title)
gr.Markdown(description)
with gr.Row():
with gr.Column():
examples_dropdown = gr.Dropdown(choices=list(examples.keys()), value=list(examples.keys())[0],
interactive=True,
label="Samples",
info="""You can choose among high/low hallucinations examples from Wiki Bio.
More samples are available below.""")
example_text = gr.TextArea(value=list(examples.values())[0][1])
with gr.Accordion("Detection threshold", open=False):
treshold = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=THRESHOLD, label="Detection threshold", info="""The threshold used to detect hallucinations.
A sentence is flagged as hallucination when inconsistency (SelfCheckGPT NLI) score is above threshold.
Higher threshold increases precision (flagged hallucination actually being an hallucination) but reduces recall (percentage of hallucinations flagged).""")
submit = gr.Button("Check hallucination", variant="primary")
with gr.Column():
label = gr.Label(label="Percentage of document flagged as hallucination")
highlighted_prediction = gr.HighlightedText(
label="Hallucination detection",
combine_adjacent=True,
color_map={"hallucination": "red", "factual": "green"},
show_legend=True)
with gr.Accordion("Ground truth", open=False):
gr.Markdown("Ground truth label manually annotated by humans. You can use that to compare the hallucination detection with the ground truth.")
label_ground_truth = gr.Label(label="Percentage of document actually hallucinations")
highlighted_ground_truth = gr.HighlightedText(
label="Ground truth",
combine_adjacent=True,
color_map={"hallucination": "red", "factual": "green"},
show_legend=True)
examples_dropdown.input(mirror, inputs=examples_dropdown, outputs=example_text)
submit.click(evaluate, inputs=[examples_dropdown, treshold], outputs=[label, highlighted_prediction, highlighted_ground_truth, label_ground_truth])
theme=gr.themes.Base()
demo.launch(debug=True)