Spaces:
Sleeping
Sleeping
test2023h5
commited on
Commit
•
a4d06d8
1
Parent(s):
7487ea3
Update main.py
Browse files
main.py
CHANGED
@@ -1,7 +1,30 @@
|
|
1 |
from fastapi import FastAPI
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
|
4 |
app = FastAPI()
|
5 |
|
6 |
+
# 定义一个数据模型,用于POST请求的参数
|
7 |
+
class ProcessRequest(BaseModel):
|
8 |
+
text: str
|
9 |
+
method: str
|
10 |
+
|
11 |
+
# GET请求接口
|
12 |
+
@app.get("/hello")
|
13 |
+
async def say_hello():
|
14 |
+
return {"message": "Hello, World!"}
|
15 |
+
|
16 |
+
# POST请求接口
|
17 |
+
@app.post("/process")
|
18 |
+
async def process_text(request: ProcessRequest):
|
19 |
+
if request.method == 1:
|
20 |
+
processed_text = request.text.upper()
|
21 |
+
elif request.method == 2:
|
22 |
+
processed_text = request.text.lower()
|
23 |
+
elif request.method == 3:
|
24 |
+
processed_text = request.text[::-1] # 反转字符串
|
25 |
+
else:
|
26 |
+
processed_text = request.text
|
27 |
+
|
28 |
+
return {"original_text": request.text, "processed_text": processed_text, "method": request.method}
|
29 |
+
|
30 |
+
print("fastapi done")
|