peter288 commited on
Commit
d585c70
·
verified ·
1 Parent(s): cc18b3d

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +18 -0
  2. app.py +66 -0
  3. requirements.txt +2 -0
Dockerfile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 使用一个轻量级的 Python 官方镜像
2
+ FROM python:3.10-slim
3
+
4
+ # 设置工作目录
5
+ WORKDIR /code
6
+
7
+ # 复制并安装最简化的依赖
8
+ COPY ./requirements.txt /code/requirements.txt
9
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
10
+
11
+ # 复制应用代码
12
+ COPY ./app.py /code/app.py
13
+
14
+ # 暴露 Hugging Face Spaces 期望的端口
15
+ EXPOSE 7860
16
+
17
+ # 启动 Uvicorn 服务器,并添加 --proxy-headers 标志来修复文档路径问题
18
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--proxy-headers"]
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ import uvicorn
4
+
5
+ # 创建 FastAPI 应用实例
6
+ app = FastAPI(
7
+ title="Simple Calculator API",
8
+ description="一个用于验证部署流程的最简API,接收两个数字并返回它们的和。",
9
+ version="0.1.0"
10
+ )
11
+
12
+ # --- CORS 中间件 ---
13
+ # 允许所有来源的请求,为未来的前端集成做准备
14
+ app.add_middleware(
15
+ CORSMiddleware,
16
+ allow_origins=["*"],
17
+ allow_credentials=True,
18
+ allow_methods=["*"],
19
+ allow_headers=["*"],
20
+ )
21
+
22
+
23
+ # --- 核心计算函数 (替代MIDI处理) ---
24
+ def simple_add(number1: float, number2: float) -> float:
25
+ """
26
+ 这是一个简单的计算函数,模拟你的核心业务逻辑。
27
+ 它接收两个浮点数,返回它们的和。
28
+ 这个函数未来可以被替换为 process_midi()。
29
+ """
30
+ print(f"正在计算: {number1} + {number2}")
31
+ return number1 + number2
32
+
33
+
34
+ # --- API 端点 ---
35
+
36
+ @app.get("/", tags=["General"])
37
+ def read_root():
38
+ """根路径,提供欢迎信息和文档链接。"""
39
+ return {"message": "Welcome to the Simple Calculator API!", "docs_url": "/docs"}
40
+
41
+
42
+ @app.get("/calculate", tags=["Calculation"])
43
+ def calculate_sum(
44
+ a: float = Query(..., description="第一个数字"),
45
+ b: float = Query(..., description="第二个数字")
46
+ ):
47
+ """
48
+ 接收两个查询参数 'a' 和 'b',调用计算函数,并返回结果。
49
+ 示例调用: /calculate?a=10&b=22
50
+ """
51
+ # 调用核心计算函数
52
+ result = simple_add(a, b)
53
+
54
+ # 以JSON格式返回结果
55
+ return {
56
+ "input": {
57
+ "a": a,
58
+ "b": b
59
+ },
60
+ "operation": "addition",
61
+ "result": result
62
+ }
63
+
64
+ # 这部分是为了方便在本地直接运行测试,Docker部署时不会用到
65
+ if __name__ == "__main__":
66
+ uvicorn.run(app, host="0.0.0.0", port=8000)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ fastapi
2
+ uvicorn