-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
1036234
commit f887614
Showing
5 changed files
with
121 additions
and
143 deletions.
There are no files selected for viewing
93 changes: 0 additions & 93 deletions
93
apps/sovoli.com/src/app/(dashboard)/new/actions/newHighlightAction.ts
This file was deleted.
Oops, something went wrong.
46 changes: 0 additions & 46 deletions
46
apps/sovoli.com/src/app/(dashboard)/new/components/HighlightForm.tsx
This file was deleted.
Oops, something went wrong.
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,79 @@ | ||
import type { NextRequest } from "next/server"; | ||
import { google } from "@ai-sdk/google"; | ||
import { withZod } from "@rvf/zod"; | ||
import { generateObject } from "ai"; | ||
import { z } from "zod"; | ||
|
||
const model = google("gemini-1.5-flash"); | ||
|
||
const imageFileSchema = z.instanceof(File).refine( | ||
(file) => { | ||
return file.type === "image/png" || file.type === "image/jpeg"; | ||
}, | ||
{ | ||
message: "File must be an image", | ||
}, | ||
); | ||
|
||
export const formRequestBodySchema = z.object({ | ||
image: imageFileSchema, | ||
}); | ||
|
||
const validator = withZod(formRequestBodySchema); | ||
|
||
export async function POST(req: NextRequest): Promise<Response> { | ||
const formData = await req.formData(); | ||
|
||
const result = await validator.validate(formData); | ||
|
||
if (result.error) { | ||
return new Response(JSON.stringify(result.error), { | ||
status: 400, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
} | ||
|
||
const image = result.data.image; | ||
|
||
const fileBuffer = await image.arrayBuffer(); | ||
|
||
const { object } = await generateObject({ | ||
model: model, | ||
schema: z.object({ | ||
page: z.number().optional(), | ||
chapter: z.string().optional(), | ||
highlights: z.string().array(), | ||
}), | ||
messages: [ | ||
{ | ||
role: "user", | ||
content: [ | ||
{ | ||
type: "text", | ||
text: ` | ||
This is an image of a page from a book. | ||
The chapter is displayed at the top of the page. If you cannot determine page or chapter, leave it. | ||
It should contain highlighted text, which may include a word, a phrase, a sentence, multiple sentences, or a paragraph. | ||
Separate each highlight into distinct entries if there is a natural or logical boundary, such as the end of a sentence or a clear change in context. | ||
If a highlight spans multiple sentences, group them together into one entry only if they are part of the same continuous thought or context, and there is no visible separation. | ||
`, | ||
}, | ||
{ | ||
type: "image", | ||
image: fileBuffer, | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
console.log(object); | ||
|
||
return new Response(JSON.stringify(object), { | ||
status: 200, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
} |