Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,11 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
|
|
3 |
|
4 |
# Load the hallucination evaluation model
|
5 |
-
model = AutoModelForSequenceClassification.from_pretrained(
|
|
|
|
|
6 |
tokenizer = AutoTokenizer.from_pretrained('google/flan-t5-base')
|
7 |
classifier = pipeline(
|
8 |
"text-classification",
|
@@ -11,40 +14,39 @@ classifier = pipeline(
|
|
11 |
trust_remote_code=True
|
12 |
)
|
13 |
|
14 |
-
# Function to generate a premise
|
15 |
-
def
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
gpt_tokenizer = AutoTokenizer.from_pretrained(gpt_model)
|
22 |
-
gpt = AutoModelForCausalLM.from_pretrained(gpt_model)
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
return premise
|
30 |
|
31 |
# Streamlit UI
|
32 |
st.title("Hallucination Detection App")
|
33 |
-
st.write("Enter a query, and the app will
|
34 |
|
35 |
# Input query from user
|
36 |
query = st.text_input("Enter your query:")
|
37 |
|
38 |
if query:
|
39 |
-
# Generate a premise for the query
|
40 |
-
premise =
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import wikipediaapi
|
4 |
|
5 |
# Load the hallucination evaluation model
|
6 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
7 |
+
"vectara/hallucination_evaluation_model", trust_remote_code=True
|
8 |
+
)
|
9 |
tokenizer = AutoTokenizer.from_pretrained('google/flan-t5-base')
|
10 |
classifier = pipeline(
|
11 |
"text-classification",
|
|
|
14 |
trust_remote_code=True
|
15 |
)
|
16 |
|
17 |
+
# Function to generate a premise using Wikipedia
|
18 |
+
def generate_premise_wikipedia(query):
|
19 |
+
"""
|
20 |
+
Retrieves the most relevant Wikipedia summary as a premise for the given query.
|
21 |
+
"""
|
22 |
+
wiki_wiki = wikipediaapi.Wikipedia('en') # English Wikipedia
|
23 |
+
search_results = wiki_wiki.page(query) # Search Wikipedia for the query
|
|
|
|
|
24 |
|
25 |
+
if search_results.exists():
|
26 |
+
# If the page exists, return the summary as the premise
|
27 |
+
return search_results.summary
|
28 |
+
else:
|
29 |
+
return "No relevant information found on Wikipedia."
|
|
|
30 |
|
31 |
# Streamlit UI
|
32 |
st.title("Hallucination Detection App")
|
33 |
+
st.write("Enter a query, and the app will check if it is hallucinated or factual using Wikipedia as a factual source.")
|
34 |
|
35 |
# Input query from user
|
36 |
query = st.text_input("Enter your query:")
|
37 |
|
38 |
if query:
|
39 |
+
# Generate a premise for the query using Wikipedia
|
40 |
+
premise = generate_premise_wikipedia(query)
|
41 |
+
|
42 |
+
if premise != "No relevant information found on Wikipedia.":
|
43 |
+
# Prepare input for hallucination evaluation
|
44 |
+
prompt = f"<pad> Determine if the hypothesis is true given the premise?\n\nPremise: {premise}\n\nHypothesis: {query}"
|
45 |
+
result = classifier([prompt], top_k=None)[0][0]
|
46 |
+
|
47 |
+
# Output results
|
48 |
+
st.write(f"**Premise (from Wikipedia):** {premise}")
|
49 |
+
st.write(f"**Hypothesis:** {query}")
|
50 |
+
st.write(f"**Result:** {result['label']} (Confidence: {result['score']:.2f})")
|
51 |
+
else:
|
52 |
+
st.write("**Premise:** No relevant information found on Wikipedia.")
|