Spaces:
Running
Running
import gradio as gr | |
from root import RootSignals | |
client = None # Initialize client as None | |
ROOT_EVALUATORS = ['Answer Correctness', | |
'Answer Relevance', | |
'Clarity', | |
'Coherence', | |
'Conciseness', | |
'Confidentiality', | |
'Engagingness', | |
'Formality', | |
'Harmlessness', | |
'Helpfulness', | |
'Non-toxicity', | |
'Originality', | |
'Persuasiveness', | |
'Politeness', | |
'Precision', | |
'Quality of Writing Creative', | |
'Quality of Writing Professional', | |
'Relevance', | |
'Safety for Children', | |
'Sentiment recognition', | |
] | |
def initialize_client(api_key): | |
global client | |
client = RootSignals(api_key=api_key) | |
return gr.Dropdown(choices=ROOT_EVALUATORS) | |
def process_and_evaluate(api_key, user_input, llm_response, selected_evaluator): | |
global client | |
if not client: | |
evaluator_dropdown = initialize_client(api_key) | |
# Get the evaluator instance by name | |
evaluator = client.evaluators.get_by_name(name=selected_evaluator) | |
# Run evaluation using selected evaluator | |
evaluation_result = evaluator.run(request=user_input, response=llm_response) | |
score = evaluation_result.score | |
justification = evaluation_result.justification | |
return score, justification | |
# Create the interface with a custom layout | |
with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as demo: | |
gr.HTML("""<a href="https://api.visitorbadge.io/api/visitors?path=https://huggingface.co/spaces/root-signals/RootEvaluatorsDemo"> | |
<img src="https://api.visitorbadge.io/api/visitors?path=https://huggingface.co/spaces/root-signals/RootEvaluatorsDemo" /> | |
</a>""") | |
with gr.Row(): | |
gr.Image(value="https://app.rootsignals.ai/images/root-signals-color.svg", height=70) | |
gr.Markdown("<div> </div>") # Add some space below the image | |
gr.Markdown("# Root Evaluators Demo by Root Signals") | |
gr.Markdown("[Sign-up](https://app.rootsignals.ai/register) to create your API key!") | |
api_key = gr.Textbox( | |
label="🔑 Root Signals API Key", | |
placeholder="Enter your Root Signals API key...", | |
type="password", | |
show_label=True, | |
) | |
gr.Markdown("---") # Divider | |
with gr.Row(): | |
# Left column | |
with gr.Column(): | |
user_input = gr.Textbox(label="👤 User Instruction or Question (Optional)", placeholder="Enter user input here...", interactive=True) | |
llm_response = gr.Textbox(label="🤖 LLM Response (to be evaluated)", placeholder="Enter the LLM response to be evaluated here...", interactive=True) | |
evaluator_dropdown = gr.Dropdown( | |
label="🔎 Select Evaluator", | |
choices=[], # Will be populated after client initialization | |
interactive=True | |
) | |
# Right column | |
with gr.Column(): | |
score = gr.Textbox(label="📊 Score (between 0 and 1)", interactive=False) | |
justification = gr.TextArea(label="💬 Justification", interactive=False) | |
# Initialize client and update dropdown when API key is provided | |
api_key.change( | |
fn=initialize_client, | |
inputs=[api_key], | |
outputs=[evaluator_dropdown] | |
) | |
# Button to trigger the process | |
submit_btn = gr.Button("🧐 EVALUATE", variant="primary") | |
submit_btn.click( | |
fn=process_and_evaluate, | |
inputs=[api_key, user_input, llm_response, evaluator_dropdown], | |
outputs=[score, justification] | |
) | |
gr.Markdown("[Homepage](https://www.rootsignals.ai/) | [Python SDK Docs](https://sdk.rootsignals.ai/en/latest/)") | |
if __name__ == "__main__": | |
demo.launch() |