Spaces:
Sleeping
Sleeping
| 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> </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() |