trishv commited on
Commit
b06cda9
·
1 Parent(s): a3d42ad
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ hf_token="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"
3
+
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+
6
+ model_id = "meta-llama/Llama-2-13b-chat-hf"
7
+
8
+ # load the model using 4bit quantization (https://huggingface.co/blog/4bit-transformers-bitsandbytes)
9
+ model = AutoModelForCausalLM.from_pretrained(model_id, load_in_4bit=True, use_auth_token=hf_token)
10
+ # disable Tensor Parallelism (https://github.com/huggingface/transformers/pull/24906)
11
+ model.config.pretraining_tp=1
12
+
13
+ tokenizer = AutoTokenizer.from_pretrained (model_id, use_auth_token=hf_token)
14
+
15
+ def extract_lyrics(text):
16
+ start_index = text.find("Verse 1:")
17
+ end_index = text.find("</s>")
18
+ if start_index == -1:
19
+ return text
20
+
21
+ text = text[start_index:end_index].strip()
22
+
23
+ text = text.replace("</s>", "")
24
+
25
+ return text
26
+
27
+ def generate_lyrics_test(content, sentiment):
28
+ # input_text = "Generate lyrics in standard song format that matches with following requirements. Content should be " + content + ". Genre should be " + genre + ". Sentiment of the song should be " + sentiment.lower()
29
+ input_text = "Generate lyrics in standard song format that matches with following requirements. Content should be " + content + ". Sentiment of the song should be " + sentiment.lower()
30
+
31
+ input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to("cuda")
32
+ outputs = model.generate(input_ids, max_length=500)
33
+ return extract_lyrics(tokenizer.decode(outputs[0]))
34
+
35
+ import gradio as gr
36
+
37
+ demo = gr.Interface(
38
+ generate_lyrics_test,
39
+ [
40
+ gr.Textbox(lines=5, label = "Content"),
41
+ gr.Dropdown(
42
+ ["Positive", "Negative", "Neutral"], label = "Sentiment"
43
+ ),
44
+ ],
45
+ "text"
46
+ )
47
+
48
+ demo.queue().launch( )
49
+
50
+ sentiment = 'Positive'
51
+ # content = 'create a similar lyric to beat it from michael jackson and write something motivational instead'
52
+ content = 'create a similar lyric to beat it from Michael Jackson and write something motivational instead'
53
+ # genre = "Rock"
54
+ input_text = "Generate lyrics in standard song format that matches with following requirements. Content should be " + content + ". Sentiment of the song should be " + sentiment.lower()
55
+ print('input_text:', input_text)