|
|
|
import gradio as gr |
|
import requests |
|
import os |
|
|
|
def check_hallucination(assertion, citation): |
|
api_url = "https://api-inference.huggingface.co/models/vectara/hallucination_evaluation_model" |
|
header = {"Authorization": f"Bearer {hf_token}"} |
|
payload = {"inputs": f"{assertion} [SEP] {citation}"} |
|
|
|
response = requests.post(api_url, headers=header, json=payload, timeout=120) |
|
output = response.json() |
|
output = output[0][0]["score"] |
|
|
|
return f"**hallucination score:** {output}" |
|
|
|
|
|
|
|
vapi_url = "https://api-inference.huggingface.co/models/vectara/hallucination_evaluation_model" |
|
|
|
headers = {"Authorization": f"Bearer {hf_token}"} |
|
|
|
|
|
|
|
def query(payload): |
|
response = requests.post(vapi_url, headers=headers, json=payload) |
|
return response.json() |
|
|
|
|
|
|
|
def evaluate_hallucination(input1, input2): |
|
|
|
combined_input = f"{input1}. {input2}" |
|
|
|
|
|
output = query({"inputs": combined_input}) |
|
|
|
|
|
score = output[0][0]['score'] |
|
|
|
|
|
if score < 0.5: |
|
label = f"🔴 High risk. Score: {score:.2f}" |
|
else: |
|
label = f"🟢 Low risk. Score: {score:.2f}" |
|
|
|
return label |
|
|
|
def query_vectara(text): |
|
user_message = text |
|
|
|
|
|
customer_id = os.getenv('CUSTOMER_ID') |
|
corpus_id = os.getenv('CORPUS_ID') |
|
api_key = os.getenv('API_KEY') |
|
|
|
|
|
api_key_header = { |
|
"customer-id": customer_id, |
|
"x-api-key": api_key |
|
} |
|
|
|
|
|
request_body = { |
|
"query": [ |
|
{ |
|
"query": user_message, |
|
"queryContext": "", |
|
"start": 1, |
|
"numResults": 25, |
|
"contextConfig": { |
|
"charsBefore": 0, |
|
"charsAfter": 0, |
|
"sentencesBefore": 2, |
|
"sentencesAfter": 2, |
|
"startTag": "%START_SNIPPET%", |
|
"endTag": "%END_SNIPPET%", |
|
}, |
|
"rerankingConfig": { |
|
"rerankerId": 272725718, |
|
"mmrConfig": { |
|
"diversityBias": 0.35 |
|
} |
|
}, |
|
"corpusKey": [ |
|
{ |
|
"customerId": customer_id, |
|
"corpusId": corpus_id, |
|
"semantics": 0, |
|
"metadataFilter": "", |
|
"lexicalInterpolationConfig": { |
|
"lambda": 0 |
|
}, |
|
"dim": [] |
|
} |
|
], |
|
"summary": [ |
|
{ |
|
"maxSummarizedResults": 5, |
|
"responseLang": "auto", |
|
"summarizerPromptName": "vectara-summary-ext-v1.2.0" |
|
} |
|
] |
|
} |
|
] |
|
} |
|
|
|
|
|
response = requests.post( |
|
"https://api.vectara.io/v1/query", |
|
json=request_body, |
|
verify=True, |
|
headers=api_key_header |
|
) |
|
|
|
if response.status_code == 200: |
|
query_data = response.json() |
|
if query_data: |
|
sources_info = [] |
|
|
|
|
|
summary = query_data['responseSet'][0]['summary'][0]['text'] |
|
|
|
|
|
for response_set in query_data.get('responseSet', []): |
|
|
|
|
|
for source in response_set.get('response', [])[:5]: |
|
source_metadata = source.get('metadata', []) |
|
source_info = {} |
|
|
|
for metadata in source_metadata: |
|
metadata_name = metadata.get('name', '') |
|
metadata_value = metadata.get('value', '') |
|
|
|
if metadata_name == 'title': |
|
source_info['title'] = metadata_value |
|
elif metadata_name == 'author': |
|
source_info['author'] = metadata_value |
|
elif metadata_name == 'pageNumber': |
|
source_info['page number'] = metadata_value |
|
|
|
if source_info: |
|
sources_info.append(source_info) |
|
|
|
result = {"summary": summary, "sources": sources_info} |
|
return f"{json.dumps(result, indent=2)}" |
|
else: |
|
return "No data found in the response." |
|
else: |
|
return f"Error: {response.status_code}" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=evaluate_hallucination, |
|
inputs=[gr.Textbox(label="Assertion"), gr.Textbox(label="Citation")], |
|
outputs=[gr.Label(), gr.Textbox(label="Explanation")], |
|
live=False, |
|
title="👋🏻Welcome to 🌟Tonic's 🧠🌈Hallucination Tester 🔴🟢", |
|
description="How To Use 🌈Hallucination tester: 🗣️📝add any assertion from an LLM or a human 🗣️😷 add any citation from a RAG retriever or a source 👇🏻📩 Press send 🔴red means a 🌈hallucination, 🟢 green means a 🧠credible assertion. Check out the model [vectara/hallucination_evaluation_model](https://huggingface.co/vectara/hallucination_evaluation_model) You can also use 🥒🍆🫑Vectara - Hallucination Tester 🗣️😷 via API below or way by cloning this space. 🧬🔬🔍 Simply click here: Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community 👻 [](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to 🌟 [DataTonic](https://github.com/Tonic-AI/DataTonic) 🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗", |
|
theme='ParityError/Anime', |
|
) |
|
|
|
|
|
iface.launch() |