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|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langchain_core.prompts import PromptTemplate
|
| 3 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 4 |
+
from langchain_community.chat_models.friendli import ChatFriendli
|
| 5 |
+
from korean_process import generate_korean_lyrics
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
global selected_line_value
|
| 9 |
+
selected_line_value = 0
|
| 10 |
+
|
| 11 |
+
llm = ChatFriendli(
|
| 12 |
+
friendli_token = os.getenv("FRIENDLI_API_KEY"),
|
| 13 |
+
model = "meta-llama-3-70b-instruct",
|
| 14 |
+
streaming = False,
|
| 15 |
+
# temperature=0.5,
|
| 16 |
+
# max_tokens=100,
|
| 17 |
+
# top_p=0.9,
|
| 18 |
+
# frequency_penalty=0.0,
|
| 19 |
+
# stop=["\n"],
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
def process_english(title, lyric):
|
| 23 |
+
return switch_interface(title, lyric)
|
| 24 |
+
|
| 25 |
+
def process_korean(title, lyric):
|
| 26 |
+
lyric = generate_korean_lyrics(title, lyric)
|
| 27 |
+
return switch_interface(title, lyric)
|
| 28 |
+
|
| 29 |
+
def switch_interface(title, lyric):
|
| 30 |
+
numbered_lyric = ""
|
| 31 |
+
count = 0
|
| 32 |
+
lines = lyric.split('\n')
|
| 33 |
+
numbered_lines = []
|
| 34 |
+
for line in lines:
|
| 35 |
+
if len(line) > 0:
|
| 36 |
+
count += 1
|
| 37 |
+
numbered_lyric += f"{count}. {line}\n"
|
| 38 |
+
numbered_lines.append(line)
|
| 39 |
+
|
| 40 |
+
global stored_lyrics
|
| 41 |
+
stored_lyrics = numbered_lines
|
| 42 |
+
|
| 43 |
+
return (gr.update(visible=False), # audio_input1
|
| 44 |
+
gr.update(visible=False), # audio_input2
|
| 45 |
+
gr.update(visible=False), # title_input
|
| 46 |
+
gr.update(visible=False), # lyrics_input
|
| 47 |
+
gr.update(visible=False), # submit_english_btn
|
| 48 |
+
gr.update(visible=False), # submit_korean_btn
|
| 49 |
+
gr.update(visible=True, value=f"<p style='font-size:160%;'>{title}</p>"), # hidden_title
|
| 50 |
+
gr.update(visible=True, value=f"<p style='white-space: pre-wrap;'>{numbered_lyric}</p>"), # hidden_lyric
|
| 51 |
+
gr.update(visible=True, value=0, maximum=count, minimum=0, step=1), # selected_line
|
| 52 |
+
gr.update(visible=True, value="Choice a line you want to revise"), # selected_lyric
|
| 53 |
+
gr.update(visible=True), # feedback_text
|
| 54 |
+
gr.update(visible=True), # feedback_btn
|
| 55 |
+
gr.update(visible=True)) # new_btn
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def update_selected_lyric(selected_line):
|
| 59 |
+
selected_line_value = selected_line
|
| 60 |
+
if selected_line != 0:
|
| 61 |
+
return gr.update(value=f"Selected line ({selected_line}): {stored_lyrics[selected_line-1]}")
|
| 62 |
+
else:
|
| 63 |
+
return gr.update(value="Choose a line you want to revise using the slider!")
|
| 64 |
+
|
| 65 |
+
def reset_interface():
|
| 66 |
+
return (gr.update(visible=True), # audio_input1
|
| 67 |
+
gr.update(visible=True), # audio_input2
|
| 68 |
+
gr.update(value="", visible=True), # title_input
|
| 69 |
+
gr.update(value="", visible=True), # lyrics_input
|
| 70 |
+
gr.update(visible=True), # submit_english_btn
|
| 71 |
+
gr.update(visible=True), # submit_korean_btn
|
| 72 |
+
gr.update(visible=False), # hidden_title
|
| 73 |
+
gr.update(visible=False), # hidden_lyric
|
| 74 |
+
gr.update(value=0, visible=False), # selected_line
|
| 75 |
+
gr.update(value="", visible=False), # selected_lyric
|
| 76 |
+
gr.update(visible=False), # feedback_text
|
| 77 |
+
gr.update(visible=False), # feedback_btn
|
| 78 |
+
gr.update(visible=False)) # new_btn
|
| 79 |
+
|
| 80 |
+
def feedback(lyrics, line, feedback):
|
| 81 |
+
prompt = PromptTemplate.from_template("""Modify the target lyric to include the following feedback.
|
| 82 |
+
The sentence length must remain the same with the target line.
|
| 83 |
+
Answer with a single line of lyrics you created, and nothing else.
|
| 84 |
+
Whole Lyrics: {lyrics},
|
| 85 |
+
Target Lyric: {line},
|
| 86 |
+
Feedback: {feedback},
|
| 87 |
+
Your new lyric that modified the target line:""")
|
| 88 |
+
formatted_prompt = prompt.format(lyrics=lyrics, line=line, feedback=feedback)
|
| 89 |
+
|
| 90 |
+
feedback_chain = (
|
| 91 |
+
llm
|
| 92 |
+
| StrOutputParser()
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
result = feedback_chain.invoke(formatted_prompt)
|
| 96 |
+
return result
|
| 97 |
+
|
| 98 |
+
def apply_feedback(lyrics_input, selected_lyric, feedback_text):
|
| 99 |
+
if (selected_line_value == 0):
|
| 100 |
+
return (gr.update(), gr.update(), gr.update(value="Choose a line you want to revise using the slider!"))
|
| 101 |
+
modified_lyric = feedback(lyrics_input, selected_lyric, feedback_text)
|
| 102 |
+
stored_lyrics[selected_line_value-1] = modified_lyric
|
| 103 |
+
numbered_lyric = ""
|
| 104 |
+
for lyric in stored_lyrics:
|
| 105 |
+
numbered_lyric += f"{lyric}\n"
|
| 106 |
+
|
| 107 |
+
return (gr.update(value=f"<p style='white-space: pre-wrap;'>{numbered_lyric}</p>"),
|
| 108 |
+
gr.update(value=0),
|
| 109 |
+
gr.update(value=f"Feedback applied! Selected line ({selected_line}): {modified_lyric}"))
|
| 110 |
+
|
| 111 |
+
with gr.Blocks() as demo:
|
| 112 |
+
with gr.Row():
|
| 113 |
+
audio_input1 = gr.Audio(label="Original Audio", visible=True)
|
| 114 |
+
audio_input2 = gr.Audio(label="Voice Sample (Optional)", visible=True)
|
| 115 |
+
with gr.Row():
|
| 116 |
+
title_input = gr.Textbox(label="Song Title", visible=True)
|
| 117 |
+
lyrics_input = gr.Textbox(label="Song Lyrics", visible=True)
|
| 118 |
+
|
| 119 |
+
submit_english_btn = gr.Button("Make English Lyrics")
|
| 120 |
+
submit_korean_btn = gr.Button("Make Korean Lyrics")
|
| 121 |
+
|
| 122 |
+
hidden_title= gr.HTML(visible=False)
|
| 123 |
+
hidden_lyric= gr.HTML(visible=False)
|
| 124 |
+
selected_line= gr.Slider(visible=False, interactive=True)
|
| 125 |
+
selected_lyric= gr.HTML(visible=False)
|
| 126 |
+
feedback_text = gr.Textbox(label="Feedback", visible=False, interactive=True)
|
| 127 |
+
feedback_btn = gr.Button("Feedback", visible=False, interactive=True)
|
| 128 |
+
new_btn = gr.Button("Make New", visible=False)
|
| 129 |
+
|
| 130 |
+
submit_english_btn.click(
|
| 131 |
+
process_english,
|
| 132 |
+
[title_input, lyrics_input],
|
| 133 |
+
[audio_input1, audio_input2, title_input, lyrics_input, submit_english_btn, submit_korean_btn, hidden_title, hidden_lyric, selected_line, selected_lyric, feedback_text, feedback_btn, new_btn]
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
submit_korean_btn.click(
|
| 137 |
+
process_korean,
|
| 138 |
+
[title_input, lyrics_input],
|
| 139 |
+
[audio_input1, audio_input2, title_input, lyrics_input, submit_english_btn, submit_korean_btn, hidden_title, hidden_lyric, selected_line, selected_lyric, feedback_text, feedback_btn, new_btn]
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
selected_line.change(
|
| 143 |
+
update_selected_lyric,
|
| 144 |
+
[selected_line],
|
| 145 |
+
[selected_lyric]
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
feedback_btn.click(
|
| 149 |
+
apply_feedback,
|
| 150 |
+
[lyrics_input, selected_lyric, feedback_text],
|
| 151 |
+
[hidden_lyric, selected_line, selected_lyric]
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
new_btn.click(
|
| 155 |
+
reset_interface,
|
| 156 |
+
[],
|
| 157 |
+
[audio_input1, audio_input2, title_input, lyrics_input, submit_english_btn, submit_korean_btn, hidden_title, hidden_lyric, selected_line, selected_lyric, feedback_text, feedback_btn, new_btn]
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
demo.launch()
|