Skip to content

Commit

Permalink
Remove unnecessary find method condition in WorkbenchController
Browse files Browse the repository at this point in the history
  • Loading branch information
kieranklaassen committed Aug 22, 2024
1 parent e82381f commit 2ab04d3
Showing 1 changed file with 86 additions and 44 deletions.
130 changes: 86 additions & 44 deletions app/views/leva/workbench/_results_section.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="w-1/3 bg-gray-900 border-l border-gray-800 p-5 overflow-y-auto">
<div class="w-1/3 bg-gray-900 border-l border-gray-800 p-5 overflow-y-auto" data-controller="button-loader">
<!-- Runner Dropdown -->
<div class="mb-5">
<h3 class="text-sm font-semibold mb-2 text-indigo-300">Select Runner</h3>
Expand Down Expand Up @@ -27,11 +27,13 @@
</div>
<!-- Run Button -->
<div class="mb-5">
<%= button_to run_workbench_index_path, method: :post, params: { runner: params[:runner], prompt_id: @prompt.id, dataset_record_id: @dataset_record&.id }, class: "w-full btn btn-primary bg-gradient-to-r from-blue-500 to-indigo-600 hover:from-blue-600 hover:to-indigo-700 text-white font-bold py-2 px-4 rounded-lg shadow-lg flex items-center justify-center transition duration-300 ease-in-out transform hover:scale-105" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM9.555 7.168A1 1 0 008 8v4a1 1 0 001.555.832l3-2a1 1 0 000-1.664l-3-2z" clip-rule="evenodd" />
</svg>
Run
<%= button_to run_workbench_index_path, method: :post, params: { runner: params[:runner], prompt_id: @prompt.id, dataset_record_id: @dataset_record&.id }, class: "w-full btn btn-primary bg-gradient-to-r from-blue-500 to-indigo-600 hover:from-blue-600 hover:to-indigo-700 text-white font-bold py-2 px-4 rounded-lg shadow-lg flex items-center justify-center transition duration-300 ease-in-out h-12", data: { action: "click->button-loader#handleClick", "button-loader-target": "button" } do %>
<span data-button-loader-target="buttonText" class="flex items-center">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM9.555 7.168A1 1 0 008 8v4a1 1 0 001.555.832l3-2a1 1 0 000-1.664l-3-2z" clip-rule="evenodd" />
</svg>
Run
</span>
<% end %>
</div>
<!-- Run Result -->
Expand All @@ -56,47 +58,87 @@
<div class="space-y-4">
<div class="flex items-center justify-between">
<h3 class="text-sm font-semibold text-indigo-400">Evaluators</h3>
<%= button_to run_with_evaluation_workbench_index_path, method: :post, params: { runner: params[:runner], prompt_id: @prompt.id, dataset_record_id: @dataset_record&.id }, class: "btn btn-primary bg-gradient-to-r from-green-500 to-teal-600 hover:from-green-600 hover:to-teal-700 text-white font-bold py-2 px-4 rounded-lg shadow-lg flex items-center justify-center transition duration-300 ease-in-out transform hover:scale-105" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M6 2a2 2 0 00-2 2v12a2 2 0 002 2h8a2 2 0 002-2V7.414A2 2 0 0015.414 6L12 2.586A2 2 0 0010.586 2H6zm2 10a1 1 0 10-2 0v3a1 1 0 102 0v-3zm2-3a1 1 0 011 1v5a1 1 0 11-2 0v-5a1 1 0 011-1zm4-1a1 1 0 10-2 0v7a1 1 0 102 0V8z" clip-rule="evenodd" />
</svg>
Evaluate All
<%= button_to run_with_evaluation_workbench_index_path, method: :post, params: { runner: params[:runner], prompt_id: @prompt.id, dataset_record_id: @dataset_record&.id }, class: "btn btn-primary bg-gradient-to-r from-green-500 to-teal-600 hover:from-green-600 hover:to-teal-700 text-white font-bold py-2 px-4 rounded-lg shadow-lg flex items-center justify-center transition duration-300 ease-in-out h-10", data: { action: "click->button-loader#handleClick", "button-loader-target": "button" } do %>
<span data-button-loader-target="buttonText" class="flex items-center">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M6 2a2 2 0 00-2 2v12a2 2 0 002 2h8a2 2 0 002-2V7.414A2 2 0 0015.414 6L12 2.586A2 2 0 0010.586 2H6zm2 10a1 1 0 10-2 0v3a1 1 0 102 0v-3zm2-3a1 1 0 011 1v5a1 1 0 11-2 0v-5a1 1 0 011-1zm4-1a1 1 0 10-2 0v7a1 1 0 102 0V8z" clip-rule="evenodd" />
</svg>
Evaluate All
</span>
<% end %>
</div>
<% @evaluators.each do |evaluator_class| %>
<div class="bg-gray-800 p-4 rounded-lg shadow-md">
<div class="flex items-center justify-between mb-2">
<span class="text-sm font-medium"><%= evaluator_class.name.demodulize %></span>
<div class="flex items-center space-x-2">
<%= button_to run_evaluator_workbench_index_path, method: :post, params: { evaluator: evaluator_class.name, runner: params[:runner], prompt_id: @prompt.id, dataset_record_id: @dataset_record&.id }, class: "p-2 bg-blue-600 hover:bg-blue-700 rounded-full transition duration-150 ease-in-out" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM9.555 7.168A1 1 0 008 8v4a1 1 0 001.555.832l3-2a1 1 0 000-1.664l-3-2z" clip-rule="evenodd" />
</svg>
<div class="grid grid-cols-2 gap-2">
<% @evaluators.each do |evaluator_class| %>
<div class="bg-gray-800 p-4 rounded-lg shadow-md">
<div class="flex items-center justify-between mb-2">
<span class="text-sm font-medium"><%= evaluator_class.name.demodulize %></span>
<%= button_to run_evaluator_workbench_index_path, method: :post, params: { evaluator: evaluator_class.name, runner: params[:runner], prompt_id: @prompt.id, dataset_record_id: @dataset_record&.id }, class: "p-2 bg-blue-600 hover:bg-blue-700 rounded-full transition duration-150 ease-in-out", data: { action: "click->button-loader#handleClick", "button-loader-target": "button" } do %>
<span data-button-loader-target="buttonText">
<svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM9.555 7.168A1 1 0 008 8v4a1 1 0 001.555.832l3-2a1 1 0 000-1.664l-3-2z" clip-rule="evenodd" />
</svg>
</span>
<% end %>
</div>
</div>
<% if @dataset_record %>
<% evaluation_result = @dataset_record.evaluation_results.for_evaluator(evaluator_class).last %>
<% if evaluation_result %>
<% score = evaluation_result.score %>
<% color_class = case score
when 0...0.2 then 'text-red-500'
when 0.2...0.4 then 'text-orange-500'
when 0.4...0.6 then 'text-yellow-500'
when 0.6...0.8 then 'text-lime-500'
when 0.8...1.0 then 'text-green-400'
else 'text-green-300'
end %>
<div class="text-sm <%= color_class %> font-semibold">
Score: <%= sprintf('%.2f', score) %>
</div>
<% else %>
<div class="text-sm text-gray-500">
No evaluation result yet.
</div>
<% if @dataset_record %>
<% evaluation_result = @dataset_record.evaluation_results.for_evaluator(evaluator_class).last %>
<% if evaluation_result %>
<% score = evaluation_result.score %>
<% color_class = case score
when 0...0.2 then 'text-red-500'
when 0.2...0.4 then 'text-orange-500'
when 0.4...0.6 then 'text-yellow-500'
when 0.6...0.8 then 'text-lime-500'
when 0.8...1.0 then 'text-green-400'
else 'text-green-300'
end %>
<div class="text-sm <%= color_class %> font-semibold">
Score: <%= sprintf('%.2f', score) %>
</div>
<% else %>
<div class="text-sm text-gray-500">
No evaluation result yet.
</div>
<% end %>
<% end %>
<% end %>
</div>
<% end %>
</div>
<% end %>
</div>
</div>
</div>
</div>
<script>
(() => {
const application = Stimulus.Application.start()

application.register("button-loader", class extends Stimulus.Controller {
static targets = ["button", "buttonText"]

handleClick(event) {
event.preventDefault()
const button = event.currentTarget
const form = button.closest('form')

this.disableButton(button)
this.showSpinner(button)

// Submit the form
form.submit()
}

disableButton(button) {
button.disabled = true
button.classList.add('opacity-50', 'cursor-not-allowed')
}

showSpinner(button) {
const buttonText = button.querySelector('[data-button-loader-target="buttonText"]')
buttonText.innerHTML = `
<svg class="animate-spin h-5 w-5 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
`
}
})
})()
</script>

0 comments on commit 2ab04d3

Please sign in to comment.