Skip to content

Commit

Permalink
Ask an LLM for labels
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhanshuguptagit committed Aug 26, 2024
1 parent d5bf0d2 commit d6160a8
Show file tree
Hide file tree
Showing 12 changed files with 374 additions and 47 deletions.
60 changes: 60 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ body {
margin: 0;
background: var(--sheet-background);
overflow: hidden;
font-family: system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
font-size: 0.8rem;
}

a {
Expand Down Expand Up @@ -454,4 +456,62 @@ p {
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3);
}

.popups {
position: absolute;
bottom: 40px;
right: 10px;
}

.popup {
background: var(--sheet-background);
transition: transform 0.5s ease;
box-shadow: 0px 1px 1px 0px rgba(122, 118, 118, 0.75);
border-radius: 5px;
font-family: SpaceGrotesk, sans-serif;
background-color: var(--sheet-background);
border: 1px solid #ccc;

padding: 10px;
width: 400px;
min-height: 70px;
margin: 10px;
margin-right: 20px;
}

.loader-popup {
display: flex;
}

details {
color: var(--btn-foreground);
white-space: pre-wrap;
max-height: 350px;
overflow-y: scroll;
}

summary {
cursor: pointer;
}

.llm-loader {
animation: rotate 2s linear infinite;
display: inline-block;
font-size: 48px;
line-height: 48px;
margin: auto;
color: #999999;
vertical-align: middle;
}

@keyframes rotate {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}

.label-suggestion {
cursor: pointer;
}

.label-suggestion:hover {
text-decoration: underline;
}
68 changes: 43 additions & 25 deletions src/bean/frames.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,30 @@
{:dirn dirn :color color})
sheet))

(defn add-preview-label [sheet frame-name rc dirn & [color]]
(if (= (cell-frame rc sheet) frame-name)
(assoc-in
sheet
[:frames frame-name :preview-labels (util/merged-or-self rc sheet)]
{:dirn dirn :color color})
sheet))

(defn add-labels [sheet frame-name addresses dirn]
(reduce #(add-label %1 frame-name %2 dirn
(case dirn
:top (util/random-color-hex (str (first %2) dirn))
:left (util/random-color-hex (str (second %2) dirn))
(util/random-color-hex (str %2 dirn)))) sheet addresses))

(defn add-preview-labels [sheet frame-name addresses dirn]
(reduce #(add-preview-label %1 frame-name %2 dirn 0xaa0006) sheet addresses))

(defn remove-labels [sheet frame-name addresses]
(reduce #(update-in % [:frames frame-name :labels] dissoc %2) sheet addresses))

(defn remove-preview-labels [sheet frame-name addresses]
(reduce #(update-in % [:frames frame-name :preview-labels] dissoc %2) sheet addresses))

(defn get-frame [sheet frame-name]
(get-in sheet [:frames frame-name]))

Expand Down Expand Up @@ -127,36 +141,40 @@
(top-blocking-label sheet [r c] labels)
(left-blocking-label sheet [r c] labels)))

(defn blocking-label [sheet frame-name label]
(let [{:keys [labels] :as frame} (get-frame sheet frame-name)
{:keys [dirn]} (get-in frame [:labels label])]
(defn blocking-label [sheet frame-name label dirn]
(let [{:keys [labels]} (get-frame sheet frame-name)]
(case dirn
:top (top-blocking-label sheet label labels)
:top-left (top-left-blocking-label sheet label labels)
:left (left-blocking-label sheet label labels))))

(defn label->cells [sheet frame-name label]
(let [{:keys [end] :as frame} (get-frame sheet frame-name)
[frame-end-r frame-end-c] end
labels (:labels frame)
merged-with-labels (mapcat
#(get-in (util/get-cell (:grid sheet) %)
[:style :merged-addresses])
(keys labels))]
(when-let [{:keys [dirn]} (get labels label)]
(as->
(area/area->addresses
{:start label
:end (let [[br bc] (blocking-label sheet frame-name label)]
(case dirn
:top [(if br (dec br) frame-end-r)
(min (last-col label sheet) frame-end-c)]
:left [(min (last-row label sheet) frame-end-r)
(if bc (dec bc) frame-end-c)]
:top-left [(if br br frame-end-r)
(if bc bc frame-end-c)]))}) cells
(disj cells label)
(apply disj cells merged-with-labels)))))
(defn label->cells
([sheet frame-name label]
(if-let [dirn (get-in (get-frame sheet frame-name)
[:labels label :dirn])]
(label->cells sheet frame-name label dirn)
#{}))
([sheet frame-name label dirn]
(let [{:keys [end] :as frame} (get-frame sheet frame-name)
[frame-end-r frame-end-c] end
labels (:labels frame)
merged-with-labels (mapcat
#(get-in (util/get-cell (:grid sheet) %)
[:style :merged-addresses])
(keys labels))]
(as->
(area/area->addresses
{:start label
:end (let [[br bc] (blocking-label sheet frame-name label dirn)]
(case dirn
:top [(if br (dec br) frame-end-r)
(min (last-col label sheet) frame-end-c)]
:left [(min (last-row label sheet) frame-end-r)
(if bc (dec bc) frame-end-c)]
:top-left [(if br br frame-end-r)
(if bc bc frame-end-c)]))}) cells
(disj cells label)
(apply disj cells merged-with-labels)))))

(defn skipped-cells [sheet frame-name]
(let [frame (get-frame sheet frame-name)
Expand Down
5 changes: 5 additions & 0 deletions src/bean/grid.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,18 @@

(defn remove-frame-labels [sheet frame-name addresses]
(-> (reduce #(set-cell-style %1 %2 :bold false) sheet addresses)
(frames/unmark-skipped frame-name addresses)
(frames/remove-labels frame-name addresses)
eval-sheet-a-few-times))

(defn mark-skip-cells [sheet frame-name addresses]
(-> (frames/mark-skipped sheet frame-name addresses)
eval-sheet-a-few-times))

(defn unmark-skip-cells [sheet frame-name addresses]
(-> (frames/unmark-skipped sheet frame-name addresses)
eval-sheet-a-few-times))

(defn pasted-area [pasted-at addresses]
(let [{:keys [start end]} (area/addresses->area addresses)]
{:start (util/offset start pasted-at)
Expand Down
1 change: 1 addition & 0 deletions src/bean/ui/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ average:{x.sum() / x.count()}"))
:col-widths (vec (repeat num-cols (:cell-w styles/sizes)))})
(assoc :frames {}))
:ui {:help-display false
:popups {}
:grid {:editing-cell nil
:selection nil
:highlighted-cells #{}}}}))
45 changes: 41 additions & 4 deletions src/bean/ui/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
[bean.grid :as grid]
[bean.ui.db :as db]
[bean.ui.demos :as demos]
[bean.ui.interceptors :refer [savepoint]]
[bean.ui.llm :as llm]
[bean.ui.paste :as paste]
[bean.ui.provenance :as provenance]
[bean.ui.util :as util]
[bean.ui.interceptors :refer [savepoint]]
[day8.re-frame.undo :as undo :refer [undoable]]
[re-frame.core :as rf]
[reagent.core :as rc]))
Expand Down Expand Up @@ -329,14 +330,42 @@
(update-in db [:sheet]
#(grid/add-frame-labels % frame-name addresses dirn))))

(rf/reg-event-db
::add-preview-labels
(fn add-preview-labels [db [_ frame-name addresses dirn]]
(update-in db [:sheet]
#(frames/add-preview-labels % frame-name addresses dirn))))

(rf/reg-event-db
::remove-labels
[(undoable)
(savepoint)]
(fn remove-labels [db [_ frame-name addresses]]
(-> db
(update-in [:sheet] #(frames/unmark-skipped % frame-name addresses))
(update-in [:sheet] #(grid/remove-frame-labels % frame-name addresses)))))
(update-in db [:sheet] #(grid/remove-frame-labels % frame-name addresses))))

(rf/reg-event-db
::remove-preview-labels
(fn remove-preview-labels [db [_ frame-name addresses]]
(update-in db [:sheet] #(frames/remove-preview-labels % frame-name addresses))))

(rf/reg-event-db
::dismiss-popup
(fn dismiss-popup [db [_ popup-type]]
(update-in db [:ui :popups] dissoc popup-type)))

(rf/reg-event-db
::popup-add-labels
(fn popup-add-labels [db [_ suggestions]]
(-> (assoc-in db [:ui :asking-llm] false)
(update-in [:ui :popups] assoc :add-labels suggestions))))

(rf/reg-event-fx
::ask-labels-llm
(fn add-labels-llm [{:keys [db]} [_ frame-name addresses]]
(.then (llm/suggest-labels (:sheet db) frame-name)
#(rf/dispatch [::popup-add-labels %]))
{:db (assoc-in db [:ui :asking-llm] true)
:fx [[:dispatch [::dismiss-popup :add-labels]]]}))

(rf/reg-event-db
::mark-skip-cells
Expand All @@ -346,6 +375,14 @@
(update-in db [:sheet]
#(grid/mark-skip-cells % frame-name addresses))))

(rf/reg-event-db
::unmark-skip-cells
[(undoable)
(savepoint)]
(fn unmark-skip-cells [db [_ frame-name addresses]]
(update-in db [:sheet]
#(grid/unmark-skip-cells % frame-name addresses))))

(rf/reg-event-db
::explain
(fn explain [db [_ expression]]
Expand Down
5 changes: 4 additions & 1 deletion src/bean/ui/features.cljs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
(ns bean.ui.features)
(ns bean.ui.features
(:require [clojure.string :as string]))

(def show-control-bar true)
(defn llm-labelling? [sheet]
(not (clojure.string/blank? (get-in sheet [:anthropic-api-key]))))
Loading

0 comments on commit d6160a8

Please sign in to comment.