aitist/app.py
2024-08-04 11:14:17 -05:00

35 lines
1.1 KiB
Python

from fastapi import FastAPI, File, UploadFile
import tempfile
import os
from fastapi import FastAPI
from fastapi.openapi.docs import get_swagger_ui_html
# Assuming Celery is already set up and imported correctly in the project.
from tasks import delete_temp_file # Import your Celery task here.
app = FastAPI(
title="AITist API",
description="This is a simple API for AI Tist.",
version="1.0.0",
docs_url="/docs", # Enable Swagger UI
)
@app.get("/docs")
async def custom_swagger_ui_html():
return get_swagger_ui_html(openapi_url=app.openapi_url, title=app.title + " - Swagger UI")
@app.post("/uploadfile/")
async def upload_file(file: UploadFile = File(...)):
contents = await file.read()
# Save the file to a temporary directory
temp_dir = tempfile.gettempdir()
temp_file_path = os.path.join(temp_dir, file.filename)
with open(temp_file_path, 'wb') as f:
f.write(contents)
# Call the Celery task that deletes the file after processing.
delete_temp_file.delay(temp_file_path) # Assuming this is your Celery task name.
return {"filename": file.filename}