diff options
author | polwex <polwex@sortug.com> | 2025-05-15 15:50:51 +0700 |
---|---|---|
committer | polwex <polwex@sortug.com> | 2025-05-15 15:50:51 +0700 |
commit | 05d13b6f166eae5c2de8fe6f6038819b1b6ba1a0 (patch) | |
tree | 795ca33a3319d11bd9daa0366d4d15f31eabf024 /src/pages | |
parent | 92139f14a92a535f123ad49a60498138dd2ec6cf (diff) |
m
Diffstat (limited to 'src/pages')
-rw-r--r-- | src/pages/api/formdata/[slug].ts | 50 | ||||
-rw-r--r-- | src/pages/api/nlp.ts | 32 | ||||
-rw-r--r-- | src/pages/api/proxy.ts | 23 | ||||
-rw-r--r-- | src/pages/form.tsx | 27 |
4 files changed, 132 insertions, 0 deletions
diff --git a/src/pages/api/formdata/[slug].ts b/src/pages/api/formdata/[slug].ts new file mode 100644 index 0000000..3580e6a --- /dev/null +++ b/src/pages/api/formdata/[slug].ts @@ -0,0 +1,50 @@ +// import db from "../../lib/db"; +import { NLP } from "sortug-ai"; + +type Req = { endpoint: "nlp" | "llm"; app: string; body: any }; +export const POST = async (request: Request): Promise<Response> => { + const url = URL.parse(request.url); + const path = url!.pathname.replace("/api/formdata", ""); + const body = await request.formData(); + if (path === "/ocr") return postOCR(body); + // TODO audio etc. a lot of stuff goes through here + + // if (!body.name || !body.creds) { + return Response.json({ message: "Invalid" }, { status: 400 }); + // } + + // try { + // const res = db.loginUser(body.name, body.creds); + // console.log({ res }); + + // return Response.json(res, { status: 200 }); + // } catch (error) { + // return Response.json({ message: "Failure" }, { status: 500 }); + // } +}; +export const GET = async (request: Request): Promise<Response> => { + console.log({ request }); + + // if (!body.name || !body.creds) { + return Response.json({ message: "Invalid" }, { status: 400 }); + // } + + // try { + // const res = db.loginUser(body.name, body.creds); + // console.log({ res }); + + // return Response.json(res, { status: 200 }); + // } catch (error) { + // return Response.json({ message: "Failure" }, { status: 500 }); + // } +}; + +async function postOCR(formData: FormData) { + try { + const res = await NLP.ocr(formData); + console.log({ res }); + return Response.json(res, { status: 200 }); + } catch (error) { + return Response.json({ message: "Failure" }, { status: 500 }); + } +} diff --git a/src/pages/api/nlp.ts b/src/pages/api/nlp.ts new file mode 100644 index 0000000..0e5eacb --- /dev/null +++ b/src/pages/api/nlp.ts @@ -0,0 +1,32 @@ +// import db from "../../lib/db"; +import { z } from "zod"; +import { NLP } from "sortug-ai"; + +const schema = z.object({ + app: z.enum(["stanza", "spacy"]), + text: z.string().min(3, "minimum 3 characters"), + lang: z + .custom<string>((val) => { + const check = NLP.ISO.BCP47.parse(val); + if (!check.language) return false; + const twochars = Object.values(NLP.ISO.iso6393To1); + return twochars.includes(check.language); + }) + .optional(), +}); + +export const POST = async (request: Request): Promise<Response> => { + const bod = await request.json(); + const { app, text, lang } = await schema.parseAsync(bod); + + try { + const res = + app === "stanza" + ? NLP.Stanza.segmenter(text, lang) + : NLP.Spacy.run(text, lang); + const r = await res; + return Response.json(r, { status: 200 }); + } catch (error) { + return Response.json({ message: "Failure" }, { status: 500 }); + } +}; diff --git a/src/pages/api/proxy.ts b/src/pages/api/proxy.ts new file mode 100644 index 0000000..3114f6b --- /dev/null +++ b/src/pages/api/proxy.ts @@ -0,0 +1,23 @@ +// import db from "../../lib/db"; +import { z } from "zod"; + +export const proxySchema = z.object({ + path: z.string().startsWith("/").optional(), + url: z.string().url("Invalid urladdress"), + body: z.any().optional(), + headers: z.record(z.string(), z.string()).optional(), +}); + +export const POST = async (request: Request): Promise<Response> => { + const bod = await request.json(); + const parsedBody = await proxySchema.parseAsync(bod); + + try { + const res = await fetch(parsedBody.url, parsedBody as any); + console.log({ res }); + + return Response.json(res, { status: 200 }); + } catch (error) { + return Response.json({ message: "Failure" }, { status: 500 }); + } +}; diff --git a/src/pages/form.tsx b/src/pages/form.tsx new file mode 100644 index 0000000..82ffd99 --- /dev/null +++ b/src/pages/form.tsx @@ -0,0 +1,27 @@ +import { Link } from "waku"; + +import { Counter } from "../components/counter"; +import { getContextData } from "waku/middleware/context"; +import Main from "../components/Main"; + +export default async function HomePage() { + const { user } = getContextData(); + + return <Main />; +} + +const getData = async () => { + const data = { + title: "Waku", + headline: "Waku", + body: "Hello world!", + }; + + return data; +}; + +export const getConfig = async () => { + return { + render: "dynamic", + } as const; +}; |