File size: 1,371 Bytes
47b9cf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dad3b36
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
import torch
from transformers import T5Tokenizer, T5ForConditionalGeneration
import gradio as gr

# Load the saved model and tokenizer
model_path = "trained_model"
model = T5ForConditionalGeneration.from_pretrained(model_path)
tokenizer = T5Tokenizer.from_pretrained(model_path)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)

# Define the function to generate a sequence based on the input text
def generate_sequence(input_text):
    test_input_tokens = tokenizer(input_text, return_tensors='pt').to(device)

    model.eval()
    with torch.no_grad():
        test_output_tokens = model.generate(test_input_tokens['input_ids'], num_return_sequences=1)

    test_output_sequence = tokenizer.decode(test_output_tokens[0], skip_special_tokens=True)
    return test_output_sequence

# Create a Gradio interface
iface = gr.Interface(
    fn=generate_sequence,
    inputs=gr.inputs.Textbox(lines=3, label="Input Text"),
    outputs=gr.outputs.Textbox(label="Generated Sequence"),
    title="MIDI Sequence Generator",
    description="Generate a MIDI sequence based on a text description",
    examples=[
        "A popular classical piano piece composed by Ludwig van Beethoven",
        "A beautiful and melancholic classical piano piece composed by Frédéric Chopin",
    ]
)

# Launch the Gradio interface
iface.launch(debug=True)