rapGPT / app.py
hugojarkoff's picture
Update app.py
30621c0 verified
Raw
History Blame Contribute Delete
2.68 kB
import random
import torch
from rapgpt.config import Config
from rapgpt.encoder import Encoder
from rapgpt.model import HFHubTransformerModel
import gradio as gr
from huggingface_hub import hf_hub_download
if __name__ == "__main__":
artists_tokens = hf_hub_download(
repo_id="hugojarkoff/rapgpt", filename="artists_tokens.txt", repo_type="model"
)
config_file = hf_hub_download(
repo_id="hugojarkoff/rapgpt", filename="config.toml", repo_type="model"
)
with open(artists_tokens, "r") as f:
artists_tokens = {
line.split(":")[0].title(): int(line.split(":")[1].rstrip("\n"))
for line in f
}
artists_tokens = dict(sorted(artists_tokens.items()))
config = Config.load_from_toml(config_file)
encoder = Encoder(config=config)
model = HFHubTransformerModel.from_pretrained("hugojarkoff/rapgpt")
def predict(
lyrics_prompt: str,
new_tokens: int,
artist_token: int,
seed: int = 42,
):
# Set Seed
random.seed(seed)
torch.manual_seed(seed)
# Predict
sample_input = encoder.encode_data(lyrics_prompt)
sample_input = torch.tensor(sample_input).unsqueeze(0)
output = model.generate(
x=sample_input,
new_tokens=new_tokens,
artist_token=artist_token,
)
return encoder.decode_data(output[0].tolist())
gradio_app = gr.Interface(
predict,
inputs=[
gr.Textbox(
value="Ekip",
label="Lyrics prompt",
info="rapGPT will continue this prompt",
),
gr.Number(
value=50,
maximum=100,
label="New tokens to generate",
info="Number of new tokens to generate (limited to 100)",
),
gr.Dropdown(
value="Freeze Corleone",
choices=artists_tokens.keys(),
type="index",
label="Artist",
info="Which artist style to generate",
),
gr.Number(
value=1234, label="Random seed", info="Change for different results"
),
],
outputs=[gr.TextArea(label="Generated Lyrics")],
title="rapGPT",
description="Generate rap lyrics in the style of your favorite artists",
article=("Training code available on [GitHub](https://github.com/hugojarkoff/rapgpt). "
"Inference might be as slow as ~1s/token as code is mostly educational (not production-ready) + running on HF free tier CPU"),
)
gradio_app.launch()