pathananas's picture
main.py
df1c561 verified
from fastapi import FastAPI, UploadFile, File, Form
from model import multimodal_analyze
from database import get_history, clear_history_db
from PIL import Image
from fastapi.middleware.cors import CORSMiddleware
import shutil
import os
app = FastAPI(
title="Multimodal AI Engine",
description="AI system analyzing text, image and audio",
version="1.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.add_middleware(
for route in app.routes:
print(route.path)
# ---------------- ROOT ----------------
@app.get("/")
def root():
return {"message": "Multimodal AI API running"}
# ---------------- ANALYZE ----------------
@app.post("/analyze")
async def analyze(
text: str = Form(None),
image: UploadFile = File(None),
audio: UploadFile = File(None)
):
img = None
audio_path = None
if image:
img = Image.open(image.file)
if audio:
os.makedirs("temp_audio", exist_ok=True)
audio_path = f"temp_audio/{audio.filename}"
with open(audio_path, "wb") as buffer:
shutil.copyfileobj(audio.file, buffer)
fusion, text_res, image_res, audio_res = multimodal_analyze(
text, img, audio_path
)
return {
"fusion_summary": fusion,
"text_analysis": text_res,
"image_analysis": image_res,
"audio_analysis": audio_res
}
# ---------------- HISTORY ----------------
print("REGISTERING HISTORY ROUTES")
@app.get("/history")
def history():
records = get_history()
return {"history": records}
# ---------------- CLEAR HISTORY ----------------
@app.delete("/clear-history")
def clear_history():
clear_history_db()
return {"message": "History cleared"}
# ---------------- HEALTH ----------------
@app.get("/health")
def health():
return {"status": "API running"}
#comit