Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import gradio as gr
|
3 |
+
import nltk
|
4 |
+
|
5 |
+
from nltk.tokenize import sent_tokenize
|
6 |
+
import torch
|
7 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
|
11 |
+
nltk.download("punkt")
|
12 |
+
nltk.download('punkt_tab')
|
13 |
+
|
14 |
+
model_name = "MoritzLaurer/DeBERTa-v3-large-mnli-fever-anli-ling-wanli" #"MoritzLaurer/DeBERTa-v3-base-mnli-fever-docnli-ling-2c"
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
16 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
17 |
+
|
18 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "CPU")
|
19 |
+
|
20 |
+
labels = ["entailment", "neutral", "contradiction"]
|
21 |
+
|
22 |
+
|
23 |
+
def nli(hypothesis, premise):
|
24 |
+
inputs = tokenizer(premise, hypothesis, return_tensors="pt", truncation=True, max_length=512)
|
25 |
+
logits = model(**inputs).logits[0]
|
26 |
+
probs = torch.softmax(logits, -1).tolist()
|
27 |
+
return dict(zip(labels, probs))
|
28 |
+
|
29 |
+
|
30 |
+
def get_labels(result):
|
31 |
+
if result["entailment"]> result["neutral"] and result["entailment"]> result["contradiction"]:
|
32 |
+
return "entailment"
|
33 |
+
|
34 |
+
elif result["entailment"]<result["neutral"] and result["contradiction"]<result["neutral"]:
|
35 |
+
return "neutral"
|
36 |
+
|
37 |
+
else:
|
38 |
+
return "contradiction"
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
def detect_hallucinations(generated_text, source_text):
|
45 |
+
"""
|
46 |
+
Detect intrinsic and extrinsic hallucinations in the generated text.
|
47 |
+
"""
|
48 |
+
|
49 |
+
|
50 |
+
generated_sentences = sent_tokenize(generated_text)
|
51 |
+
source_sentences = sent_tokenize(source_text)
|
52 |
+
|
53 |
+
intrinsic = []
|
54 |
+
extrinsic = []
|
55 |
+
|
56 |
+
correct_sents = []
|
57 |
+
|
58 |
+
for i in range(len(generated_sentences)):
|
59 |
+
for j in range(len(source_sentences)):
|
60 |
+
|
61 |
+
# result = nli([generated_sentences[i], source_sentences[j]])[0]
|
62 |
+
prediction = nli(generated_sentences[i], source_sentences[j])
|
63 |
+
label = get_labels(prediction)
|
64 |
+
score = prediction[label]
|
65 |
+
|
66 |
+
result = {"label": label, "score": score}
|
67 |
+
|
68 |
+
if result['label'].lower() == "contradiction":
|
69 |
+
intrinsic.append({
|
70 |
+
"generated_sentence": generated_sentences[i],
|
71 |
+
"source_sentence": source_sentences[j],
|
72 |
+
"contradiction_score": result['score']
|
73 |
+
})
|
74 |
+
|
75 |
+
elif result['label'].lower() == "entailment":
|
76 |
+
correct_sents.append(generated_sentences[i])
|
77 |
+
break
|
78 |
+
|
79 |
+
|
80 |
+
if result['label'].lower() == "neutral" and generated_sentences[i] not in correct_sents:
|
81 |
+
extrinsic.append({
|
82 |
+
"claim": generated_sentences[i],
|
83 |
+
"source_sentence": source_sentences[j],
|
84 |
+
"status": "not_supported",
|
85 |
+
"confidence": result['score']
|
86 |
+
})
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
return {
|
91 |
+
"intrinsic": intrinsic,
|
92 |
+
"extrinsic": extrinsic
|
93 |
+
}
|
94 |
+
|
95 |
+
def gradio_interface(generated_text, source_text):
|
96 |
+
result = detect_hallucinations(generated_text, source_text)
|
97 |
+
return result
|
98 |
+
|
99 |
+
theme = gr.themes.Soft(primary_hue="teal", secondary_hue="blue", neutral_hue="gray").set(
|
100 |
+
body_text_color="*neutral_900",
|
101 |
+
block_label_text_color="*neutral_900",
|
102 |
+
block_title_text_color="*neutral_900"
|
103 |
+
)
|
104 |
+
|
105 |
+
|
106 |
+
|
107 |
+
custom_css = """
|
108 |
+
.gradio-container { background-color: #ffffff !important; }
|
109 |
+
.gradio-json { font-family: 'Fira Code', monospace; font-size: 14px; color: #1f2937 !important; }
|
110 |
+
#header_text {
|
111 |
+
color: #111 !important;
|
112 |
+
"""
|
113 |
+
|
114 |
+
|
115 |
+
dark_css = """
|
116 |
+
.gradio-container {
|
117 |
+
background-color: #000 !important;
|
118 |
+
color: #eee !important;
|
119 |
+
}
|
120 |
+
.gradio-container .gr-block {
|
121 |
+
background-color: #000 !important;
|
122 |
+
}
|
123 |
+
.gradio-container textarea, .gradio-container input {
|
124 |
+
background-color: #111 !important;
|
125 |
+
color: #eee !important;
|
126 |
+
}
|
127 |
+
.gradio-json {
|
128 |
+
background-color: #111 !important;
|
129 |
+
color: #eee !important;
|
130 |
+
}
|
131 |
+
#header_text {
|
132 |
+
color: #eee !important;
|
133 |
+
}
|
134 |
+
"""
|
135 |
+
|
136 |
+
demo = gr.Blocks(theme=theme, css=dark_css)
|
137 |
+
|
138 |
+
with demo:
|
139 |
+
gr.Markdown("#Hallucination Detector", elem_id="header_text")
|
140 |
+
gr.Markdown(
|
141 |
+
"Detects **intrinsic** (internal contradictions) and **extrinsic** "
|
142 |
+
"(source unsupported) hallucinations",
|
143 |
+
elem_id="header_text"
|
144 |
+
)
|
145 |
+
gen = gr.Textbox(lines=8, label="Generated Text")
|
146 |
+
src = gr.Textbox(lines=8, label="Source Text")
|
147 |
+
out = gr.JSON(label="🔍 Analysis Result (JSON)")
|
148 |
+
|
149 |
+
gen.submit(detect_hallucinations, inputs=[gen, src], outputs=out)
|
150 |
+
src.submit(detect_hallucinations, inputs=[gen, src], outputs=out)
|
151 |
+
gr.Button("Run Analysis").click(detect_hallucinations, inputs=[gen, src], outputs=out)
|
152 |
+
|
153 |
+
demo.launch()
|
154 |
+
|
155 |
+
|
156 |
+
|
157 |
+
|