Spaces:
Sleeping
Sleeping
File size: 732 Bytes
1795a1a e88e7c9 1795a1a e88e7c9 1795a1a e88e7c9 1795a1a e88e7c9 1795a1a e88e7c9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@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) |