Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Load the Hugging Face token from environment variables
|
7 |
+
hf_token = os.getenv("HF_TOKEN")
|
8 |
+
if not hf_token:
|
9 |
+
raise ValueError("Hugging Face token not found in environment variables.")
|
10 |
+
|
11 |
+
# Load the tokenizer and model
|
12 |
+
model_name = "sander-wood/music-transformer"
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_token)
|
14 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=hf_token)
|
15 |
+
|
16 |
+
# Move the model to GPU if available
|
17 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
18 |
+
model.to(device)
|
19 |
+
|
20 |
+
# Function to generate music
|
21 |
+
def generate_music(input_text, max_length=512, temperature=0.9, top_p=0.95):
|
22 |
+
"""
|
23 |
+
Generate music based on the input text.
|
24 |
+
"""
|
25 |
+
# Tokenize the input
|
26 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(device)
|
27 |
+
|
28 |
+
# Generate music
|
29 |
+
with torch.no_grad():
|
30 |
+
outputs = model.generate(
|
31 |
+
inputs["input_ids"],
|
32 |
+
max_length=max_length,
|
33 |
+
num_return_sequences=1,
|
34 |
+
temperature=temperature,
|
35 |
+
top_p=top_p,
|
36 |
+
do_sample=True,
|
37 |
+
)
|
38 |
+
|
39 |
+
# Decode the generated output
|
40 |
+
generated_music = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
41 |
+
return generated_music
|
42 |
+
|
43 |
+
# Gradio interface
|
44 |
+
def gradio_interface(input_text, max_length, temperature, top_p):
|
45 |
+
"""
|
46 |
+
Gradio interface for generating music.
|
47 |
+
"""
|
48 |
+
generated_music = generate_music(input_text, max_length, temperature, top_p)
|
49 |
+
return generated_music
|
50 |
+
|
51 |
+
# Define Gradio inputs and outputs
|
52 |
+
inputs = [
|
53 |
+
gr.Textbox(label="Input Text", placeholder="Enter a music description..."),
|
54 |
+
gr.Slider(minimum=64, maximum=1024, value=512, label="Max Length"),
|
55 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.9, label="Temperature"),
|
56 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, label="Top-p"),
|
57 |
+
]
|
58 |
+
|
59 |
+
outputs = gr.Textbox(label="Generated Music (ABC Notation)")
|
60 |
+
|
61 |
+
# Create the Gradio app
|
62 |
+
app = gr.Interface(
|
63 |
+
fn=gradio_interface,
|
64 |
+
inputs=inputs,
|
65 |
+
outputs=outputs,
|
66 |
+
title="Music Transformer",
|
67 |
+
description="Generate music in ABC notation using the sander-wood/music-transformer model.",
|
68 |
+
)
|
69 |
+
|
70 |
+
# Launch the app
|
71 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|