import os import re from typing import List, Tuple, Union from pathlib import Path import gradio as gr import openai HF_TOKEN = os.environ.get("HF_TOKEN", None) LEPTON_API_TOKEN = os.environ.get("LEPTON_API_TOKEN", None) # client=openai.OpenAI( # base_url="https://yb15a7dy-patronus-lynx-8b-v1-1.tin.lepton.run/api/v1/", # api_key=LEPTON_API_TOKEN # ) # client=openai.OpenAI( # base_url="https://yb15a7dy-lynx-70b.tin.lepton.run/api/v1/", # api_key=LEPTON_API_TOKEN # ) PROMPT = """ Given the following QUESTION, DOCUMENT and ANSWER you must analyze the provided answer and determine whether it is faithful to the contents of the DOCUMENT. The ANSWER must not offer new information beyond the context provided in the DOCUMENT. The ANSWER also must not contradict information provided in the DOCUMENT. Output your final verdict by strictly following this format: "PASS" if the answer is faithful to the DOCUMENT and "FAIL" if the answer is not faithful to the DOCUMENT. Show your reasoning. -- QUESTION (THIS DOES NOT COUNT AS BACKGROUND INFORMATION): {question} -- DOCUMENT: {document} -- ANSWER: {answer} -- Your output should be in JSON FORMAT with the keys "REASONING" and "SCORE": {{"REASONING": , "SCORE": }} """ css = """ @import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;700&display=swap'); body, .gradio-container { font-family: 'Plus Jakarta Sans', sans-serif !important; } """ formatted_upload_svg = ''' f
''' HEADER = """ # Patronus Lynx Demo
**Patronus Lynx** is a state-of-the-art open-source model for hallucination detection. **Getting Started**: Provide a question and document or context given to your model in addition to the answer given by the model and then click submit. The output panel will indicate whether the reponse is a hallucination (Fail) or if it is faithful to the given document or context (Pass) through the score Pass or Fail and provide reasoning behind the score. """ def update_client_base_url(model_name): if model_name == "Patronus Lynx 8B v1.1": return "https://yb15a7dy-patronus-lynx-8b-v1-1.tin.lepton.run/api/v1/" elif model_name == "Patronus Lynx 70B": return "https://yb15a7dy-lynx-70b.tin.lepton.run/api/v1/" def parse_patronus_lynx_response( response: str, ) -> Tuple[bool, Union[List[str], None]]: """ Parses the response from the Patronus Lynx LLM and returns a tuple of: - Whether the response is hallucinated or not. - A reasoning trace explaining the decision. """ # Default to hallucinated hallucination, reasoning = True, None reasoning_pattern = r'"REASONING":\s*\[(.*?)\]' score_pattern = r'"SCORE":\s*"?\b(PASS|FAIL)\b"?' reasoning_match = re.search(reasoning_pattern, response, re.DOTALL) score_match = re.search(score_pattern, response) if score_match: score = score_match.group(1) if score == "PASS": hallucination = False if reasoning_match: reasoning_content = reasoning_match.group(1) reasoning = re.split(r"['\"],\s*['\"]", reasoning_content) return hallucination, reasoning def model_call(question, document, answer, client_base_url): client = openai.OpenAI( base_url=client_base_url, api_key=LEPTON_API_TOKEN ) print("CLIENT AND CLIENT BASE URL", client, client_base_url) if question == "" or document == "" or answer == "": return "", "" NEW_FORMAT = PROMPT.format(question=question, document=document, answer=answer) print("ENTIRE NEW_FORMAT", NEW_FORMAT) response = client.completions.create( model="gpt-3.5-turbo-instruct", prompt=NEW_FORMAT, temperature=0.0 ) print("RESPONSE FROM CLIENT:", response) hallucination, reasoning = parse_patronus_lynx_response(response.choices[0].text) score = "FAIL" if hallucination else "PASS" combined_reasoning = " ".join(reasoning)[1:-1] return combined_reasoning, score def upload_file(filepath): name = Path(filepath).name return [gr.UploadButton(visible=False), gr.DownloadButton(label=f"Download {name}", value=filepath, visible=True)] def download_file(): return [gr.UploadButton(visible=True), gr.DownloadButton(visible=False)] # inputs = [ # gr.Textbox(label="Question"), # gr.Textbox(label="Document"), # gr.Textbox(label="Answer") # ] # outputs = [ # gr.Textbox(label="Reasoning"), # gr.Textbox(label="Score") # ] with gr.Blocks(css=css) as demo: base_url_state = gr.State(update_client_base_url("Patronus Lynx 8B v1.1")) gr.Markdown(HEADER) # gr.Interface(fn=model_call, inputs=inputs, outputs=outputs) model_dropdown = gr.Dropdown(choices=["Patronus Lynx 8B v1.1", "Patronus Lynx 70B"], value="Patronus Lynx 8B v1.1", label="Model", interactive=True) with gr.Row(): with gr.Column(scale=1): with gr.Row(): question = gr.Textbox(label="Question", scale=9) u = gr.UploadButton(formatted_upload_svg, file_count="single", scale=1) d = gr.DownloadButton("Download the file", visible=False, scale=1) with gr.Row(): with gr.Column(scale=1): document = gr.Textbox(label="Document") with gr.Row(): with gr.Column(scale=1): answer = gr.Textbox(label="Answer") with gr.Row(): clear_btn = gr.ClearButton([question, document, answer]) submit_button = gr.Button("Submit") with gr.Column(scale=1): reasoning = gr.Textbox(label="Reasoning") score = gr.Textbox(label="Score (FAIL if Hallucinated, PASS if not)") model_dropdown.change(fn=update_client_base_url, inputs=[model_dropdown], outputs=[base_url_state]) u.upload(upload_file, u, [u, d]) d.click(download_file, None, [u, d]) submit_button.click(fn=model_call, inputs=[question, document, answer, base_url_state], outputs=[reasoning, score]) question.submit(fn=model_call, inputs=[question, document, answer, base_url_state], outputs=[reasoning, score]) document.submit(fn=model_call, inputs=[question, document, answer, base_url_state], outputs=[reasoning, score]) answer.submit(fn=model_call, inputs=[question, document, answer, base_url_state], outputs=[reasoning, score]) demo.launch()