File size: 1,476 Bytes
3d7f8a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from PIL import Image
from transformers import AutoProcessor, VisionEncoderDecoderModel

# Load model
processor = AutoProcessor.from_pretrained("truelitmus/omr-crnn")
model = VisionEncoderDecoderModel.from_pretrained("truelitmus/omr-crnn")

# Study course generator
def generate_study_course(note_string):
    note_list = note_string.split()
    unique_notes = sorted(set(note_list))

    course = {
        "Scales": {
            "All Notes": unique_notes,
            "Suggested Practice": [f"{note} Major Scale" for note in unique_notes]
        },
        "Practice Exercises": {
            "Sight Reading (4-note chunks)": [note_list[i:i+4] for i in range(0, len(note_list)-3, 4)],
            "Finger Exercises": [f"{note} Arpeggio" for note in unique_notes]
        }
    }
    return course

# Main function
def process_sheet(image: Image.Image):
    inputs = processor(images=image.convert("RGB"), return_tensors="pt")
    output = model.generate(**inputs)
    notes = processor.batch_decode(output, skip_special_tokens=True)[0]
    return generate_study_course(notes)

# Gradio interface
demo = gr.Interface(
    fn=process_sheet,
    inputs=gr.Image(type="pil", label="Upload Sheet Music Image"),
    outputs=gr.JSON(label="Generated Study Course"),
    title="🎼 Sheet Music Study AI",
    description="Upload a sheet music image to detect notes and receive a structured study course with scales and practice suggestions.",
)

demo.launch()