-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement summarize_story
for web
#64
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// TODO(#65): Unify and simplify the task paths | ||
import { Textarea } from "@chakra-ui/react"; | ||
import { useRef, useState } from "react"; | ||
import useSWRMutation from "swr/mutation"; | ||
import useSWRImmutable from "swr/immutable"; | ||
|
||
import fetcher from "src/lib/fetcher"; | ||
import poster from "src/lib/poster"; | ||
|
||
const SummarizeStory = () => { | ||
// Use an array of tasks that record the sequence of steps until a task is | ||
// deemed complete. | ||
const [tasks, setTasks] = useState([]); | ||
|
||
const inputRef = useRef<HTMLTextAreaElement>(null); | ||
|
||
// Fetch the very fist task. We can ignore everything except isLoading | ||
// because the onSuccess handler will update `tasks` when ready. | ||
const { isLoading } = useSWRImmutable("/api/new_task/summarize_story", fetcher, { | ||
onSuccess: (data) => { | ||
console.log(data); | ||
setTasks([data]); | ||
}, | ||
}); | ||
|
||
// Every time we submit an answer to the latest task, let the backend handle | ||
// all the interactions then add the resulting task to the queue. This ends | ||
// when we hit the done task. | ||
const { trigger, isMutating } = useSWRMutation("/api/update_task", poster, { | ||
onSuccess: async (data) => { | ||
const newTask = await data.json(); | ||
// This is the more efficient way to update a react state array. | ||
setTasks((oldTasks) => [...oldTasks, newTask]); | ||
}, | ||
}); | ||
|
||
// Trigger a mutation that updates the current task. We should probably | ||
// signal somewhere that this interaction is being processed. | ||
const submitResponse = (task: { id: string }) => { | ||
const text = inputRef.current.value.trim(); | ||
trigger({ | ||
id: task.id, | ||
content: { | ||
update_type: "text_reply_to_post", | ||
text, | ||
}, | ||
}); | ||
}; | ||
|
||
/** | ||
* TODO: Make this a nicer loading screen. | ||
*/ | ||
if (tasks.length == 0) { | ||
return <div className=" p-6 h-full mx-auto bg-slate-100 text-gray-800">Loading...</div>; | ||
} | ||
|
||
return ( | ||
<div className=" p-6 h-full mx-auto bg-slate-100 text-gray-800"> | ||
{/* Instrunction and Output panels */} | ||
<section className="mb-8 lt-lg:mb-12 "> | ||
<div className="grid lg:gap-x-12 lg:grid-cols-2"> | ||
{/* Instruction panel */} | ||
<div className="rounded-lg shadow-lg h-full block bg-white"> | ||
<div className="p-6"> | ||
<h5 className="text-lg font-semibold">Instruction</h5> | ||
<p className="text-lg py-1">Summarize the following story</p> | ||
<div className="bg-slate-800 p-6 rounded-xl text-white whitespace-pre-wrap">{tasks[0].task.story}</div> | ||
</div> | ||
</div> | ||
|
||
{/* Output panel */} | ||
<div className="mt-6 lg:mt-0 rounded-lg shadow-lg h-full block bg-white"> | ||
<div className="flex justify-center p-6"> | ||
<Textarea name="summary" placeholder="Summary" ref={inputRef} /> | ||
</div> | ||
</div> | ||
</div> | ||
</section> | ||
|
||
{/* Info & controls */} | ||
<section className="mb-8 p-4 rounded-lg shadow-lg bg-white flex flex-row justify-items-stretch "> | ||
<div className="flex flex-col justify-self-start text-gray-700"> | ||
<div> | ||
<span> | ||
<b>Prompt</b> | ||
</span> | ||
<span className="ml-2">{tasks[0].id}</span> | ||
</div> | ||
<div> | ||
<span> | ||
<b>Output</b> | ||
</span> | ||
<span className="ml-2">Submit your answer</span> | ||
</div> | ||
</div> | ||
|
||
{/* Skip / Submit controls */} | ||
<div className="flex justify-center ml-auto"> | ||
<button | ||
type="button" | ||
className="mr-2 inline-flex items-center rounded-md border border-transparent bg-indigo-100 px-4 py-2 text-sm font-medium text-indigo-700 hover:bg-indigo-200 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2" | ||
> | ||
Skip | ||
</button> | ||
<button | ||
type="button" | ||
onClick={() => submitResponse(tasks[0])} | ||
className="inline-flex items-center rounded-md border border-transparent bg-indigo-600 px-4 py-2 text-sm font-medium text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2" | ||
> | ||
Submit | ||
</button> | ||
</div> | ||
</section> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SummarizeStory; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment at the top here:
// TODO(#65): Unify and simplify the task paths.