Spaces:
Running
Running
| import os | |
| import shutil | |
| import tempfile | |
| from fastapi import UploadFile | |
| UPLOAD_DIR = "./data/uploads" | |
| # UPLOAD_DIR = "uploads" | |
| os.makedirs(UPLOAD_DIR, exist_ok=True) | |
| # def save_upload_file(file: UploadFile) -> str: | |
| # file_path = os.path.join(UPLOAD_DIR, file.filename) | |
| # with open(file_path, "wb") as buffer: | |
| # shutil.copyfileobj(file.file, buffer) | |
| # # print(f"File path: {file_path.split(',')[0]}{file_path.split(',')[-1]}") | |
| # return file_path | |
| # Use ./data/uploads for writable folder inside Hugging Face Spaces | |
| # UPLOAD_DIR = "./uploads" | |
| # os.makedirs(UPLOAD_DIR, exist_ok=True) | |
| def save_upload_image(file: UploadFile) -> str: | |
| """ | |
| Save an uploaded file to the UPLOAD_DIR | |
| Returns the full file path | |
| """ | |
| print("Running save upload image") | |
| file_path = os.path.join(UPLOAD_DIR, file.filename) | |
| # Save file | |
| with open(file_path, "wb") as buffer: | |
| shutil.copyfileobj(file.file, buffer) | |
| return file_path | |
| # def save_upload_image(file: UploadFile) -> str: | |
| # """ | |
| # Save an uploaded file to a temporary folder and return the file path. | |
| # The file will exist only during the app runtime and does not persist. | |
| # """ | |
| # # Create a temporary file | |
| # temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=f"_{file.filename}") | |
| # | |
| # # Save the uploaded file | |
| # with temp_file as buffer: | |
| # shutil.copyfileobj(file.file, buffer) | |
| # | |
| # return temp_file.name |