Код: Выделить всё
from fastapi import FastAPI, File, UploadFile, HTTPException
import speech_recognition as sr
import shutil
from pydub import AudioSegment
app = FastAPI()
@app.post("/")
async def transcribe_audio(file: UploadFile = File(...)):
if not file:
raise HTTPException(status_code=400, detail="No file uploaded")
# Save the uploaded file
with open(f"temp_{file.filename}", "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
# Convert the audio file to WAV format
audio = AudioSegment.from_file(f"temp_{file.filename}")
wav_filename = f"temp_{file.filename}.wav"
audio.export(wav_filename, format="wav")
# Initialize the recognizer
recognizer = sr.Recognizer()
# Load the audio file
with sr.AudioFile(wav_filename) as source:
audio_data = recognizer.record(source)
# Transcribe the audio file
try:
text = recognizer.recognize_google(audio_data)
return {"transcription": text}
except sr.UnknownValueError:
return {"error": "Could not understand the audio"}
except sr.RequestError as e:
return {"error": f"Could not request results; {e}"}
p>
{"detail":[{"type":"missing","loc":["body","file"],"msg":" Поле обязательно","input":null}]
и 422 Unprocessable Entity в Postman.
Вот изображение :

Знаете ли вы, как написать API, который получает аудио с помощью Фастапи? Кажется, ошибка возникает только в Postman, поскольку я попробовал это вручную с помощью библиотеки request, и все было в порядке.
Подробнее здесь: https://stackoverflow.com/questions/788 ... in-postman