Spaces:
Sleeping
Sleeping
feat: file upload
Browse files
main.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
import requests
|
3 |
+
from PIL import Image
|
4 |
+
import oss2
|
5 |
+
import os
|
6 |
+
import gradio as gr
|
7 |
+
import uuid
|
8 |
+
import json
|
9 |
+
import base64
|
10 |
+
from io import BytesIO
|
11 |
+
from datetime import datetime
|
12 |
+
from oss2.credentials import EnvironmentVariableCredentialsProvider,StaticCredentialsProvider
|
13 |
+
|
14 |
+
app = FastAPI()
|
15 |
+
|
16 |
+
|
17 |
+
UPLOAD_DIRECTORY = "./uploaded_files"
|
18 |
+
|
19 |
+
# 如果目录不存在,则创建
|
20 |
+
if not os.path.exists(UPLOAD_DIRECTORY):
|
21 |
+
os.makedirs(UPLOAD_DIRECTORY)
|
22 |
+
|
23 |
+
|
24 |
+
@app.get("/")
|
25 |
+
def read_root():
|
26 |
+
return {"Hello": "World!"}
|
27 |
+
|
28 |
+
@app.post("/upload")
|
29 |
+
async def upload_file(file: UploadFile = File(...)):
|
30 |
+
# 定义文件存储路径和名称
|
31 |
+
file_location = os.path.join(UPLOAD_DIRECTORY, file.filename)
|
32 |
+
|
33 |
+
# 将上传的文件写入本地文件系统
|
34 |
+
with open(file_location, "wb") as buffer:
|
35 |
+
buffer.write(await file.read())
|
36 |
+
|
37 |
+
return {"info": f"file '{file.filename}' saved at '{file_location}'"}
|