pachet commited on
Commit
29529d7
·
1 Parent(s): c0b1af5

added the style fix to the js

Browse files
Files changed (1) hide show
  1. app.py +20 -17
app.py CHANGED
@@ -1,28 +1,32 @@
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
- # ✅ JavaScript to Capture and Send MIDI Data
 
 
 
 
 
 
 
10
  midi_js = """
11
  <script>
12
- console.log("JavaScript is running");
13
 
14
  // ✅ Detect MIDI Access
15
  navigator.requestMIDIAccess()
16
- .then(access => {
17
- console.log("✅ MIDI Access Granted!");
18
- })
19
  .catch(err => console.error("🚨 MIDI API Error:", err));
20
 
21
- // ✅ Dummy Button Function
22
  function sendMIDI() {
23
  console.log("🎹 Sending Test MIDI Note");
24
 
25
- fetch("/api/midi_input", { // ✅ Fix: Use /api prefix
26
  method: "POST",
27
  headers: { "Content-Type": "application/json" },
28
  body: JSON.stringify({ note: 60, velocity: 100 })
@@ -44,20 +48,19 @@ function sendMIDI() {
44
  </div>
45
  """
46
 
47
- # ✅ Inject JavaScript and HTML
48
  with gr.Blocks() as demo:
49
  gr.HTML(midi_js)
50
 
51
- # ✅ Mount Gradio on a separate path (prevents API conflicts)
52
- app = gr.mount_gradio_app(app, demo, path="/")
53
 
54
- # ✅ Define FastAPI Endpoint (Fixing Not Found Issue)
55
- @app.post("/api/midi_input") # Fix: Ensure endpoint is under /api
56
- async def process_midi(request: Request):
57
- data = await request.json()
58
- return {"status": "success", "received_note": data["note"], "received_velocity": data["velocity"]}
59
 
60
- # ✅ Run FastAPI & Gradio Server
61
  if __name__ == "__main__":
62
  print("🚀 Starting FastAPI with Gradio...")
63
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  import gradio as gr
2
  from fastapi import FastAPI, Request
 
3
  import uvicorn
4
 
5
  # ✅ Create FastAPI App
6
  app = FastAPI()
7
 
8
+ # ✅ Define FastAPI Endpoint
9
+ @app.post("/api/midi_input") # Ensure it's reachable at /api/midi_input
10
+ async def process_midi(request: Request):
11
+ data = await request.json()
12
+ print(f"📩 Received MIDI: {data}") # Debugging log
13
+ return {"status": "success", "received_note": data["note"], "received_velocity": data["velocity"]}
14
+
15
+ # ✅ JavaScript for MIDI Data Sending
16
  midi_js = """
17
  <script>
18
+ console.log("JavaScript is running");
19
 
20
  // ✅ Detect MIDI Access
21
  navigator.requestMIDIAccess()
22
+ .then(access => console.log("✅ MIDI Access Granted!"))
 
 
23
  .catch(err => console.error("🚨 MIDI API Error:", err));
24
 
25
+ // ✅ Function to Send MIDI Note
26
  function sendMIDI() {
27
  console.log("🎹 Sending Test MIDI Note");
28
 
29
+ fetch("/api/midi_input", { // ✅ Now sending to the correct endpoint
30
  method: "POST",
31
  headers: { "Content-Type": "application/json" },
32
  body: JSON.stringify({ note: 60, velocity: 100 })
 
48
  </div>
49
  """
50
 
51
+ # ✅ Inject JavaScript into Gradio
52
  with gr.Blocks() as demo:
53
  gr.HTML(midi_js)
54
 
55
+ # ✅ Mount Gradio on `/` but **keep API routes separate**
56
+ app.mount("/", gr.mount_gradio_app(app, demo))
57
 
58
+ # ✅ Debug: Print all registered FastAPI routes
59
+ print("🚀 FastAPI Routes Registered:")
60
+ for route in app.routes:
61
+ print(route.path, "→", route.name)
 
62
 
63
+ # ✅ Run Server
64
  if __name__ == "__main__":
65
  print("🚀 Starting FastAPI with Gradio...")
66
  uvicorn.run(app, host="0.0.0.0", port=7860)