Spaces:
Running
Running
File size: 12,106 Bytes
31fda98 061ae36 31fda98 06d4d61 37aa22e 31fda98 06826f1 81602d3 31fda98 06826f1 061ae36 06826f1 061ae36 06826f1 06d4d61 061ae36 06d4d61 06826f1 06d4d61 31fda98 06d4d61 31fda98 06d4d61 31fda98 06d4d61 31fda98 06d4d61 31fda98 06d4d61 31fda98 06d4d61 31fda98 06d4d61 81602d3 b9ee4b2 81602d3 b9ee4b2 81602d3 31fda98 06826f1 81602d3 b9ee4b2 81602d3 b9ee4b2 81602d3 b9ee4b2 81602d3 31fda98 b9ee4b2 81602d3 06d4d61 31fda98 81602d3 b9ee4b2 06d4d61 81602d3 06d4d61 81602d3 06d4d61 81602d3 b9ee4b2 81602d3 b9ee4b2 81602d3 06d4d61 81602d3 06d4d61 31fda98 06d4d61 b9ee4b2 06d4d61 31fda98 06d4d61 31fda98 06d4d61 81602d3 06d4d61 31fda98 06d4d61 81602d3 31fda98 81602d3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
import gradio as gr
import spaces
import pandas as pd
from typing import List, Dict, Tuple
from flow_judge import Hf, FlowJudge, EvalInput
from flow_judge.metrics import CustomMetric, RubricItem
from huggingface_hub import snapshot_download
from flow_judge.models.huggingface import Hf
from examples import get_examples
MODEL_NAME = "flowaicom/Flow-Judge-v0.1"
def download_model():
try:
print(f"Downloading model {MODEL_NAME}...")
snapshot_download(repo_id=MODEL_NAME)
print(f"Model {MODEL_NAME} downloaded to default Hugging Face cache")
return True
except Exception as e:
raise RuntimeError(f"Failed to download model {MODEL_NAME}: {e}")
@spaces.GPU
def evaluate(
inputs_task: List[Dict[str, str]],
output_name: str,
output_value: str,
evaluation_criteria: str,
rubric_items: List[Dict[str, str]]
) -> Tuple[str, int]:
# [{'name': 'a', 'value': 'a'}]
try:
model = Hf(flash_attn=False)
except Exception as e:
raise RuntimeError(f"Failed to initialize Hf Model: {e}")
eval_input = EvalInput(
inputs=[{input['name']: input['value']} for input in inputs_task],
output={output_name: output_value}
)
score_rubric_items = [
RubricItem(
score=int(rubric_item['name']),
description=rubric_item['value']
)
for rubric_item in rubric_items
]
custom_metric = CustomMetric(
name="custom-metric",
criteria=evaluation_criteria,
rubric=score_rubric_items,
required_inputs=[input['name'] for input in inputs_task],
required_output=output_name
)
judge = FlowJudge(model=model, metric=custom_metric)
try:
result = judge.evaluate(eval_input)
except Exception as e:
raise RuntimeError(f"Failed to evaluate: {e}")
return result.feedback, result.score
def reset_all():
return (
[], "", "", [], "", "", # Existing resets for inputs and rubrics
"", "", "", "", "" # New resets for additional fields
)
# Define presets
EXAMPLES = get_examples()
IMAGE_PATH = "./img/flow_judge_banner.png"
HEADER = """<h1 align="center" style="font-family: 'Courier New', Courier, monospace;">Flow Judge Demo</h1>
<p align="center" style="font-family: 'Courier New', Courier, monospace;">
<strong>
<a href="https://www.flow-ai.com/judge">Technical Report</a> |
<a href="https://huggingface.co/collections/flowaicom/flow-judge-v01-66e6af5fc3b3a128bde07dec">Model Weights</a> |
<a href="https://github.com/flowaicom/lm-evaluation-harness/tree/Flow-Judge-v0.1_evals/lm_eval/tasks/flow_judge_evals">Evaluation Code</a> |
<a href="https://github.com/flowaicom/flow-judge/tree/main/examples">Tutorials</a>
</strong>
</p>
<p align="center" style="font-family: 'Courier New', Courier, monospace;">
<code>flow-judge</code> is a lightweight library for evaluating LLM applications with <code>Flow-Judge-v0.1</code>.
</p>"""
with gr.Blocks() as demo:
model_downloaded = download_model()
with gr.Row(equal_height=False):
with gr.Column(scale=2):
gr.Image(IMAGE_PATH, show_label=False, interactive=False, show_share_button=False, show_fullscreen_button=False, show_download_button=False)
with gr.Column(scale=3):
gr.HTML(HEADER)
gr.Markdown("# ⚡ **Quickstart Examples**")
with gr.Row():
with gr.Column(scale=1):
preset_buttons = [gr.Button(example["description"]) for example in EXAMPLES[:len(EXAMPLES)//3]]
with gr.Column(scale=1):
preset_buttons += [gr.Button(example["description"]) for example in EXAMPLES[len(EXAMPLES)//3:2*len(EXAMPLES)//3]]
with gr.Column(scale=1):
preset_buttons += [gr.Button(example["description"]) for example in EXAMPLES[2*len(EXAMPLES)//3:]]
with gr.Row(equal_height=False):
with gr.Column(scale=1):
gr.Markdown("## **Evaluation task inputs**")
gr.Markdown("*<span style='color: gray;'>Define the input names and values for the evaluation task. Inputs are optional if evaluation depends on the output only.</span>*")
with gr.Group():
inputs_task = gr.State([])
new_input_name = gr.Textbox(label="Name")
new_input_value = gr.Textbox(label="Value")
def add_input(inputs_task, new_input_name, new_input_value):
return inputs_task + [{"name": new_input_name, "value": new_input_value}], "", ""
@gr.render(inputs=inputs_task) # You have to pass the state here
def render_inputs(inputs_list): # Use different name than the state variable
for input in inputs_list:
with gr.Group():
with gr.Row(equal_height=True):
with gr.Column(min_width=60, scale=2):
gr.Textbox(input['name'], label="Name", show_label=True, interactive=False, autoscroll=False, max_lines=1)
with gr.Column(scale=8):
gr.Textbox(input['value'], label="Value", show_label=True, interactive=False, autoscroll=False, max_lines=3)
with gr.Column(min_width=15, scale=1):
delete_btn = gr.Button("X", size="lg", variant="secondary")
def delete(input=input):
inputs_list.remove(input)
return inputs_list
delete_btn.click(delete, None, [inputs_task]) # This is the state variable
gr.Button("Add Input").click(
add_input,
[inputs_task, new_input_name, new_input_value],
[inputs_task, new_input_name, new_input_value]
)
with gr.Column(scale=1):
gr.Markdown("## **Evaluation task output**")
gr.Markdown("*<span style='color: gray;'>Define the output name and value for the evaluation task. Output is always required.</span>*")
with gr.Group():
with gr.Row(equal_height=True):
with gr.Column(min_width=60, scale=2):
output_name = gr.Textbox(label="Name", show_label=True, interactive=True, autoscroll=False, max_lines=1)
with gr.Column(scale=9):
output_value = gr.Textbox(label="Value", show_label=True, interactive=True, autoscroll=False, max_lines=3)
with gr.Column(scale=1):
gr.Markdown("## **Evaluation criteria and rubric**")
gr.Markdown("*<span style='color: gray;'>Define the evaluation criteria and rubric for the evaluation task. Supported scoring scales: Binary (0 and 1), 3-Likert and 5-Likert.</span>*\n\n*<span style='color: gray;'>❗You can experiment with other scoring scales. However, performance may vary.</span>*")
with gr.Row():
with gr.Column(scale=1):
rubric_items = gr.State([])
new_rubric_name = gr.Textbox(label="Score", show_label=True, interactive=True, autoscroll=False, max_lines=1)
new_rubric_value = gr.Textbox(label="Description", show_label=True, interactive=True, autoscroll=False, max_lines=3)
def add_rubric_item(rubric_items, new_rubric_name, new_rubric_value):
return rubric_items + [{"name": new_rubric_name, "value": new_rubric_value}], "", ""
@gr.render(inputs=rubric_items) # You have to pass the state here
def render_rubrics(rubric_items_list): # Use different name than the state variable
for rubric_item in rubric_items_list:
with gr.Group():
with gr.Row(equal_height=True):
with gr.Column(min_width=30, scale=1):
gr.Textbox(rubric_item['name'], label="Score", show_label=True, interactive=False)
with gr.Column(scale=8):
gr.Textbox(rubric_item['value'], label="Description", show_label=True, interactive=False)
with gr.Column(min_width=15, scale=1):
delete_btn = gr.Button("X", size="lg", variant="secondary")
def delete(rubric_item=rubric_item):
rubric_items_list.remove(rubric_item)
return rubric_items_list
delete_btn.click(delete, None, [rubric_items]) # This is the state variable
gr.Button("Add Rubric Item").click(
add_rubric_item,
[rubric_items, new_rubric_name, new_rubric_value],
[rubric_items, new_rubric_name, new_rubric_value]
)
with gr.Column(scale=1):
evaluation_criteria = gr.Textbox(label="Evaluation criteria")
with gr.Row():
with gr.Column(scale=1, variant="panel"):
gr.Markdown("# **Evaluation**")
with gr.Group():
with gr.Row(equal_height=True):
with gr.Column(min_width=15, scale=1):
score = gr.Textbox(label="Score", interactive=False, autoscroll=False, max_lines=1)
with gr.Column(scale=5):
feedback = gr.Textbox(label="Feedback", interactive=False, autoscroll=False, max_lines=6)
with gr.Column(min_width=15, scale=1):
evaluate_btn = gr.Button("Evaluate", variant="primary")
reset_all_btn = gr.Button("Clear All", variant="stop") # Add Reset All button
reset_all_btn.click(
reset_all,
inputs=[],
outputs=[
inputs_task,
new_input_name,
new_input_value,
rubric_items,
new_rubric_name,
new_rubric_value,
evaluation_criteria, # Reset evaluation criteria
output_name, # Reset output name
output_value, # Reset output value
feedback, # Reset feedback
score # Reset score
]
)
evaluate_btn.click(
evaluate,
inputs=[inputs_task, output_name, output_value, evaluation_criteria, rubric_items],
outputs=[feedback, score]
)
for i, button in enumerate(preset_buttons):
def populate_preset(ex_i=i):
return populate_fields(ex_i)
button.click(
populate_preset, # Use the closure to pass the current index
inputs=[], # No direct inputs needed
outputs=[
inputs_task,
output_name,
output_value,
evaluation_criteria,
rubric_items,
feedback, # Add feedback to be reset
score # Add score to be reset
]
)
def populate_fields(example_index: int):
example = EXAMPLES[example_index]
return (
example["inputs_task"],
example["output"]["name"],
example["output"]["value"],
example["evaluation_criteria"],
example["rubric"],
"", # Reset feedback
"" # Reset score
)
demo.launch(debug=True)
|