Blane187 commited on
Commit
ac768e2
·
verified ·
1 Parent(s): 96e3ec8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ from transformers import pipeline
4
+
5
+ def load_model():
6
+ model_ckpt = "flax-community/gpt2-rap-lyric-generator"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_ckpt, from_flax=True)
8
+ model = AutoModelForCausalLM.from_pretrained(model_ckpt, from_flax=True)
9
+ return tokenizer, model
10
+
11
+ def load_rappers():
12
+ with open("rappers.txt") as text_file:
13
+ rappers = text_file.readlines()
14
+ rappers = [name.strip() for name in rappers]
15
+ rappers.sort()
16
+ return rappers
17
+
18
+ tokenizer, model = load_model()
19
+ text_generation = pipeline("text-generation", model=model, tokenizer=tokenizer)
20
+
21
+ def generate_lyrics(artist, song_name):
22
+ prefix_text = f"<BOS>{song_name} [Verse 1:{artist}]"
23
+ generated_song = text_generation(prefix_text, max_length=750, do_sample=True)[0]
24
+ lyrics = ""
25
+ for count, line in enumerate(generated_song['generated_text'].split("\n")):
26
+ if "<EOS>" in line:
27
+ break
28
+ if count == 0:
29
+ lyrics += f"**{line[line.find('['):]}**\n"
30
+ continue
31
+ if "<BOS>" in line:
32
+ lyrics += f"{line[5:]}\n"
33
+ continue
34
+ if line.startswith("["):
35
+ lyrics += f"**{line}**\n"
36
+ continue
37
+ lyrics += f"{line}\n"
38
+ return lyrics
39
+
40
+ list_of_rappers = load_rappers()
41
+
42
+ interface = gr.Blocks(theme="Blane187/fuchsia")
43
+
44
+ with interface:
45
+ gr.Markdown("# Rap Lyrics Generator")
46
+ artist = gr.Dropdown(choices=list_of_rappers, label="Choose your rapper", value=list_of_rappers[-1])
47
+ song_name = gr.Textbox(label="Enter the desired song name", value="Sadboys")
48
+ generate_button = gr.Button("Generate lyrics")
49
+ output = gr.Markdown()
50
+
51
+ generate_button.click(generate_lyrics, inputs=[artist, song_name], outputs=output)
52
+
53
+ interface.launch()