@app.post("/imagecaption/") async def caption_from_frontend(file: UploadFile): try: # Save temp image contents = await file.read() tmp_path = os.path.join(tempfile.gettempdir(), file.filename) with open(tmp_path, "wb") as f: f.write(contents) caption = generate_caption(tmp_path) # Generate audio audio_path = os.path.join(tempfile.gettempdir(), file.filename + ".mp3") tts = gTTS(text=caption) tts.save(audio_path) return JSONResponse({ "answer": caption, "audio": f"/files/{os.path.basename(audio_path)}" }) except Exception as e: return JSONResponse({"error": str(e)}, status_code=500)