2025-03-22 20:54:10 +08:00
|
|
|
from fastapi import FastAPI, Form, UploadFile
|
|
|
|
from fastapi.responses import JSONResponse
|
|
|
|
import asyncio
|
2025-02-07 19:18:35 +08:00
|
|
|
|
2025-03-22 20:54:10 +08:00
|
|
|
app = FastAPI()
|
2025-02-07 19:18:35 +08:00
|
|
|
|
2025-03-22 20:54:10 +08:00
|
|
|
@app.post("/api/text")
|
|
|
|
async def text_query_endpoint(question: str = Form(...)):
|
|
|
|
"""
|
|
|
|
API endpoint to process text input with the user's query.
|
|
|
|
"""
|
|
|
|
from endpoints.text import text_query
|
|
|
|
return await text_query(question=question)
|
2025-02-07 19:18:35 +08:00
|
|
|
|
|
|
|
|
2025-03-22 20:54:10 +08:00
|
|
|
@app.post("/api/image")
|
|
|
|
async def image_query_endpoint(file: UploadFile, question: str = Form(...)):
|
|
|
|
"""
|
|
|
|
API endpoint to process an image with the user's query.
|
|
|
|
"""
|
|
|
|
from endpoints.image import image_query
|
|
|
|
return await image_query(file=file, question=question)
|
2025-02-07 19:18:35 +08:00
|
|
|
|
|
|
|
|
2025-03-22 20:54:10 +08:00
|
|
|
@app.post("/api/video")
|
|
|
|
async def video_query_endpoint(file: UploadFile, question: str = Form(...)):
|
|
|
|
"""
|
|
|
|
API endpoint to process a video file with the user's query.
|
|
|
|
"""
|
|
|
|
from endpoints.video import video_query
|
|
|
|
return await video_query(file=file, question=question)
|
2025-02-07 19:18:35 +08:00
|
|
|
|
|
|
|
|
2025-03-22 20:54:10 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import uvicorn
|
|
|
|
uvicorn.run("main:app", host="0.0.0.0", port=8002, reload=True, loop="uvloop")
|
2025-02-07 19:18:35 +08:00
|
|
|
|
|
|
|
|
2025-01-23 21:50:55 +08:00
|
|
|
|
2025-03-22 20:54:10 +08:00
|
|
|
|
|
|
|
# from fastapi import FastAPI, Form, UploadFile
|
|
|
|
# from fastapi.responses import JSONResponse
|
|
|
|
# import shutil
|
|
|
|
# import uuid
|
|
|
|
# from celery import chain
|
|
|
|
# from tasks import text_query_task, image_query_task, video_query_task
|
|
|
|
# from tasks import text_query_task, image_query_task, preprocess_video, inference_video
|
|
|
|
|
|
|
|
# app = FastAPI()
|
|
|
|
|
|
|
|
# @app.get("/")
|
|
|
|
# def read_root():
|
|
|
|
# return {"message": "FastAPI with Celery & RabbitMQ"}
|
2025-01-23 21:50:55 +08:00
|
|
|
|
2025-02-07 19:18:35 +08:00
|
|
|
# @app.post("/api/text")
|
|
|
|
# async def text_query_endpoint(question: str = Form(...)):
|
2025-03-22 20:54:10 +08:00
|
|
|
# print(f"Received request: {question}")
|
2025-02-07 19:18:35 +08:00
|
|
|
# task = text_query_task.apply_async(args=[question])
|
2025-03-22 20:54:10 +08:00
|
|
|
# print(f"Task sent: {task.id}")
|
2025-02-07 19:18:35 +08:00
|
|
|
# return JSONResponse({"task_id": task.id})
|
|
|
|
|
2025-03-22 20:54:10 +08:00
|
|
|
# @app.post("/api/image")
|
|
|
|
# async def image_query_endpoint(file: UploadFile, question: str = Form(...)):
|
|
|
|
# file_path = f"/tmp/{uuid.uuid4()}_{file.filename}"
|
|
|
|
# with open(file_path, "wb") as buffer:
|
|
|
|
# shutil.copyfileobj(file.file, buffer)
|
|
|
|
|
|
|
|
# task = image_query_task.apply_async(args=[file_path, question])
|
|
|
|
# return JSONResponse({"task_id": task.id})
|
2025-02-07 19:18:35 +08:00
|
|
|
|
2025-03-22 20:54:10 +08:00
|
|
|
# # @app.post("/api/video")
|
|
|
|
# # async def video_query_endpoint(file: UploadFile, question: str = Form(...)):
|
|
|
|
# # file_path = f"/tmp/{uuid.uuid4()}_{file.filename}"
|
|
|
|
# # with open(file_path, "wb") as buffer:
|
|
|
|
# # shutil.copyfileobj(file.file, buffer)
|
2025-02-07 19:18:35 +08:00
|
|
|
|
2025-03-22 20:54:10 +08:00
|
|
|
# # task = video_query_task.apply_async(args=[file_path, question])
|
|
|
|
# # return JSONResponse({"task_id": task.id})
|
2025-01-23 21:50:55 +08:00
|
|
|
|
2025-03-22 20:54:10 +08:00
|
|
|
# @app.post("/api/video")
|
|
|
|
# async def video_query_endpoint(file: UploadFile, question: str = Form(...)):
|
|
|
|
# # Save the uploaded file to a temporary location
|
|
|
|
# file_path = f"/tmp/{uuid.uuid4()}_{file.filename}"
|
|
|
|
# with open(file_path, "wb") as buffer:
|
|
|
|
# shutil.copyfileobj(file.file, buffer)
|
2025-02-07 19:18:35 +08:00
|
|
|
|
2025-03-22 20:54:10 +08:00
|
|
|
# # Chain the preprocessing and inference tasks
|
|
|
|
# task_chain = chain(
|
|
|
|
# preprocess_video.s(file_path, question), # Preprocessing task
|
|
|
|
# inference_video.s() # Inference task
|
|
|
|
# ).apply_async()
|
|
|
|
|
|
|
|
# return JSONResponse({"task_id": task_chain.id})
|
|
|
|
|
|
|
|
# @app.get("/api/task/{task_id}")
|
|
|
|
# async def get_task_result(task_id: str):
|
|
|
|
# from celery.result import AsyncResult
|
|
|
|
# result = AsyncResult(task_id)
|
|
|
|
# if result.ready():
|
|
|
|
# return JSONResponse({"status": "completed", "result": result.result})
|
|
|
|
# return JSONResponse({"status": "pending"})
|
|
|
|
|
|
|
|
# if __name__ == "__main__":
|
|
|
|
# import uvicorn
|
|
|
|
# uvicorn.run("main:app", host="0.0.0.0", port=8002, reload=True, loop="uvloop")
|