File size: 3,092 Bytes
385be56
be92704
385be56
37f0854
385be56
37f0854
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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:
    with gr.Row():
        gr.Image(value="https://lh3.googleusercontent.com/d/1e49skZjYGTZscWCVrhf6Ad1bRP2pvwpI", height=60)
        gr.Markdown("<div>&nbsp;</div>")  # Add some space below the image
    gr.Markdown("# Root Evaluators Demo by Root Signals")
    
    api_key = gr.Textbox(
        label="🔑 Root Signals API Key",
        placeholder="Enter your Root Signals API key...",
        type="password",
        show_label=True,
    )
    
    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]
    )

if __name__ == "__main__":
    demo.launch()