pachet commited on
Commit
1490a6f
·
1 Parent(s): e75a900

added the style fix to the js

Browse files
Files changed (1) hide show
  1. app.py +26 -38
app.py CHANGED
@@ -1,73 +1,61 @@
1
  import gradio as gr
2
  from fastapi import FastAPI, Request
3
  import json
4
- import uvicorn
5
 
6
  # ✅ Create FastAPI App
7
  app = FastAPI()
8
 
9
- # ✅ MIDI Processing Function
10
- @app.post("/midi_input")
11
- async def process_midi(request: Request):
12
- try:
13
- midi_data = await request.json()
14
- note = midi_data["note"]
15
- velocity = midi_data["velocity"]
16
-
17
- print(f"🎹 Received MIDI Note: {note}, Velocity: {velocity}")
18
-
19
- # 🚀 Process MIDI: Transpose + Increase Velocity
20
- generated_note = (note + 3) % 128
21
- generated_velocity = min(velocity + 10, 127)
22
-
23
- return {"status": "success", "generated_note": generated_note, "generated_velocity": generated_velocity}
24
- except Exception as e:
25
- return {"status": "error", "message": str(e)}
26
-
27
- # ✅ JavaScript for MIDI
28
  midi_js = """
29
  <script>
30
- console.log("JavaScript is running");
31
 
 
32
  navigator.requestMIDIAccess()
33
  .then(access => {
34
  console.log("✅ MIDI Access Granted!");
35
- const inputs = access.inputs.values();
36
- for (let input of inputs) {
37
- input.onmidimessage = handleMIDIMessage;
38
- }
39
  })
40
  .catch(err => console.error("🚨 MIDI API Error:", err));
41
 
42
- function handleMIDIMessage(event) {
43
- let [command, note, velocity] = event.data;
44
- if (command === 144 && velocity > 0) {
45
- console.log(`🎤 Sending MIDI Note: ${note}, Velocity: ${velocity}`);
46
- sendMIDI(note, velocity);
47
- }
48
- }
49
-
50
- function sendMIDI(note, velocity) {
51
  fetch("/midi_input", {
52
  method: "POST",
53
  headers: { "Content-Type": "application/json" },
54
- body: JSON.stringify({ note, velocity })
55
  })
56
  .then(response => response.json())
57
- .then(data => console.log("📩 Python Response:", data))
 
 
 
58
  .catch(error => console.error("🚨 Error sending MIDI:", error));
59
  }
60
  </script>
 
 
 
 
 
 
 
61
  """
62
 
63
- # ✅ Inject JavaScript in Gradio UI
64
  with gr.Blocks() as demo:
65
  gr.HTML(midi_js)
66
 
67
  # ✅ Mount FastAPI with Gradio
68
  app = gr.mount_gradio_app(app, demo, path="/")
69
 
70
- # ✅ Run FastAPI Server
 
 
 
 
 
71
  if __name__ == "__main__":
 
72
  print("🚀 Starting FastAPI with Gradio...")
73
  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
 
5
  # ✅ Create FastAPI App
6
  app = FastAPI()
7
 
8
+ # ✅ JavaScript to Capture and Send MIDI Data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  midi_js = """
10
  <script>
11
+ console.log("JavaScript is running");
12
 
13
+ // ✅ Detect MIDI Access
14
  navigator.requestMIDIAccess()
15
  .then(access => {
16
  console.log("✅ MIDI Access Granted!");
 
 
 
 
17
  })
18
  .catch(err => console.error("🚨 MIDI API Error:", err));
19
 
20
+ // Dummy Button Function
21
+ function sendMIDI() {
22
+ console.log("🎹 Sending Test MIDI Note");
 
 
 
 
 
 
23
  fetch("/midi_input", {
24
  method: "POST",
25
  headers: { "Content-Type": "application/json" },
26
+ body: JSON.stringify({ note: 60, velocity: 100 })
27
  })
28
  .then(response => response.json())
29
+ .then(data => {
30
+ console.log("📩 Python Response:", data);
31
+ document.getElementById("output").innerText = "MIDI Sent! Response: " + JSON.stringify(data);
32
+ })
33
  .catch(error => console.error("🚨 Error sending MIDI:", error));
34
  }
35
  </script>
36
+
37
+ <!-- 🎛 Simple UI -->
38
+ <div>
39
+ <h2>🎹 MIDI Controller</h2>
40
+ <button onclick="sendMIDI()">Send Test MIDI Note</button>
41
+ <p id="output">Waiting for MIDI response...</p>
42
+ </div>
43
  """
44
 
45
+ # ✅ Inject JavaScript and HTML
46
  with gr.Blocks() as demo:
47
  gr.HTML(midi_js)
48
 
49
  # ✅ Mount FastAPI with Gradio
50
  app = gr.mount_gradio_app(app, demo, path="/")
51
 
52
+ # ✅ Dummy API Endpoint (For Testing)
53
+ @app.post("/midi_input")
54
+ async def process_midi(request: Request):
55
+ data = await request.json()
56
+ return {"status": "success", "received_note": data["note"], "received_velocity": data["velocity"]}
57
+
58
  if __name__ == "__main__":
59
+ import uvicorn
60
  print("🚀 Starting FastAPI with Gradio...")
61
  uvicorn.run(app, host="0.0.0.0", port=7860)