-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt_to_lyrics.py
49 lines (37 loc) · 1.48 KB
/
prompt_to_lyrics.py
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
from bardapi import Bard
from auth_keys import bard_api_key
import re, json
def remove_pattern_from_text(text):
pattern = r"\((.*?)\)"
text = re.sub(pattern, "", text)
if "**" in text:
pattern = r"\*\*(.*?)\*\*"
text = re.sub(pattern, "", text)
return text
def clean(str):
if "```" in str:
index = str.index("```")
return remove_pattern_from_text(str[index+3:-3])
elif ":" in str:
index = str.index(":")
return remove_pattern_from_text(str[index+1:])
def parse_json(json_string):
cleaned_string = re.sub(r"[\x00-\x1F\x7F]", "", json_string)
# Parse the cleaned JSON data
json_data = json.loads(cleaned_string)
return json_data
def promt_to_lyrics(prompt):
bard = Bard(token=bard_api_key)
raw_answer = bard.get_answer("Write lyrics for a song about about'"+prompt+"' of the lyrics, suitable tilte,bpm, and no of bars for the song, club all the things in json, the json file should have title, lyrics,bpm, bars. Only return json file and no extra text")['content']
raw_answer+="."
print(raw_answer)
json_str = raw_answer[raw_answer.index("{"):raw_answer.index("}")+1]
# print(json_str)
return parse_json(json_str)
if __name__ =="__main__":
prompt = input("Topic/decsription of the song: ")
jfile = promt_to_lyrics(prompt)
print(jfile["title"])
print(jfile["bars"])
print(jfile["lyrics"][0])
print(jfile["bpm"])