File size: 3,115 Bytes
e252480
e108d52
26968e1
611eb64
c21b03e
 
 
 
e108d52
e2fe7c7
 
 
 
2940078
 
c21b03e
e2fe7c7
e108d52
 
 
 
 
 
 
 
 
 
 
 
97a58f0
e108d52
 
 
4a040df
 
e108d52
 
 
 
f980ac6
 
e108d52
611eb64
4a040df
 
 
 
611eb64
 
5890d08
 
611eb64
 
 
 
 
 
 
 
 
 
 
 
7d8e226
611eb64
 
 
 
 
 
 
 
 
4a040df
 
611eb64
 
c963472
611eb64
 
f980ac6
 
611eb64
 
f980ac6
84c9a1d
f980ac6
611eb64
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

import streamlit as st
from PIL import Image
import spacy
import streamlit as st
from streamlit_pdf_viewer import pdf_viewer



st.set_page_config(page_title="FACTOID: FACtual enTailment fOr hallucInation Detection", layout="wide")
st.title('Welcome to :blue[FACTOID] ') 

st.header('FACTOID: FACtual enTailment fOr hallucInation Detection :blue[Web Demo]')
#image = Image.open('image.png')
#st.image(image, caption='Traditional Entailment vs Factual Entailment')
pdf_viewer(input="fac.pdf", width=700)

# List of sentences
sentence1 = [f"U.S. President Barack Obama declared that the U.S. will refrain from deploying troops in Ukraine."]
sentence2 = [f"Joe Biden said we’d not send U.S. troops to fight Russian troops in Ukraine, but we would provide robust military assistance and try to unify the Western world against Russia’s aggression."]
# Create a dropdown menu
selected_sentence1 = st.selectbox("Select first sentence:", sentence1)
selected_sentence2 = st.selectbox("Select first sentence:", sentence2)

from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")

model_name = "MoritzLaurer/mDeBERTa-v3-base-xnli-multilingual-nli-2mil7"
tokenizer = AutoTokenizer.from_pretrained(model_name,use_fast=False)
model = AutoModelForSequenceClassification.from_pretrained(model_name)


premise = selected_sentence1
hypothesis = selected_sentence2
input = tokenizer(premise, hypothesis, truncation=True, return_tensors="pt")
output = model(input["input_ids"].to(device))  # device = "cuda:0" or "cpu"
prediction = torch.softmax(output["logits"][0], -1).tolist()
label_names = ["support", "neutral", "refute"]
prediction = {name: float(pred) for pred, name in zip(prediction, label_names)}
highest_label = max(prediction, key=prediction.get)


from transformers import pipeline
pipe = pipeline("text-classification",model="sileod/deberta-v3-base-tasksource-nli")
labels=pipe([dict(text=selected_sentence1,
  text_pair=selected_sentence2)])


import en_core_web_sm


def extract_person_names(sentence):
    """
    Extract person names from a sentence using spaCy's named entity recognition.
    
    Parameters:
        sentence (str): Input sentence.
    
    Returns:
        list: List of person names extracted from the sentence.
    """
    # Load English language model
    nlp = spacy.load("en_core_web_sm")
    
    # Process the sentence using spaCy
    doc = nlp(sentence)
    
    # Extract person names
    person_names = [entity.text for entity in doc.ents if entity.label_ == 'PERSON']
    
    return person_names[0]

person_name1 = extract_person_names(selected_sentence1)
person_name2 = extract_person_names(selected_sentence2)


col1, col2 = st.columns(2)

with col1:
    st.write("Without Factual Entailment.")
    st.write("Textual Entailment Model:\n",highest_label)

with col2:
    st.write("With Factual Entailment:")
    st.write("Textual Entailment Model:\n",labels[0]['label'])
    st.write("Span Detection Model:\n")
    st.write(f"{person_name1}::{person_name2}")