peoplepilot commited on
Commit
e09762f
·
1 Parent(s): f560ce7

add file upload

Browse files
Files changed (1) hide show
  1. app.py +34 -1
app.py CHANGED
@@ -12,6 +12,36 @@ import time
12
  from sam_audio import SAMAudio, SAMAudioProcessor
13
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  warnings.filterwarnings("ignore")
16
 
17
  logger = logging.getLogger("sam_space")
@@ -350,4 +380,7 @@ with gr.Blocks(title="SAM-Audio Test") as demo:
350
  )
351
 
352
  if __name__ == "__main__":
353
- demo.launch(show_error=True, share=True)
 
 
 
 
12
  from sam_audio import SAMAudio, SAMAudioProcessor
13
 
14
 
15
+ import os, uuid
16
+ from fastapi import FastAPI, UploadFile, File
17
+ from fastapi.responses import JSONResponse
18
+ import gradio as gr
19
+
20
+ api = FastAPI()
21
+
22
+ UPLOAD_DIR = "/tmp/uploads"
23
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
24
+
25
+ @api.post("/upload_audio")
26
+ async def upload_audio(file: UploadFile = File(...)):
27
+ # Save uploaded bytes
28
+ ext = os.path.splitext(file.filename)[1] or ".wav"
29
+ out_name = f"{uuid.uuid4().hex}{ext}"
30
+ out_path = os.path.join(UPLOAD_DIR, out_name)
31
+
32
+ data = await file.read()
33
+ with open(out_path, "wb") as f:
34
+ f.write(data)
35
+
36
+ # Serve it back via a URL on this same Space
37
+ # We'll add a simple file-serving route:
38
+ return JSONResponse({"path": out_path, "url": f"/files/{out_name}"})
39
+
40
+ from fastapi.staticfiles import StaticFiles
41
+ api.mount("/files", StaticFiles(directory=UPLOAD_DIR), name="files")
42
+
43
+
44
+
45
  warnings.filterwarnings("ignore")
46
 
47
  logger = logging.getLogger("sam_space")
 
380
  )
381
 
382
  if __name__ == "__main__":
383
+ demo.launch(show_error=True, share=True)
384
+
385
+ app = gr.mount_gradio_app(api, demo, path="/")
386
+