Update main.py
Browse files
main.py
CHANGED
|
@@ -1,20 +1,35 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
from transformers import pipeline
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def t5(input):
|
| 13 |
-
output = pipe_flan(input)
|
| 14 |
-
return {"output": output[0]["generated_text"]}
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
+
# 定义请求体模型
|
| 8 |
+
class RequestData(BaseModel):
|
| 9 |
+
content: str
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# POST请求:创建test.txt文件并写入内容
|
| 12 |
+
@app.post("/create_file")
|
| 13 |
+
async def create_file(data: RequestData):
|
| 14 |
+
try:
|
| 15 |
+
# 打开并写入文件
|
| 16 |
+
with open("test.txt", "w") as file:
|
| 17 |
+
file.write(data.content)
|
| 18 |
+
return {"message": "File created successfully."}
|
| 19 |
+
except Exception as e:
|
| 20 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 21 |
|
| 22 |
+
# GET请求:读取test.txt文件内容
|
| 23 |
+
@app.get("/read_file")
|
| 24 |
+
async def read_file():
|
| 25 |
+
try:
|
| 26 |
+
# 检查文件是否存在
|
| 27 |
+
if not os.path.exists("test.txt"):
|
| 28 |
+
raise HTTPException(status_code=404, detail="File not found.")
|
| 29 |
+
|
| 30 |
+
# 读取文件内容
|
| 31 |
+
with open("test.txt", "r") as file:
|
| 32 |
+
content = file.read()
|
| 33 |
+
return {"content": content}
|
| 34 |
+
except Exception as e:
|
| 35 |
+
raise HTTPException(status_code=500, detail=str(e))
|