File size: 4,597 Bytes
385be56
be92704
385be56
ab0b62d
385be56
37f0854
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aba4c86
ab0b62d
aba4c86
ab0b62d
aba4c86
37f0854
ab0b62d
37f0854
 
 
 
ab0b62d
37f0854
 
 
ab0b62d
 
37f0854
 
ab0b62d
 
61909cd
ab0b62d
 
61909cd
37f0854
ec7fff2
ab0b62d
3ebe57a
ab0b62d
 
 
 
c7f65ea
37f0854
 
 
 
 
 
ab0b62d
 
37f0854
 
 
 
24dc9b8
ab0b62d
 
24dc9b8
 
 
 
ab0b62d
 
24dc9b8
 
 
37f0854
 
24dc9b8
 
 
37f0854
 
 
 
 
 
 
ab0b62d
37f0854
 
 
aba4c86
37f0854
 
ab0b62d
37f0854
ab0b62d
 
 
37f0854
 
 
ab0b62d
 
 
 
 
 
 
 
 
37f0854
 
3ebe57a
ec7fff2
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
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
import gradio as gr
from root import RootSignals

client = 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)

def process_and_evaluate(api_key, user_input, llm_response, selected_evaluator):
    global client
    if not api_key:
        # Display a pop-up info message (if supported by your version)
        gr.Info("API key is missing. Please enter your API key to proceed.")
        return "", "", gr.update(visible=False)
    
    if not client:
        initialize_client(api_key)
    
    # Get the evaluator instance by name
    evaluator = client.evaluators.get_by_name(name=selected_evaluator)
    
    # Run evaluation using the selected evaluator
    evaluation_result = evaluator.run(request=user_input, response=llm_response)
    score = evaluation_result.score
    justification = evaluation_result.justification
    # Reveal the monitoring button
    return score, justification, gr.update(visible=True)

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>&nbsp;</div>")
    gr.Markdown("# *Root* Evaluators Demo by Root Signals")
    gr.Markdown(
        "[Sign-up](https://app.rootsignals.ai/register) to create your API key or "
        "[create a temporary one](https://app.rootsignals.ai/demo-user)!"
    )
    
    api_key = gr.Textbox(
        label="🔑 Root Signals API Key",
        placeholder="Enter your Root Signals API key...",
        type="password",
        show_label=True,
    )
    
    gr.Markdown("---")
    
    with gr.Row():
        # Left column
        with gr.Column():
            user_input = gr.Textbox(
                label="👤 User Instruction or Question (Optional)",
                placeholder="Enter user input here...",
                value="Where can I apply to this position?",
                interactive=True
            )
            llm_response = gr.Textbox(
                label="🤖 LLM Response (to be evaluated)",
                placeholder="Enter the LLM response to be evaluated here...",
                value="You can find the instructions from our Careers page.",
                interactive=True
            )
            evaluator_dropdown = gr.Dropdown(
                label="🔎 Select Evaluator",
                choices=ROOT_EVALUATORS,
                interactive=True,
                value="Politeness",
            )
        
        # 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 when API key is provided.
    api_key.change(
        fn=initialize_client,
        inputs=[api_key],
        outputs=[]
    )
    
    # Button to trigger the evaluation.
    submit_btn = gr.Button("🧐 EVALUATE", variant="primary")
    # New button for monitoring (initially hidden).
    monitoring_btn = gr.Button("See in Root Signals Monitoring View", visible=False)
    
    submit_btn.click(
        fn=process_and_evaluate,
        inputs=[api_key, user_input, llm_response, evaluator_dropdown],
        outputs=[score, justification, monitoring_btn]
    )
    
    # When clicked, the monitoring button opens the monitoring dashboard.
    monitoring_btn.click(
        fn=None,
        inputs=[],
        outputs=[],
        js="() => window.open('https://app.rootsignals.ai/monitoring/dashboard', '_blank')"
    )

    gr.Markdown("[🌐 Homepage](https://www.rootsignals.ai/) | [🤖 Github Repo](https://github.com/root-signals/rs-python-sdk) | [🐍 Python SDK Docs](https://sdk.rootsignals.ai/en/latest/) | [💬 Discord](https://discord.gg/EhazTQsFnj)")

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