Spaces:
Running
Running
add manually model + catch error
Browse files
src/routes/api/generate/+server.ts
CHANGED
|
@@ -49,6 +49,8 @@ export async function POST({ request, cookies } : RequestEvent) {
|
|
| 49 |
}),
|
| 50 |
})
|
| 51 |
.then((response) => {
|
|
|
|
|
|
|
| 52 |
return response.arrayBuffer()
|
| 53 |
})
|
| 54 |
.then((response) => {
|
|
@@ -62,9 +64,7 @@ export async function POST({ request, cookies } : RequestEvent) {
|
|
| 62 |
|
| 63 |
if ("error" in response) {
|
| 64 |
return json({
|
| 65 |
-
error:
|
| 66 |
-
token: response.error
|
| 67 |
-
}
|
| 68 |
}, { status: 400 })
|
| 69 |
}
|
| 70 |
|
|
@@ -73,8 +73,8 @@ export async function POST({ request, cookies } : RequestEvent) {
|
|
| 73 |
if (token) {
|
| 74 |
const user = await tokenIsAvailable(token)
|
| 75 |
if (user?.sub) {
|
| 76 |
-
const dir = await promises.opendir(process
|
| 77 |
-
if (!dir) await promises.mkdir(process
|
| 78 |
const file_name_formatted = randomUUID() + "_" + generation?.inputs?.replaceAll(/[^a-zA-Z0-9]/g, "-") + ".png"
|
| 79 |
await promises.writeFile(`${process.env.PUBLIC_FILE_UPLOAD_DIR}/${file_name_formatted}`, response)
|
| 80 |
|
|
|
|
| 49 |
}),
|
| 50 |
})
|
| 51 |
.then((response) => {
|
| 52 |
+
if (response.status !== 200) throw new Error(response.statusText)
|
| 53 |
+
|
| 54 |
return response.arrayBuffer()
|
| 55 |
})
|
| 56 |
.then((response) => {
|
|
|
|
| 64 |
|
| 65 |
if ("error" in response) {
|
| 66 |
return json({
|
| 67 |
+
error: response.error
|
|
|
|
|
|
|
| 68 |
}, { status: 400 })
|
| 69 |
}
|
| 70 |
|
|
|
|
| 73 |
if (token) {
|
| 74 |
const user = await tokenIsAvailable(token)
|
| 75 |
if (user?.sub) {
|
| 76 |
+
const dir = await promises.opendir(process?.env?.PUBLIC_FILE_UPLOAD_DIR as string).catch(() => null)
|
| 77 |
+
if (!dir) await promises.mkdir(process?.env?.PUBLIC_FILE_UPLOAD_DIR as string)
|
| 78 |
const file_name_formatted = randomUUID() + "_" + generation?.inputs?.replaceAll(/[^a-zA-Z0-9]/g, "-") + ".png"
|
| 79 |
await promises.writeFile(`${process.env.PUBLIC_FILE_UPLOAD_DIR}/${file_name_formatted}`, response)
|
| 80 |
|
src/routes/api/scrap-models/+server.ts
CHANGED
|
@@ -47,7 +47,7 @@ export async function POST({ request }) {
|
|
| 47 |
image: model.image,
|
| 48 |
likes: model.likes,
|
| 49 |
downloads: model.downloads,
|
| 50 |
-
isPublic:
|
| 51 |
}
|
| 52 |
}).catch(() => {})
|
| 53 |
|
|
|
|
| 47 |
image: model.image,
|
| 48 |
likes: model.likes,
|
| 49 |
downloads: model.downloads,
|
| 50 |
+
isPublic: true,
|
| 51 |
}
|
| 52 |
}).catch(() => {})
|
| 53 |
|
src/routes/api/scrap-models/[slug]/+server.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// latent-consistency/lcm-lora-sdv1-5
|
| 2 |
+
/** @type {import('./$types').RequestHandler} */
|
| 3 |
+
|
| 4 |
+
import { json } from '@sveltejs/kit';
|
| 5 |
+
// import moment from 'moment';
|
| 6 |
+
import prisma from '$lib/prisma';
|
| 7 |
+
|
| 8 |
+
export async function POST({ request, params }) {
|
| 9 |
+
const headers = Object.fromEntries(request.headers.entries());
|
| 10 |
+
const slug= params?.slug?.replace("@", "/")
|
| 11 |
+
|
| 12 |
+
if (headers["x-hf-token"] !== process.env.SECRET_HF_TOKEN) {
|
| 13 |
+
return Response.json({
|
| 14 |
+
message: "Wrong castle fam :^)"
|
| 15 |
+
}, { status: 401 });
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
const response = await fetch(`https://huggingface.co/api/models/${slug}`)
|
| 19 |
+
const model = await response.json();
|
| 20 |
+
|
| 21 |
+
const hasReadme = model?.siblings?.find((sibling: Record<string, string>) => sibling?.rfilename === "README.md")
|
| 22 |
+
if (hasReadme) {
|
| 23 |
+
const readmeRes = await fetch(`https://huggingface.co/${model.id}/raw/main/README.md`)
|
| 24 |
+
const readme = await readmeRes.text().catch(() => null)
|
| 25 |
+
if (!readme) {
|
| 26 |
+
return json({
|
| 27 |
+
message: "No readme"
|
| 28 |
+
}, { status: 404 })
|
| 29 |
+
}
|
| 30 |
+
const imageRegex = /!\[.*\]\((.*)\)/
|
| 31 |
+
let image = readme.match(imageRegex)?.[1]
|
| 32 |
+
|
| 33 |
+
if (!image) {
|
| 34 |
+
return json({
|
| 35 |
+
message: "No readme"
|
| 36 |
+
}, { status: 404 })
|
| 37 |
+
}
|
| 38 |
+
image = image.replace(///g, "/")
|
| 39 |
+
if (image.startsWith("http")) model.image = image
|
| 40 |
+
else model.image = `https://huggingface.co/${model.id}/resolve/main/${image.replace("./", "")}`
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
await prisma.model.create({
|
| 44 |
+
data: {
|
| 45 |
+
id: model.id,
|
| 46 |
+
image: model.image,
|
| 47 |
+
likes: model.likes,
|
| 48 |
+
downloads: model.downloads,
|
| 49 |
+
isPublic: true,
|
| 50 |
+
instance_prompt: model?.cardData?.instance_prompt,
|
| 51 |
+
}
|
| 52 |
+
}).catch(() => {})
|
| 53 |
+
|
| 54 |
+
return json({
|
| 55 |
+
message: `Successfully added the model ${model.id}`,
|
| 56 |
+
})
|
| 57 |
+
}
|
src/routes/generate/+page.svelte
CHANGED
|
@@ -12,6 +12,7 @@
|
|
| 12 |
import Response from "$lib/components/generate/Response.svelte";
|
| 13 |
import Autocomplete from "$lib/components/models/autocomplete/Autocomplete.svelte";
|
| 14 |
import { generationStore } from "$lib/stores/use-generation";
|
|
|
|
| 15 |
|
| 16 |
export let data
|
| 17 |
let generation = get(generationStore);
|
|
@@ -40,11 +41,15 @@
|
|
| 40 |
|
| 41 |
const response = await request.json();
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
loading = false
|
| 49 |
}
|
| 50 |
</script>
|
|
|
|
| 12 |
import Response from "$lib/components/generate/Response.svelte";
|
| 13 |
import Autocomplete from "$lib/components/models/autocomplete/Autocomplete.svelte";
|
| 14 |
import { generationStore } from "$lib/stores/use-generation";
|
| 15 |
+
import { error } from "$lib/utils/toaster.js";
|
| 16 |
|
| 17 |
export let data
|
| 18 |
let generation = get(generationStore);
|
|
|
|
| 41 |
|
| 42 |
const response = await request.json();
|
| 43 |
|
| 44 |
+
if (response?.error) {
|
| 45 |
+
error(response?.error)
|
| 46 |
+
} else {
|
| 47 |
+
generationStore.set({
|
| 48 |
+
image: response?.image,
|
| 49 |
+
gallery: response?.gallery,
|
| 50 |
+
form: form
|
| 51 |
+
})
|
| 52 |
+
}
|
| 53 |
loading = false
|
| 54 |
}
|
| 55 |
</script>
|