Songyou commited on
Commit
0e80e71
·
verified ·
1 Parent(s): 01ce2cc

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +30 -15
main.py CHANGED
@@ -1,20 +1,35 @@
1
- from fastapi import FastAPI
2
- from fastapi.staticfiles import StaticFiles
3
- from fastapi.responses import FileResponse
4
-
5
- from transformers import pipeline
6
 
7
  app = FastAPI()
8
 
9
- pipe_flan = pipeline("text2text-generation", model="google/flan-t5-small")
10
-
11
- @app.get("/infer_t5")
12
- def t5(input):
13
- output = pipe_flan(input)
14
- return {"output": output[0]["generated_text"]}
15
 
16
- app.mount("/", StaticFiles(directory="static", html=True), name="static")
 
 
 
 
 
 
 
 
 
17
 
18
- @app.get("/")
19
- def index() -> FileResponse:
20
- return FileResponse(path="/app/static/index.html", media_type="text/html")
 
 
 
 
 
 
 
 
 
 
 
 
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))