Spaces:
Sleeping
Sleeping
added the style fix to the js
Browse files
app.py
CHANGED
|
@@ -1,48 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
import json
|
|
|
|
| 4 |
|
| 5 |
-
# ✅ Create FastAPI App
|
| 6 |
app = FastAPI()
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
note = midi_data["note"]
|
| 14 |
-
velocity = midi_data["velocity"]
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
generated_note = (note + 5) % 128
|
| 20 |
-
generated_velocity = min(velocity + 10, 127)
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
|
|
|
| 28 |
|
| 29 |
@app.post("/midi_phrase")
|
| 30 |
async def process_midi_phrase(request: Request):
|
| 31 |
try:
|
| 32 |
data = await request.json()
|
| 33 |
-
phrase = data["phrase"]
|
| 34 |
|
| 35 |
print(f"🎹 Received MIDI Phrase ({len(phrase)} notes)")
|
| 36 |
|
| 37 |
-
# 🚀
|
| 38 |
generated_phrase = [
|
| 39 |
{
|
| 40 |
-
"note": (note["note"] +
|
| 41 |
"velocity": note["velocity"],
|
| 42 |
-
"duration": note["duration"],
|
| 43 |
-
"inter_onset": note["inter_onset"]
|
|
|
|
| 44 |
}
|
| 45 |
-
for note in phrase
|
| 46 |
]
|
| 47 |
|
| 48 |
return {"status": "success", "generated_phrase": generated_phrase}
|
|
@@ -51,6 +59,7 @@ async def process_midi_phrase(request: Request):
|
|
| 51 |
print(f"🚨 Error processing MIDI phrase: {str(e)}")
|
| 52 |
return {"status": "error", "message": str(e)}
|
| 53 |
|
|
|
|
| 54 |
# ✅ JavaScript Code Embedded as a String
|
| 55 |
midi_js = """
|
| 56 |
<script>
|
|
@@ -243,14 +252,19 @@ function playGeneratedPhrase(phrase) {
|
|
| 243 |
</div>
|
| 244 |
"""
|
| 245 |
|
| 246 |
-
|
| 247 |
with gr.Blocks() as demo:
|
| 248 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
-
# ✅ Mount FastAPI with Gradio
|
| 251 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 252 |
|
| 253 |
if __name__ == "__main__":
|
| 254 |
import uvicorn
|
| 255 |
print("🚀 Starting FastAPI with Gradio...")
|
| 256 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
import json
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
|
|
|
|
| 6 |
app = FastAPI()
|
| 7 |
+
transpose_amount = 0
|
| 8 |
|
| 9 |
+
def update_transposition(value):
|
| 10 |
+
global transpose_amount
|
| 11 |
+
transpose_amount = value
|
| 12 |
+
print(f"🎚️ Transposition set to: {transpose_amount}")
|
| 13 |
+
return f"Transposition: {transpose_amount}"
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def generate_piano_roll(phrase):
|
| 16 |
+
""" Generate a simple piano roll plot from MIDI phrase data """
|
| 17 |
+
if not phrase:
|
| 18 |
+
return None
|
| 19 |
|
| 20 |
+
fig, ax = plt.subplots(figsize=(10, 4))
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# ✅ Convert phrase data into a piano roll format
|
| 23 |
+
for note in phrase:
|
| 24 |
+
start_time = sum(n["inter_onset"] for n in phrase[:phrase.index(note)]) # Calculate note start time
|
| 25 |
+
ax.broken_barh([(start_time, note["duration"])], (note["note"] - 0.5, 1),
|
| 26 |
+
facecolors='blue')
|
| 27 |
|
| 28 |
+
ax.set_xlabel("Time (ms)")
|
| 29 |
+
ax.set_ylabel("MIDI Note")
|
| 30 |
+
ax.set_yticks(range(21, 109, 12)) # Show octaves
|
| 31 |
+
ax.set_ylim(20, 110)
|
| 32 |
+
ax.grid(True)
|
| 33 |
|
| 34 |
+
return fig
|
| 35 |
|
| 36 |
@app.post("/midi_phrase")
|
| 37 |
async def process_midi_phrase(request: Request):
|
| 38 |
try:
|
| 39 |
data = await request.json()
|
| 40 |
+
phrase = data["phrase"]
|
| 41 |
|
| 42 |
print(f"🎹 Received MIDI Phrase ({len(phrase)} notes)")
|
| 43 |
|
| 44 |
+
# 🚀 Transpose and calculate start times
|
| 45 |
generated_phrase = [
|
| 46 |
{
|
| 47 |
+
"note": (note["note"] + transpose_amount) % 128,
|
| 48 |
"velocity": note["velocity"],
|
| 49 |
+
"duration": note["duration"],
|
| 50 |
+
"inter_onset": note["inter_onset"],
|
| 51 |
+
"start_time": sum(n["inter_onset"] for n in phrase[:i]) # Calculate start time
|
| 52 |
}
|
| 53 |
+
for i, note in enumerate(phrase)
|
| 54 |
]
|
| 55 |
|
| 56 |
return {"status": "success", "generated_phrase": generated_phrase}
|
|
|
|
| 59 |
print(f"🚨 Error processing MIDI phrase: {str(e)}")
|
| 60 |
return {"status": "error", "message": str(e)}
|
| 61 |
|
| 62 |
+
|
| 63 |
# ✅ JavaScript Code Embedded as a String
|
| 64 |
midi_js = """
|
| 65 |
<script>
|
|
|
|
| 252 |
</div>
|
| 253 |
"""
|
| 254 |
|
| 255 |
+
|
| 256 |
with gr.Blocks() as demo:
|
| 257 |
+
gr.Markdown("# 🎵 MIDI Processing App")
|
| 258 |
+
slider = gr.Slider(-12, 12, step=1, value=0, label="Transposition", interactive=True)
|
| 259 |
+
output_text = gr.Textbox("Transposition: 0", label="Selected Transposition", interactive=False)
|
| 260 |
+
piano_roll = gr.Plot(label="🎹 Piano Roll Visualization")
|
| 261 |
+
|
| 262 |
+
slider.change(fn=update_transposition, inputs=slider, outputs=output_text)
|
| 263 |
+
demo.load(generate_piano_roll, inputs=[], outputs=[piano_roll])
|
| 264 |
|
|
|
|
| 265 |
app = gr.mount_gradio_app(app, demo, path="/")
|
| 266 |
|
| 267 |
if __name__ == "__main__":
|
| 268 |
import uvicorn
|
| 269 |
print("🚀 Starting FastAPI with Gradio...")
|
| 270 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|