File size: 3,290 Bytes
e864e26
f62b8d3
1185ec1
4c34e70
e864e26
f62b8d3
4c34e70
f62b8d3
0f35d4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f62b8d3
 
 
 
4c34e70
8f2b05f
4c34e70
f62b8d3
4c34e70
 
 
93f8352
4c34e70
 
e864e26
f62b8d3
 
 
 
 
4c34e70
 
0f35d4c
4c34e70
 
93f8352
4c34e70
 
e864e26
f62b8d3
 
 
 
 
 
 
 
 
f27679f
 
 
 
 
4c34e70
 
 
93f8352
4c34e70
 
0f35d4c
f27679f
4c34e70
93f8352
 
 
f62b8d3
 
 
93f8352
f62b8d3
93f8352
 
f62b8d3
 
93f8352
 
 
 
 
4c34e70
 
 
93f8352
4c34e70
 
0f35d4c
f62b8d3
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { parseMediaOrientation, defaultMediaOrientation } from "@aitube/clap"

import { ChannelInfo, ParsedDatasetPrompt } from "@/types/general"
import { parseVideoModelName } from "./parseVideoModelName"
import { defaultVideoModel } from "@/app/config"

export function parseDatasetPrompt(markdown: string, channel: ChannelInfo): ParsedDatasetPrompt {
  try {
    const {
      title,
      description,
      tags,
      prompt,
      model,
      lora,
      style,
      thumbnail,
      voice,
      music,
      // duration,
      // width,
      // height,
      orientation
    } = parseMarkdown(markdown)

    return {
      title: typeof title === "string" && title ? title : "",
      description: typeof description === "string" && description ? description : "",
      tags:
        tags && typeof tags === "string" ? tags.split("- ").map(x => x.trim()).filter(x => x)
        : (channel.tags || []),
      prompt: typeof prompt === "string" && prompt ? prompt : "",
      model: parseVideoModelName(model, channel.model),
      lora: typeof lora === "string" && lora ? lora : (channel.lora || ""),
      style: typeof style === "string" && style ? style : (channel.style || ""),
      thumbnail: typeof thumbnail === "string" && thumbnail ? thumbnail : "",
      voice: typeof voice === "string" && voice ? voice : (channel.voice || ""),
      music: typeof music === "string" && music ? music : (channel.music || ""),
      orientation: parseMediaOrientation(orientation, channel.orientation),
    }
  } catch (err) {
    return {
      title: "",
      description:  "",
      tags: channel.tags || [],
      prompt: "",
      model: channel.model || defaultVideoModel,
      lora: channel.lora || "",
      style: channel.style || "",
      thumbnail: "",
      voice: channel.voice || "",
      music: channel.music || "",
      orientation: channel.orientation || defaultMediaOrientation,
    }
  }
}

/**
 * Simple Markdown Parser to extract sections into a JSON object
 * @param markdown A Markdown string containing Description and Prompt sections
 * @returns A JSON object with { "description": "...", "prompt": "..." }
 */
function parseMarkdown(markdown: string): {
  title: string
  description: string
  tags: string
  prompt: string
  model: string
  lora: string
  style: string
  thumbnail: string
  voice: string
  music: string
  orientation: string
} {
  markdown = `${markdown || ""}`.trim()

  // Improved regular expression to find markdown sections and accommodate multi-line content.
  const sectionRegex = /^#+\s+(?<key>.+?)\n\n?(?<content>[^#]+)/gm;

  const sections: { [key: string]: string } = {};

  let match;
  while ((match = sectionRegex.exec(markdown))) {
    const { key, content } = match.groups as { key: string; content: string };
    sections[key.trim().toLowerCase()] = content.trim();
  }

  return {
    title: sections["title"] || "",
    description: sections["description"] || "",
    tags: sections["tags"] || "",
    prompt: sections["prompt"] || "",
    model: sections["model"] || "",
    lora: sections["lora"] || "",
    style: sections["style"] || "",
    thumbnail: sections["thumbnail"] || "",
    voice: sections["voice"] || "",
    music: sections["music"] || "",
    orientation: sections["orientation"] || "",
  };
}