Prathamesh1420 commited on
Commit
552d488
·
verified ·
1 Parent(s): 8c8afef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -29
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("vectara/hallucination_evaluation_model", trust_remote_code=True)
 
 
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 (fact-based reference)
15
- def generate_premise(query):
16
- # This can use GPT, external APIs, or predefined logic
17
- # Example using GPT or any fact-retrieval API:
18
- from transformers import AutoModelForCausalLM, AutoTokenizer
19
-
20
- gpt_model = "google/flan-t5-base" # Replace with your preferred model
21
- gpt_tokenizer = AutoTokenizer.from_pretrained(gpt_model)
22
- gpt = AutoModelForCausalLM.from_pretrained(gpt_model)
23
 
24
- # Generate a fact-based premise
25
- prompt = f"Generate a fact-based premise for the query: {query}"
26
- inputs = gpt_tokenizer(prompt, return_tensors="pt")
27
- outputs = gpt.generate(**inputs)
28
- premise = gpt_tokenizer.decode(outputs[0], skip_special_tokens=True)
29
- return premise
30
 
31
  # Streamlit UI
32
  st.title("Hallucination Detection App")
33
- st.write("Enter a query, and the app will tell you if it is hallucinated or accurate based on factual information.")
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 = generate_premise(query)
41
-
42
- # Prepare input for hallucination evaluation
43
- prompt = f"<pad> Determine if the hypothesis is true given the premise?\n\nPremise: {premise}\n\nHypothesis: {query}"
44
- result = classifier([prompt], top_k=None)[0][0]
45
-
46
- # Output results
47
- st.write(f"**Premise:** {premise}")
48
- st.write(f"**Hypothesis:** {query}")
49
- st.write(f"**Result:** {result['label']} (Confidence: {result['score']:.2f})")
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.")