Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, UploadFile, File | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import shutil | |
| import uuid | |
| import os | |
| from inference import predict | |
| app = FastAPI(title="Audio Emotion Recognition API") | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| UPLOAD_DIR = "/tmp" | |
| os.makedirs(UPLOAD_DIR, exist_ok=True) | |
| def root(): | |
| return { | |
| "message": "Audio Emotion Recognition API is running" | |
| } | |
| def health(): | |
| return {"status": "ok"} | |
| async def predict_emotion(file: UploadFile = File(...)): | |
| file_path = f"{UPLOAD_DIR}/{uuid.uuid4()}.wav" | |
| with open(file_path, "wb") as buffer: | |
| shutil.copyfileobj(file.file, buffer) | |
| result = predict(file_path) | |
| os.remove(file_path) | |
| return result | |