Spaces:
Build error
Build error
Create korean_process.py
Browse files- korean_process.py +115 -0
korean_process.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import dotenv
|
| 3 |
+
import openai
|
| 4 |
+
import random
|
| 5 |
+
import make_dataset
|
| 6 |
+
import re
|
| 7 |
+
from langchain_core.prompts import PromptTemplate
|
| 8 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 9 |
+
from langchain_community.chat_models.friendli import ChatFriendli
|
| 10 |
+
|
| 11 |
+
dotenv.load_dotenv()
|
| 12 |
+
|
| 13 |
+
llm = ChatFriendli(
|
| 14 |
+
friendli_token = os.getenv("FRIENDLI_API_KEY"),
|
| 15 |
+
model = "meta-llama-3-70b-instruct",
|
| 16 |
+
streaming = False,
|
| 17 |
+
# temperature=0.5,
|
| 18 |
+
# max_tokens=100,
|
| 19 |
+
# top_p=0.9,
|
| 20 |
+
# frequency_penalty=0.0,
|
| 21 |
+
# stop=["\n"],
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
frame_prompt = """Suggest a single line of Korean lyric that matches with given syllables,lyrics, and title.
|
| 25 |
+
Ensure to avoid repeating previous lyrics. Focus on creative and original expression.
|
| 26 |
+
Match the length of the sentence to the syllables I provide as closely as possible.
|
| 27 |
+
For example, if Syllables: 7 given, you should write a 6~8 letter korean sentence.
|
| 28 |
+
Your answer should feel like soft, trendy K-pop lyrics without any profanity.
|
| 29 |
+
Your answer should be short, and only composed with a single sentence.
|
| 30 |
+
Answer with a single line of lyrics you created, and nothing else.
|
| 31 |
+
|
| 32 |
+
Here,
|
| 33 |
+
Title: {title}
|
| 34 |
+
Mood and Symbol: {mood}
|
| 35 |
+
Previous Lyrics: {lyric}
|
| 36 |
+
Syllables: {syllables}
|
| 37 |
+
|
| 38 |
+
Your korean lyric that should be added to the previous lyrics:
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
client = openai.OpenAI(base_url="https://inference.friendli.ai/dedicated/v1", api_key=os.getenv("FRIENDLI_API_KEY"))
|
| 42 |
+
|
| 43 |
+
def update_language(current_language):
|
| 44 |
+
if random.random() < 0.8:
|
| 45 |
+
return current_language
|
| 46 |
+
else:
|
| 47 |
+
return random.choice(make_dataset.languages)
|
| 48 |
+
|
| 49 |
+
def extract_message(text, ending):
|
| 50 |
+
pattern = re.escape(ending) + r'([^"\n]+)'
|
| 51 |
+
match = re.search(pattern, text)
|
| 52 |
+
if match:
|
| 53 |
+
message = match.group(1).strip()
|
| 54 |
+
return message.strip('"')
|
| 55 |
+
return ""
|
| 56 |
+
|
| 57 |
+
def find_mood(lyric):
|
| 58 |
+
prompt = PromptTemplate.from_template("find mood and symbolic keyword of the given song. one sentence each. song: {lyric}. Mood and symbol: ")
|
| 59 |
+
formatted_prompt = prompt.format(lyric=lyric)
|
| 60 |
+
|
| 61 |
+
feedback_chain = (
|
| 62 |
+
llm
|
| 63 |
+
| StrOutputParser()
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
return feedback_chain.invoke(formatted_prompt)
|
| 67 |
+
|
| 68 |
+
def generate_korean_lyrics(title, lyric):
|
| 69 |
+
"""
|
| 70 |
+
prompt = make_dataset.frame_prompt.format(title=title, lyric=lyric, syllables=make_dataset.count_syllables(lyric))
|
| 71 |
+
response = client.chat.completions.create(
|
| 72 |
+
model=os.getenv("KR_ENDPOINT"),
|
| 73 |
+
messages=[
|
| 74 |
+
{"role": "system", "content": f"You are an assistant of lyricist"},
|
| 75 |
+
|
| 76 |
+
{"role": "user", "content": prompt},
|
| 77 |
+
],
|
| 78 |
+
)
|
| 79 |
+
return response.choices[0].message.content
|
| 80 |
+
"""
|
| 81 |
+
lines = lyric.split('\n')
|
| 82 |
+
result = ""
|
| 83 |
+
mood = find_mood(lyric)
|
| 84 |
+
print(mood)
|
| 85 |
+
for line in lines:
|
| 86 |
+
if len(line) > 0:
|
| 87 |
+
candidate = ""
|
| 88 |
+
candidate_count = 7
|
| 89 |
+
syllables = make_dataset.count_syllables(line)
|
| 90 |
+
for i in range(1):
|
| 91 |
+
#print("Original:", line)
|
| 92 |
+
response = client.chat.completions.create(
|
| 93 |
+
model=os.getenv("KR_ENDPOINT"),
|
| 94 |
+
messages=[
|
| 95 |
+
{"role": "system", "content": f"You are an assistant of lyricist"},
|
| 96 |
+
{"role": "user", "content": frame_prompt.format(title=title, lyric=result, syllables=syllables, mood=mood)},
|
| 97 |
+
],
|
| 98 |
+
)
|
| 99 |
+
suggestion = response.choices[0].message.content
|
| 100 |
+
count = make_dataset.count_syllables(suggestion)
|
| 101 |
+
if count == syllables:
|
| 102 |
+
#print(f"[Success!] Suggestion {i}:", suggestion)
|
| 103 |
+
candidate = suggestion
|
| 104 |
+
break
|
| 105 |
+
elif count < syllables:
|
| 106 |
+
if syllables - count < candidate_count:
|
| 107 |
+
candidate = suggestion
|
| 108 |
+
candidate_count = syllables - count
|
| 109 |
+
else:
|
| 110 |
+
if count - syllables < candidate_count:
|
| 111 |
+
candidate = suggestion
|
| 112 |
+
candidate_count = count - syllables
|
| 113 |
+
#print(f"[Fail!] Suggestion {i}:", suggestion)
|
| 114 |
+
result += candidate + '\n'
|
| 115 |
+
return result
|