File size: 2,280 Bytes
b06cda9
 
 
c164a3d
9a23bd3
b06cda9
 
 
 
c164a3d
b06cda9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57

hf_token="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"

from transformers import AutoModelForCausalLM, AutoTokenizer
# from transformers.src.transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "meta-llama/Llama-2-13b-chat-hf"

# load the model using 4bit quantization (https://huggingface.co/blog/4bit-transformers-bitsandbytes)
model = AutoModelForCausalLM.from_pretrained(model_id, load_in_4bit=True, use_auth_token=hf_token)
# disable Tensor Parallelism (https://github.com/huggingface/transformers/pull/24906)
model.config.pretraining_tp=1

tokenizer = AutoTokenizer.from_pretrained (model_id, use_auth_token=hf_token)

def extract_lyrics(text):
    start_index = text.find("Verse 1:")
    end_index = text.find("</s>")
    if start_index == -1:
        return text

    text = text[start_index:end_index].strip()

    text = text.replace("</s>", "")

    return text

def generate_lyrics_test(content, sentiment):
    # 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()
    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()

    input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to("cuda")
    outputs = model.generate(input_ids, max_length=500)
    return extract_lyrics(tokenizer.decode(outputs[0]))

import gradio as gr

demo = gr.Interface(
    generate_lyrics_test,
    [
        gr.Textbox(lines=5, label = "Content"),
        gr.Dropdown(
            ["Positive", "Negative", "Neutral"], label = "Sentiment"
        ),
    ],
    "text"
)

demo.queue().launch( )

sentiment = 'Positive'
# content = 'create a similar lyric to beat it from michael jackson and write something motivational instead'
content = 'create a similar lyric to beat it from Michael Jackson and write something motivational instead'
# genre = "Rock"
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()
print('input_text:', input_text)