peter288 commited on
Commit
2535bd4
·
verified ·
1 Parent(s): 77b3d81

Update backend/app.py

Browse files
Files changed (1) hide show
  1. backend/app.py +66 -66
backend/app.py CHANGED
@@ -1,67 +1,67 @@
1
- from fastapi import FastAPI, UploadFile, File
2
- from fastapi.middleware.cors import CORSMiddleware
3
- from fastapi.responses import StreamingResponse, FileResponse
4
- from fastapi.staticfiles import StaticFiles
5
- import os
6
- from midi_utils import process_midi
7
-
8
- app = FastAPI()
9
-
10
- # 核心MIDI处理API
11
- @app.post("/process-midi")
12
- async def process_midi_file(file: UploadFile = File(...)):
13
- try:
14
- print(f"Processing MIDI file: {file.filename}")
15
- midi_data = await file.read()
16
- processed_data = process_midi(midi_data)
17
-
18
- return StreamingResponse(
19
- processed_data,
20
- media_type="audio/midi",
21
- headers={"Content-Disposition": f"attachment; filename=processed_{file.filename}"}
22
- )
23
- except Exception as e:
24
- print(f"Error processing MIDI: {e}")
25
- return {"error": str(e)}
26
-
27
- @app.get("/health")
28
- def health_check():
29
- return {"status": "OK", "version": "1.1.0"}
30
-
31
- # --- 新增部分:提供前端服务 ---
32
-
33
- # 挂载静态文件目录,所有Next.js生成的文件都在这里
34
- # '/_next', '/favicon.ico' 等请求会由这里处理
35
- app.mount("/_next", StaticFiles(directory="static/_next"), name="next-assets")
36
- app.mount("/public", StaticFiles(directory="static/public"), name="public-assets")
37
-
38
-
39
- # 定义一个捕获所有路径的路由,作为单页应用(SPA)的入口
40
- # 任何未匹配到API的GET请求,都返回index.html
41
- @app.get("/{full_path:path}")
42
- async def serve_react_app(full_path: str):
43
- # 检查请求的文件是否存在于static目录中
44
- file_path = os.path.join("static", full_path)
45
- if os.path.exists(file_path) and os.path.isfile(file_path):
46
- return FileResponse(file_path)
47
- # 如果是目录或不存在,则默认返回主页
48
- return FileResponse("static/index.html")
49
-
50
- # 确保根路径'/'也返回index.html
51
- @app.get("/")
52
- async def root():
53
- return FileResponse('static/index.html')
54
-
55
- # (可选,但推荐) 移除CORS,因为前后端现在是同源的
56
- # 如果您仍希望从其他地方调用API,可以保留
57
- # app.add_middleware(
58
- # CORSMiddleware,
59
- # allow_origins=["*"],
60
- # allow_methods=["*"],
61
- # allow_headers=["*"],
62
- # )
63
-
64
- if __name__ == "__main__":
65
- import uvicorn
66
- # Hugging Face Spaces 默认使用 7860 端口
67
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from fastapi.responses import StreamingResponse, FileResponse
4
+ from fastapi.staticfiles import StaticFiles
5
+ import os
6
+ from midi_utils import process_midi
7
+
8
+ app = FastAPI()
9
+
10
+ # 核心MIDI处理API
11
+ @app.post("/process-midi")
12
+ async def process_midi_file(file: UploadFile = File(...)):
13
+ try:
14
+ print(f"Processing MIDI file: {file.filename}")
15
+ midi_data = await file.read()
16
+ processed_data = process_midi(midi_data)
17
+
18
+ return StreamingResponse(
19
+ processed_data,
20
+ media_type="audio/midi",
21
+ headers={"Content-Disposition": f"attachment; filename=processed_{file.filename}"}
22
+ )
23
+ except Exception as e:
24
+ print(f"Error processing MIDI: {e}")
25
+ return {"error": str(e)}
26
+
27
+ @app.get("/health")
28
+ def health_check():
29
+ return {"status": "OK", "version": "1.1.0"}
30
+
31
+ # --- 新增部分:提供前端服务 ---
32
+
33
+ # 挂载静态文件目录,所有Next.js生成的文件都在这里
34
+ # '/_next', '/favicon.ico' 等请求会由这里处理
35
+ app.mount("/_next", StaticFiles(directory="static/_next"), name="next-assets")
36
+
37
+
38
+
39
+ # 定义一个捕获所有路径的路由,作为单页应用(SPA)的入口
40
+ # 任何未匹配到API的GET请求,都返回index.html
41
+ @app.get("/{full_path:path}")
42
+ async def serve_react_app(full_path: str):
43
+ # 检查请求的文件是否存在于static目录中
44
+ file_path = os.path.join("static", full_path)
45
+ if os.path.exists(file_path) and os.path.isfile(file_path):
46
+ return FileResponse(file_path)
47
+ # 如果是目录或不存在,则默认返回主页
48
+ return FileResponse("static/index.html")
49
+
50
+ # 确保根路径'/'也返回index.html
51
+ @app.get("/")
52
+ async def root():
53
+ return FileResponse('static/index.html')
54
+
55
+ # (可选,但推荐) 移除CORS,因为前后端现在是同源的
56
+ # 如果您仍希望从其他地方调用API,可以保留
57
+ # app.add_middleware(
58
+ # CORSMiddleware,
59
+ # allow_origins=["*"],
60
+ # allow_methods=["*"],
61
+ # allow_headers=["*"],
62
+ # )
63
+
64
+ if __name__ == "__main__":
65
+ import uvicorn
66
+ # Hugging Face Spaces 默认使用 7860 端口
67
  uvicorn.run(app, host="0.0.0.0", port=7860)