Spaces:
Running
on
Zero
Running
on
Zero
import gradio as gr | |
from huggingface_hub import InferenceClient | |
# Initialize the client | |
client = InferenceClient("zhangchenxu/TinyV-1.5B") | |
LV_PROMPT = """ | |
You are an AI tasked with identifying false negatives in answer verification. A false negative occurs when a model's answer is essentially correct but is marked as incorrect due to minor discrepancies or formatting issues. Your job is to analyze the given question, ground truth answer, and model answer to determine if the model's answer is actually correct despite appearing different from the ground truth. | |
<question>{question}</question> | |
<ground_truth_answer>{ground_truth}</ground_truth_answer> | |
<model_answer>{model_answer}</model_answer> | |
Return "True" if the model's answer is correct, otherwise return "False". | |
""" | |
# Define our example sets | |
EXAMPLES = [ | |
{ | |
"name": "Order-Insensitive", | |
"question": "Determine all real values of $x$ for which $(x+8)^{4}=(2 x+16)^{2}$.", | |
"ground_truth": "-6,-8,-10", | |
"model_answer": "-10, -8, -6", | |
"temp": 0.3, | |
"top_p": 0.95, | |
"tokens": 2 | |
}, | |
{ | |
"name": "Latex Expression", | |
"question": "A bag contains 3 green balls, 4 red balls, and no other balls. Victor removes balls randomly from the bag, one at a time, and places them on a table. Each ball in the bag is equally likely to be chosen each time that he removes a ball. He stops removing balls when there are two balls of the same colour on the table. What is the probability that, when he stops, there is at least 1 red ball and at least 1 green ball on the table?", | |
"ground_truth": "$\\frac{4}{7}$", | |
"model_answer": "4/7", | |
"temp": 0.3, | |
"top_p": 0.95, | |
"tokens": 2 | |
}, | |
{ | |
"name": "Variable Labeling", | |
"question": "If $T=x^{2}+\\frac{1}{x^{2}}$, determine the values of $b$ and $c$ so that $x^{6}+\\frac{1}{x^{6}}=T^{3}+b T+c$ for all non-zero real numbers $x$.", | |
"ground_truth": "-3,0", | |
"model_answer": "b=-3, c=0", | |
"temp": 0.3, | |
"top_p": 0.95, | |
"tokens": 2 | |
}, | |
{ | |
"name": "Paraphrase", | |
"question": "Peter has 8 coins, of which he knows that 7 are genuine and weigh the same, while one is fake and differs in weight, though he does not know whether it is heavier or lighter. Peter has access to a balance scale, which shows which side is heavier but not by how much. For each weighing, Peter must pay Vasya one of his coins before the weighing. If Peter pays with a genuine coin, Vasya will provide an accurate result; if a fake coin is used, Vasya will provide a random result. Peter wants to determine 5 genuine coins and ensure that none of these genuine coins are given to Vasya. Can Peter guaranteedly achieve this?", | |
"ground_truth": "Petya can guarantee finding 5 genuine coins.", | |
"model_answer": "Yes, Peter can guarantee finding 5 genuine coins while ensuring that none of these genuine coins are paid to Vasya.", | |
"temp": 0.3, | |
"top_p": 0.95, | |
"tokens": 2 | |
}, | |
{ | |
"name": "False Example", | |
"question": "What is the tallest mountain in the world?", | |
"ground_truth": "Mount Everest is the tallest mountain in the world.", | |
"model_answer": "K2 is the tallest mountain on Earth.", | |
"temp": 0.3, | |
"top_p": 0.95, | |
"tokens": 2 | |
} | |
] | |
def verify_answer(question, ground_truth, model_answer, temperature, top_p, max_tokens): | |
prompt = LV_PROMPT.format( | |
question=question, | |
ground_truth=ground_truth, | |
model_answer=model_answer | |
) | |
messages = [{"role": "user", "content": prompt}] | |
response_text = "" | |
try: | |
for message in client.chat_completion( | |
messages, | |
max_tokens=max_tokens, | |
stream=True, | |
temperature=temperature, | |
top_p=top_p, | |
): | |
token = message.choices[0].delta.content | |
if token: | |
response_text += token | |
yield response_text | |
except Exception as e: | |
yield f"Error: {str(e)}" | |
def load_example(example_index): | |
example = EXAMPLES[example_index] | |
return ( | |
example["question"], | |
example["ground_truth"], | |
example["model_answer"], | |
example["temp"], | |
example["top_p"], | |
example["tokens"] | |
) | |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo: | |
gr.Markdown("## π§ TinyV - Answer Verification Tool\nThis tool verifies model-generated answers for correctness.") | |
with gr.Row(): | |
with gr.Column(scale=1): | |
question = gr.Textbox(lines=3, label="π Question") | |
ground_truth = gr.Textbox(lines=3, label="β Ground Truth Answer") | |
model_answer = gr.Textbox(lines=3, label="π€ Model Answer") | |
gr.Markdown("### π Try Examples:") | |
example_buttons = [] | |
with gr.Row(): | |
for i, ex in enumerate(EXAMPLES): | |
btn = gr.Button(ex["name"], size="sm") | |
btn.click( | |
fn=lambda idx=i: load_example(idx), | |
outputs=[question, ground_truth, model_answer, temperature, top_p, max_tokens] | |
) | |
example_buttons.append(btn) | |
with gr.Column(scale=1): | |
with gr.Accordion("βοΈ Advanced Settings", open=False): | |
temperature = gr.Slider(0, 1, value=0.3, step=0.1, label="Temperature") | |
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p") | |
max_tokens = gr.Slider(1, 128, value=2, step=1, label="Max Tokens") | |
verify_btn = gr.Button("β Verify Answer", variant="primary") | |
result = gr.Textbox(label="π§Ύ Verification Result", lines=5, placeholder="Result will appear here...") | |
verify_btn.click( | |
fn=verify_answer, | |
inputs=[question, ground_truth, model_answer, temperature, top_p, max_tokens], | |
outputs=result | |
) | |
# Launch the app | |
demo.queue() | |
if __name__ == "__main__": | |
demo.launch() |