-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
305 additions
and
6 deletions.
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
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,48 @@ | ||
'use client' | ||
|
||
import { useCompletion } from 'ai/react' | ||
import { Button } from '@/components/ui/button' | ||
import { Textarea } from '@/components/ui/textarea' | ||
import { MagicWandIcon } from '@radix-ui/react-icons' | ||
import { ComponentPropsWithoutRef } from 'react' | ||
import { Loader2 } from 'lucide-react' | ||
|
||
export interface VideoDescriptionInputProps | ||
extends ComponentPropsWithoutRef<'textarea'> { | ||
videoId: string | ||
} | ||
|
||
export function VideoDescriptionInput({ | ||
videoId, | ||
...props | ||
}: VideoDescriptionInputProps) { | ||
const { completion, complete, isLoading } = useCompletion({ | ||
api: `/api/ai/generate/description?videoId=${videoId}`, | ||
}) | ||
|
||
return ( | ||
<> | ||
<Textarea | ||
disabled={isLoading} | ||
className="min-h-[160px] leading-relaxed" | ||
value={completion} | ||
{...props} | ||
/> | ||
<div> | ||
<Button | ||
disabled={isLoading} | ||
onClick={() => complete(videoId)} | ||
size="sm" | ||
variant="outline" | ||
> | ||
{isLoading ? ( | ||
<Loader2 className="mr-2 h-3 w-3 animate-spin" /> | ||
) : ( | ||
<MagicWandIcon className="mr-2 h-3 w-3" /> | ||
)} | ||
Generate with AI | ||
</Button> | ||
</div> | ||
</> | ||
) | ||
} |
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 @@ | ||
'use client' | ||
|
||
import { Badge } from '@/components/ui/badge' | ||
import { Button } from '@/components/ui/button' | ||
import { | ||
Command, | ||
CommandEmpty, | ||
CommandGroup, | ||
CommandInput, | ||
CommandItem, | ||
CommandList, | ||
} from '@/components/ui/command' | ||
import { | ||
Popover, | ||
PopoverContent, | ||
PopoverTrigger, | ||
} from '@/components/ui/popover' | ||
import { Separator } from '@/components/ui/separator' | ||
import { cn } from '@/lib/utils' | ||
import { CheckIcon } from '@radix-ui/react-icons' | ||
import { Tag } from 'lucide-react' | ||
|
||
const options = [ | ||
'ignite', | ||
'node', | ||
'react', | ||
'react-native', | ||
'explorer', | ||
'plus', | ||
'node-ddd', | ||
] | ||
|
||
export function VideoTagInput() { | ||
const error: any = false | ||
const tags = ['ignite', 'node'] | ||
|
||
return ( | ||
<Popover> | ||
<PopoverTrigger asChild> | ||
<Button | ||
data-error={!!error} | ||
variant="outline" | ||
size="sm" | ||
className="flex h-8 items-center border-dashed px-2 data-[error=true]:border-red-400 data-[error=true]:bg-red-50" | ||
> | ||
<Tag className="mr-2 h-3 w-3" /> | ||
<span className="text-xs">Tags</span> | ||
|
||
{!!error && ( | ||
<span className="ml-2 text-xs font-normal">{error.message}</span> | ||
)} | ||
|
||
{tags.length > 0 && ( | ||
<> | ||
<Separator orientation="vertical" className="mx-2 h-4" /> | ||
<div className="flex gap-1"> | ||
{tags.length > 2 ? ( | ||
<Badge | ||
variant="secondary" | ||
className="pointer-events-none rounded-sm px-1 font-normal" | ||
> | ||
{tags.length} selected | ||
</Badge> | ||
) : ( | ||
options | ||
.filter((option) => tags.includes(option)) | ||
.map((option) => ( | ||
<Badge | ||
variant="secondary" | ||
key={option} | ||
className="pointer-events-none rounded-sm px-1 font-normal" | ||
> | ||
{option} | ||
</Badge> | ||
)) | ||
)} | ||
</div> | ||
</> | ||
)} | ||
</Button> | ||
</PopoverTrigger> | ||
<PopoverContent className="w-[200px] p-0" align="start"> | ||
<Command> | ||
<CommandInput placeholder="Tags" /> | ||
<CommandList> | ||
<CommandEmpty>No results found.</CommandEmpty> | ||
<CommandGroup> | ||
{options.map((option) => { | ||
const isSelected = tags.includes(option) | ||
|
||
return ( | ||
<CommandItem | ||
key={option} | ||
onSelect={() => { | ||
// TODO: make it work | ||
}} | ||
> | ||
<div | ||
className={cn( | ||
'mr-2 flex h-4 w-4 items-center justify-center rounded-sm border border-primary', | ||
isSelected | ||
? 'bg-primary text-primary-foreground' | ||
: 'opacity-50 [&_svg]:invisible', | ||
)} | ||
> | ||
<CheckIcon className={cn('h-4 w-4')} /> | ||
</div> | ||
<span>{option}</span> | ||
</CommandItem> | ||
) | ||
})} | ||
</CommandGroup> | ||
</CommandList> | ||
</Command> | ||
</PopoverContent> | ||
</Popover> | ||
) | ||
} |
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,56 @@ | ||
import { openai } from '@/lib/openai' | ||
import { prisma } from '@/lib/prisma' | ||
import { OpenAIStream, StreamingTextResponse } from 'ai' | ||
import { NextResponse } from 'next/server' | ||
|
||
export async function POST(request: Request) { | ||
const { prompt } = await request.json() | ||
|
||
const [transcription] = await prisma.$queryRaw<[{ text: string }]>/* sql */ ` | ||
SELECT | ||
STRING_AGG("public"."TranscriptionSegment"."text", '') as "text" | ||
FROM "public"."TranscriptionSegment" | ||
JOIN "public"."Transcription" ON "public"."Transcription"."id" = "public"."TranscriptionSegment"."transcriptionId" | ||
WHERE "public"."Transcription"."videoId" = ${prompt} | ||
` | ||
|
||
if (!transcription.text) { | ||
return NextResponse.json( | ||
{ message: 'Transcription not found.' }, | ||
{ | ||
status: 400, | ||
}, | ||
) | ||
} | ||
|
||
const response = await openai.createChatCompletion({ | ||
model: 'gpt-3.5-turbo', | ||
stream: true, | ||
messages: [ | ||
{ | ||
role: 'system', | ||
content: | ||
'Se comporte como um especialista em programação que cria resumos a partir da transcrição de uma aula.', | ||
}, | ||
{ | ||
role: 'system', | ||
content: | ||
'Responda em primeira pessoa como se você fosse o instrutor da aula. Utilize uma linguagem menos formal.', | ||
}, | ||
{ | ||
role: 'system', | ||
content: | ||
'Seja sucinto e retorne no máximo 80 palavras em markdown sem cabeçalhos.', | ||
}, | ||
{ | ||
role: 'user', | ||
content: `Gere um resumo da transcrição abaixo: \n\n ${transcription.text}`, | ||
}, | ||
], | ||
temperature: 0, | ||
}) | ||
|
||
const stream = OpenAIStream(response) | ||
|
||
return new StreamingTextResponse(stream) | ||
} |
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