Update app.py
Browse files
app.py
CHANGED
@@ -562,6 +562,35 @@ async function uploadFile(file, element) {
|
|
562 |
except Exception as e:
|
563 |
raise HTTPException(status_code=400, detail=str(e))
|
564 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
565 |
@app.post("/fetch-url/")
|
566 |
async def fetch_url(request: Request):
|
567 |
try:
|
@@ -581,16 +610,13 @@ async def fetch_url(request: Request):
|
|
581 |
if not content_type.startswith('image/'):
|
582 |
raise HTTPException(status_code=400, detail="URL does not point to an image")
|
583 |
|
584 |
-
# Get original filename
|
585 |
original_filename = url.split('/')[-1]
|
586 |
if not original_filename or '.' not in original_filename:
|
587 |
ext = get_image_extension(content_type)
|
588 |
original_filename = f"downloaded_image.{ext}"
|
589 |
|
590 |
-
# Generate unique path
|
591 |
unique_path = generate_unique_filename(original_filename)
|
592 |
|
593 |
-
# Upload to HuggingFace with the new path
|
594 |
response = api.upload_file(
|
595 |
path_or_fileobj=response.content,
|
596 |
path_in_repo=f"images/{unique_path}",
|
@@ -599,38 +625,11 @@ async def fetch_url(request: Request):
|
|
599 |
token=hf_token
|
600 |
)
|
601 |
|
602 |
-
|
|
|
603 |
return {"url": image_url, "filename": original_filename}
|
604 |
except Exception as e:
|
605 |
raise HTTPException(status_code=500, detail=str(e))
|
606 |
|
607 |
-
@app.post("/upload/")
|
608 |
-
async def upload_image(file: UploadFile = File(...)):
|
609 |
-
if not file:
|
610 |
-
raise HTTPException(status_code=400, detail="No file uploaded")
|
611 |
-
|
612 |
-
if not file.content_type.startswith('image/'):
|
613 |
-
raise HTTPException(status_code=400, detail="File must be an image")
|
614 |
-
|
615 |
-
try:
|
616 |
-
contents = await file.read()
|
617 |
-
|
618 |
-
# Generate unique path
|
619 |
-
unique_path = generate_unique_filename(file.filename)
|
620 |
-
|
621 |
-
# Upload to HuggingFace with the new path
|
622 |
-
response = api.upload_file(
|
623 |
-
path_or_fileobj=contents,
|
624 |
-
path_in_repo=f"images/{unique_path}",
|
625 |
-
repo_id=hf_dataset_id,
|
626 |
-
repo_type="dataset",
|
627 |
-
token=hf_token
|
628 |
-
)
|
629 |
-
|
630 |
-
image_url = f"https://{PROXY_DOMAIN}/images/{unique_path}"
|
631 |
-
return {"url": image_url}
|
632 |
-
except Exception as e:
|
633 |
-
raise HTTPException(status_code=500, detail=str(e))
|
634 |
-
|
635 |
if __name__ == "__main__":
|
636 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
562 |
except Exception as e:
|
563 |
raise HTTPException(status_code=400, detail=str(e))
|
564 |
|
565 |
+
@app.post("/upload/")
|
566 |
+
async def upload_image(file: UploadFile = File(...)):
|
567 |
+
if not file:
|
568 |
+
raise HTTPException(status_code=400, detail="No file uploaded")
|
569 |
+
|
570 |
+
if not file.content_type.startswith('image/'):
|
571 |
+
raise HTTPException(status_code=400, detail="File must be an image")
|
572 |
+
|
573 |
+
try:
|
574 |
+
contents = await file.read()
|
575 |
+
|
576 |
+
# Generate unique path
|
577 |
+
unique_path = generate_unique_filename(file.filename)
|
578 |
+
|
579 |
+
# Upload to HuggingFace
|
580 |
+
response = api.upload_file(
|
581 |
+
path_or_fileobj=contents,
|
582 |
+
path_in_repo=f"images/{unique_path}",
|
583 |
+
repo_id=hf_dataset_id,
|
584 |
+
repo_type="dataset",
|
585 |
+
token=hf_token
|
586 |
+
)
|
587 |
+
|
588 |
+
# 修改返回URL格式
|
589 |
+
image_url = f"https://{PROXY_DOMAIN}/datasets/{hf_dataset_id}/resolve/main/images/{unique_path}"
|
590 |
+
return {"url": image_url}
|
591 |
+
except Exception as e:
|
592 |
+
raise HTTPException(status_code=500, detail=str(e))
|
593 |
+
|
594 |
@app.post("/fetch-url/")
|
595 |
async def fetch_url(request: Request):
|
596 |
try:
|
|
|
610 |
if not content_type.startswith('image/'):
|
611 |
raise HTTPException(status_code=400, detail="URL does not point to an image")
|
612 |
|
|
|
613 |
original_filename = url.split('/')[-1]
|
614 |
if not original_filename or '.' not in original_filename:
|
615 |
ext = get_image_extension(content_type)
|
616 |
original_filename = f"downloaded_image.{ext}"
|
617 |
|
|
|
618 |
unique_path = generate_unique_filename(original_filename)
|
619 |
|
|
|
620 |
response = api.upload_file(
|
621 |
path_or_fileobj=response.content,
|
622 |
path_in_repo=f"images/{unique_path}",
|
|
|
625 |
token=hf_token
|
626 |
)
|
627 |
|
628 |
+
# 修改返回URL格式
|
629 |
+
image_url = f"https://{PROXY_DOMAIN}/datasets/{hf_dataset_id}/resolve/main/images/{unique_path}"
|
630 |
return {"url": image_url, "filename": original_filename}
|
631 |
except Exception as e:
|
632 |
raise HTTPException(status_code=500, detail=str(e))
|
633 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
634 |
if __name__ == "__main__":
|
635 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|