Spaces:
Sleeping
Sleeping
Initial commit: Gradio app and requirements
Browse files- app.py +36 -145
- requirements.txt +2 -4
app.py
CHANGED
|
@@ -1,161 +1,52 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import os
|
| 3 |
-
import shutil
|
| 4 |
-
import zipfile
|
| 5 |
-
import pathlib
|
| 6 |
-
import pandas as pd
|
| 7 |
import gradio as gr
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
import autogluon.tabular
|
| 11 |
-
import getpass
|
| 12 |
-
import shutil
|
| 13 |
-
from git import Repo as GitRepo
|
| 14 |
-
|
| 15 |
-
model_repo_id = "madhavkarthi/24679-HW2-tabular-autolguon-predictor"
|
| 16 |
-
zip_filename = "autogluon_predictor_dir.zip"
|
| 17 |
-
cache_dir = pathlib.Path("hf_assests")
|
| 18 |
-
extract_dir = cache_dir / "predictor_native"
|
| 19 |
-
|
| 20 |
-
def prepare_predictor_dir() -> str:
|
| 21 |
-
cache_dir.mkdir(parents=True, exist_ok=True)
|
| 22 |
-
local_zip = h.hf_hub_download(
|
| 23 |
-
repo_id=model_repo_id,
|
| 24 |
-
filename=zip_filename,
|
| 25 |
-
repo_type="model",
|
| 26 |
-
local_dir=str(cache_dir),
|
| 27 |
-
local_dir_use_symlinks=False,
|
| 28 |
-
)
|
| 29 |
-
if extract_dir.exists():
|
| 30 |
-
shutil.rmtree(extract_dir)
|
| 31 |
-
extract_dir.mkdir(parents=True, exist_ok=True)
|
| 32 |
-
with zipfile.ZipFile(local_zip, "r") as zf:
|
| 33 |
-
zf.extractall(str(extract_dir))
|
| 34 |
-
contents = list(extract_dir.iterdir())
|
| 35 |
-
predictor_root = contents[0] if (len(contents) == 1 and contents[0].is_dir()) else extract_dir
|
| 36 |
-
return str(predictor_root)
|
| 37 |
-
|
| 38 |
-
predictor_dir = prepare_predictor_dir()
|
| 39 |
-
predictor = autogluon.tabular.TabularPredictor.load(predictor_dir, require_py_version_match=False)
|
| 40 |
-
|
| 41 |
-
def do_predict(right_hand_notes, left_hand_notes, measures, Key_Center, marking):
|
| 42 |
-
|
| 43 |
-
row = {
|
| 44 |
-
feature_col[0]: int(right_hand_notes),
|
| 45 |
-
feature_col[1]: int(left_hand_notes),
|
| 46 |
-
feature_col[2]: int(measures),
|
| 47 |
-
feature_col[3]: int(Key_Center),
|
| 48 |
-
feature_col[4]: int(marking),
|
| 49 |
-
}
|
| 50 |
-
X = pd.DataFrame([row], columns=feature_col)
|
| 51 |
-
|
| 52 |
-
pred_series = predictor.predict(X)
|
| 53 |
-
raw_pred = pred_series.iloc[0]
|
| 54 |
-
|
| 55 |
-
try:
|
| 56 |
-
proba = predictor.predict_proba(X)
|
| 57 |
-
if isinstance(proba, pd.Series):
|
| 58 |
-
proba = proba.to_frame().T
|
| 59 |
-
except Exception:
|
| 60 |
-
proba = None
|
| 61 |
-
|
| 62 |
-
pred_label = human_label(raw_pred)
|
| 63 |
-
|
| 64 |
-
proba_dict = None
|
| 65 |
-
if proba is not None:
|
| 66 |
-
row0 = proba.iloc[0]
|
| 67 |
-
tmp = {}
|
| 68 |
-
for cls, val in row0.items():
|
| 69 |
-
key = human_label(cls)
|
| 70 |
-
tmp[key] = float(val) + float(tmp.get(key, 0.0))
|
| 71 |
-
proba_dict = dict(sorted(tmp.items(), key=lambda kv: kv[1], reverse=True))
|
| 72 |
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
key_center_mapping = {
|
| 76 |
0: "A", 1: "Bb", 2: "B", 3: "C", 4: "Db", 5: "D",
|
| 77 |
6: "Eb", 7: "E", 8: "F", 9: "Gb", 10: "G", 11: "Ab"
|
| 78 |
}
|
| 79 |
-
|
| 80 |
marking_mapping = {
|
| 81 |
-
0: "Minuet", 1: "Allegro", 2: "Andante",
|
| 82 |
-
4: "Allegretto", 5: "Dance"
|
| 83 |
}
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
gr.Markdown("# Classical Music Composer Classifier")
|
| 88 |
-
gr.Markdown(
|
| 89 |
-
"Predict whether a classical piano piece was composed by **Mozart** or **Beethoven** "
|
| 90 |
-
"based on musical characteristics."
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
with gr.Row():
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
label="Right Hand Notes",
|
| 98 |
-
info="Number of notes played by right hand"
|
| 99 |
-
)
|
| 100 |
-
left_hand_input = gr.Number(
|
| 101 |
-
value=100,
|
| 102 |
-
precision=0,
|
| 103 |
-
label="Left Hand Notes",
|
| 104 |
-
info="Number of notes played by left hand"
|
| 105 |
-
)
|
| 106 |
-
measures_input = gr.Number(
|
| 107 |
-
value=20,
|
| 108 |
-
precision=0,
|
| 109 |
-
label="Measures",
|
| 110 |
-
info="Number of musical measures"
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
with gr.Row():
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
-
choices=[(f"{v} ({k})", k) for k, v in marking_mapping.items()],
|
| 122 |
-
value=1,
|
| 123 |
-
label="Musical Marking",
|
| 124 |
-
info="Tempo/style marking of the piece"
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
predicted_composer_output = gr.Textbox(label="Predicted Composer")
|
| 128 |
-
proba_output = gr.Label(num_top_classes=2, label="Prediction Probabilities")
|
| 129 |
-
|
| 130 |
-
inputs_for_predict = [
|
| 131 |
-
right_hand_input, left_hand_input, measures_input,
|
| 132 |
-
key_center_input, marking_input
|
| 133 |
-
]
|
| 134 |
-
outputs_for_predict = [predicted_composer_output, proba_output]
|
| 135 |
-
|
| 136 |
-
for comp in inputs_for_predict:
|
| 137 |
-
comp.change(
|
| 138 |
-
fn=do_predict,
|
| 139 |
-
inputs=inputs_for_predict,
|
| 140 |
-
outputs=outputs_for_predict
|
| 141 |
-
)
|
| 142 |
-
|
| 143 |
-
gr.Examples(
|
| 144 |
-
examples=examples,
|
| 145 |
-
inputs=inputs_for_predict,
|
| 146 |
-
label="Example Pieces",
|
| 147 |
-
examples_per_page=5,
|
| 148 |
-
cache_examples=False,
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
-
gr.Markdown("""
|
| 152 |
-
### About the Features:
|
| 153 |
-
- **Right Hand Notes**: Count of notes played by the right hand
|
| 154 |
-
- **Left Hand Notes**: Count of notes played by the left hand
|
| 155 |
-
- **Measures**: Total number of musical measures in the piece
|
| 156 |
-
- **Key Center**: The musical key (A=0, Bb=1, B=2, C=3, Db=4, D=5, Eb=6, E=7, F=8, Gb=9, G=10, Ab=11)
|
| 157 |
-
- **Musical Marking**: Tempo/style indication (Minuet=0, Allegro=1, Andante=2, Moderato=3, Allegretto=4, Dance=5)
|
| 158 |
-
""")
|
| 159 |
-
|
| 160 |
-
if __name__ == "__main__":
|
| 161 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from autogluon.tabular import TabularPredictor
|
| 3 |
+
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load the model from the `model/` folder in this repo
|
| 6 |
+
predictor = TabularPredictor.load("model/")
|
| 7 |
|
| 8 |
key_center_mapping = {
|
| 9 |
0: "A", 1: "Bb", 2: "B", 3: "C", 4: "Db", 5: "D",
|
| 10 |
6: "Eb", 7: "E", 8: "F", 9: "Gb", 10: "G", 11: "Ab"
|
| 11 |
}
|
|
|
|
| 12 |
marking_mapping = {
|
| 13 |
+
0: "Minuet", 1: "Allegro", 2: "Andante",
|
| 14 |
+
3: "Moderato", 4: "Allegretto", 5: "Dance"
|
| 15 |
}
|
| 16 |
|
| 17 |
+
def predict_composer(rh, lh, measures, key_center, marking):
|
| 18 |
+
df = pd.DataFrame({
|
| 19 |
+
'right hand notes': [rh],
|
| 20 |
+
'left hand notes': [lh],
|
| 21 |
+
'measures': [measures],
|
| 22 |
+
'Key Center': [key_center],
|
| 23 |
+
'marking': [marking]
|
| 24 |
+
})
|
| 25 |
+
pred = predictor.predict(df)[0]
|
| 26 |
+
probs = predictor.predict_proba(df).iloc[0].to_dict()
|
| 27 |
+
return pred, probs
|
| 28 |
+
|
| 29 |
+
examples = [
|
| 30 |
+
[108, 82, 16, 3, 1],
|
| 31 |
+
[196, 136, 29, 2, 2],
|
| 32 |
+
[96, 49, 13, 2, 4],
|
| 33 |
+
[481, 561, 31, 5, 5],
|
| 34 |
+
[174, 129, 31, 2, 1],
|
| 35 |
+
]
|
| 36 |
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
gr.Markdown("# Classical Music Composer Classifier")
|
| 39 |
+
gr.Markdown("Predict whether a piece was composed by **Mozart** or **Beethoven**.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
with gr.Row():
|
| 41 |
+
rh = gr.Number(150, label="Right Hand Notes")
|
| 42 |
+
lh = gr.Number(100, label="Left Hand Notes")
|
| 43 |
+
measures = gr.Number(20, label="Measures")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
with gr.Row():
|
| 45 |
+
key_center = gr.Dropdown(list(key_center_mapping.keys()), value=3, label="Key Center")
|
| 46 |
+
marking = gr.Dropdown(list(marking_mapping.keys()), value=1, label="Marking")
|
| 47 |
+
out_label = gr.Textbox(label="Predicted Composer")
|
| 48 |
+
out_probs = gr.Label(num_top_classes=2, label="Probabilities")
|
| 49 |
+
for inp in [rh, lh, measures, key_center, marking]:
|
| 50 |
+
inp.change(fn=predict_composer, inputs=[rh, lh, measures, key_center, marking], outputs=[out_label, out_probs])
|
| 51 |
+
gr.Examples(examples, inputs=[rh, lh, measures, key_center, marking], outputs=[out_label, out_probs])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
-
gradio
|
|
|
|
| 2 |
pandas
|
| 3 |
-
numpy
|
| 4 |
-
pillow
|
| 5 |
-
autogluon.multimodal
|
| 6 |
huggingface_hub
|
|
|
|
| 1 |
+
gradio>=3.0
|
| 2 |
+
autogluon.tabular
|
| 3 |
pandas
|
|
|
|
|
|
|
|
|
|
| 4 |
huggingface_hub
|