Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Write the lyrics: in the language and script of the song's description

Adarsh65bdfeeacd1d
diff --git a/src/lib/lyrics-language.ts b/src/lib/lyrics-language.ts
new file mode 100644
index 00000000..e5755f40
--- /dev/null
+++ b/src/lib/lyrics-language.ts
@@ -0,0 +1,52 @@
+/**
+ * The language a song's lyrics are written in: the script of the person's description says it, so a small model that
+ * answers in English by habit is told plainly (Hindi words in Devanagari get Hindi in Devanagari).
+ */
+
+/** Scripts by Unicode block, with the language most descriptions in them are written in. */
+const SCRIPTS: { script: string; language: string | null; range: RegExp }[] = [
+ { script: "Devanagari", language: null, range: /[ऀ-ॿ]/g },
+ { script: "Gujarati", language: "Gujarati", range: /[઀-૿]/g },
+ { script: "Bengali", language: null, range: /[ঀ-৿]/g },
+ { script: "Gurmukhi", language: "Punjabi", range: /[਀-੿]/g },
+ { script: "Odia", language: "Odia", range: /[଀-୿]/g },
+ { script: "Tamil", language: "Tamil", range: /[஀-௿]/g },
+ { script: "Telugu", language: "Telugu", range: /[ఀ-౿]/g },
+ { script: "Kannada", language: "Kannada", range: /[ಀ-೿]/g },
+ { script: "Malayalam", language: "Malayalam", range: /[ഀ-ൿ]/g },
+ { script: "Sinhala", language: "Sinhala", range: /[඀-෿]/g },
+ { script: "Thai", language: "Thai", range: /[฀-๿]/g },
+ { script: "Arabic", language: null, range: /[؀-ۿݐ-ݿ]/g },
+ { script: "Hebrew", language: "Hebrew", range: /[֐-׿]/g },
+ { script: "Cyrillic", language: null, range: /[Ѐ-ӿ]/g },
+ { script: "Greek", language: "Greek", range: /[Ͱ-Ͽ]/g },
+ { script: "Georgian", language: "Georgian", range: /[Ⴀ-ჿ]/g },
+ { script: "Armenian", language: "Armenian", range: /[԰-֏]/g },
+ { script: "Hangul", language: "Korean", range: /[가-힯ᄀ-ᇿ]/g },
+ { script: "Japanese kana", language: "Japanese", range: /[぀-ヿ]/g },
+ { script: "Chinese characters", language: null, range: /[一-鿿]/g },
+];
+
+/** Devanagari words that say Marathi or Nepali rather than Hindi. */
+const NOT_HINDI = /(आहे|आणि|मराठी|नेपाली)/;
+
+/**
+ * One sentence for the lyrics request: write in the description's language and script, when its letters are mostly in
+ * one non-Latin script. Null for a description in Latin letters (the model keeps its language from the words).
+ */
+export function lyricsLanguageHint(idea: string): string | null {
+ const letters = (idea.match(/\p{L}/gu) ?? []).length;
+ if (!letters) return null;
+ let best: { script: string; language: string | null; n: number } | null = null;
+ for (const s of SCRIPTS) {
+ const n = (idea.match(s.range) ?? []).length;
+ if (n && (!best || n > best.n)) best = { script: s.script, language: s.language, n };
+ }
+ // Japanese kana outnumber nothing when kanji are many: kana anywhere means Japanese.
+ if (best?.script === "Chinese characters" && /[぀-ヿ]/.test(idea)) best = { script: "Japanese kana", language: "Japanese", n: best.n };
+ if (!best || best.n < letters * 0.3) return null;
+ const language = best.script === "Devanagari" ? (NOT_HINDI.test(idea) ? null : "Hindi") : best.language;
+ const script = best.script === "Japanese kana" ? "Japanese script" : `${best.script} script`;
+ const what = language ? `${language}, in ${script}` : `the description's language, in ${script}`;
+ return `Write the lyrics in ${what}, not in English, unless the description asks for another language. Keep the section tags in English.`;
+}
diff --git a/src/server/enhance.ts b/src/server/enhance.ts
index 04e3e415..f9dd07ad 100644
--- a/src/server/enhance.ts
+++ b/src/server/enhance.ts
@@ -3,6 +3,7 @@ import Anthropic from "@anthropic-ai/sdk";
import type { MediaKind, Operation } from "@/lib/catalog/types";
import { writeCaption } from "@/lib/ideogram-caption";
import { normalizeLyrics } from "@/lib/lyrics";
+import { lyricsLanguageHint } from "@/lib/lyrics-language";
import { activeLocal, aiHelper, chatLocal, type JsonSpec } from "./llm";
import { engineMode, getSetting } from "./settings";
import { tenant } from "./tenant";
@@ -251,7 +252,8 @@ Rules:
- A common shape is Verse, Chorus, Verse, Chorus, Bridge, Chorus; repeat the chorus word for word.
- Short, singable lines of 5 to 10 words that rhyme or half-rhyme. Concrete images over abstractions.
- Only the words to be sung: no stage directions, no instruments, no chord names.
-- Keep the description's language, subject, mood and any words it quotes.`;
+- Write in the language and script of the description (Hindi in Devanagari, Gujarati in Gujarati script), unless it asks for another language. The section tags stay in English.
+- Keep the description's subject, mood and any words it quotes.`;
/**
* Lyrics for a song described in `idea`, about as long as `seconds` of music and at most `max` characters, from whichever
@@ -261,7 +263,9 @@ export async function writeLyrics(idea: string, o: { seconds?: number; max?: num
const seconds = o.seconds && o.seconds > 0 ? Math.min(600, o.seconds) : 120;
const sections = seconds <= 45 ? "one verse and one chorus" : seconds <= 100 ? "a verse, a chorus, a second verse and the chorus again" : "a full song (two or three verses, the chorus after each, a bridge)";
const room = o.max ? ` Stay under ${Math.floor(o.max * 0.9)} characters.` : "";
- const text = await llmText(LYRICS_SYSTEM, `Write lyrics for this song, about ${Math.round(seconds / 60 * 10) / 10} minutes long: ${sections}.${room}\n\n${idea.trim()}`, { task: "lyrics", waitMs: 75_000, ...o.helper });
+ // The script of the description says the language: a small model is told it again after the words.
+ const hint = lyricsLanguageHint(idea);
+ const text = await llmText(LYRICS_SYSTEM, `Write lyrics for this song, about ${Math.round(seconds / 60 * 10) / 10} minutes long: ${sections}.${room}\n\n${idea.trim()}${hint ? `\n\n${hint}` : ""}`, { task: "lyrics", waitMs: 75_000, ...o.helper });
const words = normalizeLyrics(text?.replace(/^```\w*\n?|```$/g, ""));
if (!words) return null;
return o.max && words.length > o.max ? trimLyrics(words, o.max) : words;
diff --git a/tests/lyrics-language.test.ts b/tests/lyrics-language.test.ts
new file mode 100644
index 00000000..e80b5a73
--- /dev/null
+++ b/tests/lyrics-language.test.ts
@@ -0,0 +1,27 @@
+import { describe, expect, it } from "vitest";
+import { lyricsLanguageHint } from "@/lib/lyrics-language";
+
+// The lyrics request names the language and script of the description, read from its letters.
+describe("the lyrics' language from the description's script", () => {
+ it("Hindi in Devanagari, Gujarati in Gujarati script", () => {
+ expect(lyricsLanguageHint("मानसून की पहली बारिश पर एक रोमांटिक बॉलीवुड गीत")).toBe(
+ "Write the lyrics in Hindi, in Devanagari script, not in English, unless the description asks for another language. Keep the section tags in English.",
+ );
+ expect(lyricsLanguageHint("નવરાત્રિ ગરબા પર એક ઉત્સાહી ગીત")).toMatch(/^Write the lyrics in Gujarati, in Gujarati script, not in English/);
+ });
+
+ it("other scripts, and Devanagari that isn't Hindi keeps the description's language", () => {
+ expect(lyricsLanguageHint("மழைக்கால காதல் பாடல்")).toMatch(/in Tamil, in Tamil script/);
+ expect(lyricsLanguageHint("비 오는 밤의 사랑 노래")).toMatch(/in Korean, in Hangul script/);
+ expect(lyricsLanguageHint("雨の夜のラブソング")).toMatch(/in Japanese, in Japanese script/);
+ expect(lyricsLanguageHint("पावसाळ्यातील प्रेमगीत आहे, मराठी")).toMatch(/in the description's language, in Devanagari script/);
+ expect(lyricsLanguageHint("Песня о дожде")).toMatch(/in the description's language, in Cyrillic script/);
+ });
+
+ it("no hint for Latin letters, a few borrowed words, or nothing", () => {
+ expect(lyricsLanguageHint("Upbeat synth-pop about driving through Mumbai at night")).toBeNull();
+ expect(lyricsLanguageHint("A Bollywood love song with the word प्यार in the chorus, sung in English")).toBeNull();
+ expect(lyricsLanguageHint("Canción de amor bajo la lluvia")).toBeNull();
+ expect(lyricsLanguageHint("120 bpm")).toBeNull();
+ });
+});
diff --git a/tests/text-helper.test.ts b/tests/text-helper.test.ts
index a06843d9..9dfeef3a 100644
--- a/tests/text-helper.test.ts
+++ b/tests/text-helper.test.ts
@@ -301,6 +301,24 @@ describe("the AI helper's order and the lyrics route", () => {
await slot.stop();
});
+ it("lyrics for a Gujarati or Hindi description are asked for in its language and script", async () => {
+ process.env.FUOBE_HOSTED_RUNS = "1";
+ const w = addWorker();
+ const asked: { system: string; user: string }[] = [];
+ const slot = standIn(w, (job) => {
+ asked.push({ system: job.system, user: job.user });
+ return "[Verse]\nગરબા રમીએ";
+ });
+ for (const prompt of ["નવરાત્રિ ગરબા પર એક ઉત્સાહી ગીત", "मानसून की पहली बारिश पर एक गीत", "A synth-pop song about rain"]) {
+ expect((await enhanceRoute.POST(post("/api/enhance", { prompt, target: "lyrics", seconds: 30 }))).status).toBe(200);
+ }
+ await slot.stop();
+ expect(asked[0].system).toContain("Write in the language and script of the description");
+ expect(asked[0].user).toMatch(/નવરાત્રિ[\s\S]*Write the lyrics in Gujarati, in Gujarati script, not in English/);
+ expect(asked[1].user).toContain("Write the lyrics in Hindi, in Devanagari script");
+ expect(asked[2].user).not.toContain("Write the lyrics in");
+ });
+
it("never a dead end: busy words for a visitor, the way to set one up for the owner, and the proxy without hosted runs", async () => {
process.env.FUOBE_HOSTED_RUNS = "1";
const busy = await enhanceRoute.POST(post("/api/enhance", { prompt: "a song", target: "lyrics" }));