Spaces:
Sleeping
Sleeping
feat: 下载文件
Browse files
main.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
from fastapi import FastAPI, File, UploadFile, HTTPException
|
|
|
2 |
import requests
|
3 |
import os
|
4 |
import json
|
@@ -25,6 +26,11 @@ if not os.path.exists(UPLOAD_DIRECTORY):
|
|
25 |
os.makedirs(UPLOAD_DIRECTORY)
|
26 |
|
27 |
|
|
|
|
|
|
|
|
|
|
|
28 |
# HF_API_TOKEN = "your_huggingface_api_token" # 替换为您的 Hugging Face API Token
|
29 |
# HfFolder.save_token(HF_API_TOKEN)
|
30 |
|
@@ -33,7 +39,7 @@ def read_root():
|
|
33 |
return {"Hello": "World!"}
|
34 |
|
35 |
@app.post("/upload")
|
36 |
-
async def upload_file(file: UploadFile = File(
|
37 |
logger.info(f'收到请求 {file.filename}')
|
38 |
# 定义文件存储路径和名称
|
39 |
file_location = os.path.join(UPLOAD_DIRECTORY, file.filename)
|
@@ -43,4 +49,9 @@ async def upload_file(file: UploadFile = File(...)):
|
|
43 |
logger.info(f'写入本地文件 {file.filename}')
|
44 |
buffer.write(await file.read())
|
45 |
|
46 |
-
return {"info": f"file '{file.filename}' saved at '{file_location}'"}
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
+
from fastapi.responses import StreamingResponse
|
3 |
import requests
|
4 |
import os
|
5 |
import json
|
|
|
26 |
os.makedirs(UPLOAD_DIRECTORY)
|
27 |
|
28 |
|
29 |
+
def download_file(url: str):
|
30 |
+
with requests.get(url, stream=True) as response:
|
31 |
+
for chunk in response.iter_content(chunk_size=1024): # 每次读取 1KB
|
32 |
+
if chunk:
|
33 |
+
yield chunk
|
34 |
# HF_API_TOKEN = "your_huggingface_api_token" # 替换为您的 Hugging Face API Token
|
35 |
# HfFolder.save_token(HF_API_TOKEN)
|
36 |
|
|
|
39 |
return {"Hello": "World!"}
|
40 |
|
41 |
@app.post("/upload")
|
42 |
+
async def upload_file(file: UploadFile = File()):
|
43 |
logger.info(f'收到请求 {file.filename}')
|
44 |
# 定义文件存储路径和名称
|
45 |
file_location = os.path.join(UPLOAD_DIRECTORY, file.filename)
|
|
|
49 |
logger.info(f'写入本地文件 {file.filename}')
|
50 |
buffer.write(await file.read())
|
51 |
|
52 |
+
return {"info": f"file '{file.filename}' saved at '{file_location}'"}
|
53 |
+
|
54 |
+
|
55 |
+
@app.get("/download")
|
56 |
+
def download():
|
57 |
+
return StreamingResponse(download_file("https://huggingface.co/wsj1995/stable-diffusion-models/resolve/main/3Guofeng3_v34.safetensors?download=true"), media_type="application/octet-stream")
|