pachet commited on
Commit
e826329
·
1 Parent(s): f0987a6

Add first version file

Browse files
Files changed (2) hide show
  1. app.py +73 -4
  2. first_version.py +76 -0
app.py CHANGED
@@ -1,7 +1,76 @@
1
  import gradio as gr
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import mido
3
+ from mido import Message, MidiFile, MidiTrack
4
+ import numpy as np
5
+ import os
6
 
 
 
7
 
8
+ def generate_chords(hexachord):
9
+ # Placeholder for your actual chord generation function
10
+ # Assuming hexachord is a list of MIDI note numbers
11
+ chords = []
12
+ for i in range(4): # Generate 4 chords
13
+ chords.append([note + (i * 2) for note in hexachord]) # Simple transposition
14
+ return chords
15
+
16
+
17
+ def create_midi(chords):
18
+ mid = MidiFile()
19
+ track = MidiTrack()
20
+ mid.tracks.append(track)
21
+
22
+ for chord in chords:
23
+ for note in chord:
24
+ track.append(Message('note_on', note=int(note), velocity=64, time=0))
25
+ for note in chord:
26
+ track.append(Message('note_off', note=int(note), velocity=64, time=480))
27
+
28
+ midi_path = "output.mid"
29
+ mid.save(midi_path)
30
+ return midi_path
31
+
32
+
33
+ def process_hexachord(note1, note2, note3, note4, note5, note6):
34
+ notes = [note1, note2, note3, note4, note5, note6]
35
+ notes = [int(note) for note in notes if note is not None] # Convert to int, remove None values
36
+
37
+ if len(notes) != 6 or len(set(notes)) != 6:
38
+ return "Please select exactly 6 unique notes."
39
+
40
+ chords = generate_chords(notes)
41
+ midi_path = create_midi(chords)
42
+ return midi_path
43
+
44
+
45
+ # UI Components
46
+ note_options = list(range(21, 109)) # MIDI note numbers
47
+ default_notes = [60, 62, 64, 65, 67, 69] # Default MIDI notes for C major hexachord
48
+
49
+ with gr.Blocks() as ui:
50
+ gr.Markdown("# Hexachord-based Chord Generator")
51
+
52
+ with gr.Row():
53
+ note_inputs = [gr.Dropdown(choices=note_options, label=f"Note {i + 1}", value=default_notes[i]) for i in
54
+ range(6)]
55
+
56
+ generate_button = gr.Button("Generate Chords")
57
+ midi_output = gr.File(label="Generated MIDI")
58
+
59
+ generate_button.click(
60
+ fn=process_hexachord,
61
+ inputs=note_inputs,
62
+ outputs=[midi_output]
63
+ )
64
+
65
+ # Detect if running on Hugging Face Spaces
66
+ on_huggingface = "HUGGINGFACE_SPACE" in os.environ
67
+
68
+
69
+ def launch_app():
70
+ if on_huggingface:
71
+ ui.launch(server_name="0.0.0.0", server_port=7860)
72
+ else:
73
+ ui.launch()
74
+
75
+
76
+ launch_app()
first_version.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import mido
3
+ from mido import Message, MidiFile, MidiTrack
4
+ import numpy as np
5
+ import os
6
+
7
+
8
+ def generate_chords(hexachord):
9
+ # Placeholder for your actual chord generation function
10
+ # Assuming hexachord is a list of MIDI note numbers
11
+ chords = []
12
+ for i in range(4): # Generate 4 chords
13
+ chords.append([note + (i * 2) for note in hexachord]) # Simple transposition
14
+ return chords
15
+
16
+
17
+ def create_midi(chords):
18
+ mid = MidiFile()
19
+ track = MidiTrack()
20
+ mid.tracks.append(track)
21
+
22
+ for chord in chords:
23
+ for note in chord:
24
+ track.append(Message('note_on', note=int(note), velocity=64, time=0))
25
+ for note in chord:
26
+ track.append(Message('note_off', note=int(note), velocity=64, time=480))
27
+
28
+ midi_path = "output.mid"
29
+ mid.save(midi_path)
30
+ return midi_path
31
+
32
+
33
+ def process_hexachord(note1, note2, note3, note4, note5, note6):
34
+ notes = [note1, note2, note3, note4, note5, note6]
35
+ notes = [int(note) for note in notes if note is not None] # Convert to int, remove None values
36
+
37
+ if len(notes) != 6 or len(set(notes)) != 6:
38
+ return "Please select exactly 6 unique notes."
39
+
40
+ chords = generate_chords(notes)
41
+ midi_path = create_midi(chords)
42
+ return midi_path
43
+
44
+
45
+ # UI Components
46
+ note_options = list(range(21, 109)) # MIDI note numbers
47
+ default_notes = [60, 62, 64, 65, 67, 69] # Default MIDI notes for C major hexachord
48
+
49
+ with gr.Blocks() as ui:
50
+ gr.Markdown("# Hexachord-based Chord Generator")
51
+
52
+ with gr.Row():
53
+ note_inputs = [gr.Dropdown(choices=note_options, label=f"Note {i + 1}", value=default_notes[i]) for i in
54
+ range(6)]
55
+
56
+ generate_button = gr.Button("Generate Chords")
57
+ midi_output = gr.File(label="Generated MIDI")
58
+
59
+ generate_button.click(
60
+ fn=process_hexachord,
61
+ inputs=note_inputs,
62
+ outputs=[midi_output]
63
+ )
64
+
65
+ # Detect if running on Hugging Face Spaces
66
+ on_huggingface = "HUGGINGFACE_SPACE" in os.environ
67
+
68
+
69
+ def launch_app():
70
+ if on_huggingface:
71
+ ui.launch(server_name="0.0.0.0", server_port=7860)
72
+ else:
73
+ ui.launch()
74
+
75
+
76
+ launch_app()