Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -222,6 +222,38 @@ async def transcribe_and_answer(
|
|
222 |
logging.error(f"General error: {e}")
|
223 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
# Set up CORS middleware
|
226 |
origins = ["*"] # or specify your list of allowed origins
|
227 |
app.add_middleware(
|
|
|
222 |
logging.error(f"General error: {e}")
|
223 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
224 |
|
225 |
+
@app.post("/test-transcription/", description="Upload an audio file to test transcription using speech_recognition.")
|
226 |
+
async def test_transcription(file: UploadFile = File(...)):
|
227 |
+
try:
|
228 |
+
# Check if the file format is supported
|
229 |
+
if file.content_type not in ["audio/wav", "audio/mpeg", "audio/mp3"]:
|
230 |
+
raise HTTPException(status_code=400, detail="Unsupported audio format. Please upload a WAV or MP3 file.")
|
231 |
+
|
232 |
+
# Convert uploaded file to WAV if necessary for compatibility with SpeechRecognition
|
233 |
+
audio_data = await file.read()
|
234 |
+
audio_file = io.BytesIO(audio_data)
|
235 |
+
|
236 |
+
if file.content_type in ["audio/mpeg", "audio/mp3"]:
|
237 |
+
# Convert MP3 to WAV
|
238 |
+
audio = AudioSegment.from_file(audio_file, format="mp3")
|
239 |
+
audio_wav = io.BytesIO()
|
240 |
+
audio.export(audio_wav, format="wav")
|
241 |
+
audio_wav.seek(0)
|
242 |
+
else:
|
243 |
+
audio_wav = audio_file
|
244 |
+
|
245 |
+
# Transcribe audio using speech_recognition
|
246 |
+
recognizer = sr.Recognizer()
|
247 |
+
with sr.AudioFile(audio_wav) as source:
|
248 |
+
audio = recognizer.record(source)
|
249 |
+
transcription = recognizer.recognize_google(audio)
|
250 |
+
|
251 |
+
# Return the transcription
|
252 |
+
return {"transcription": transcription}
|
253 |
+
|
254 |
+
except Exception as e:
|
255 |
+
raise HTTPException(status_code=500, detail=f"Error during transcription: {str(e)}")
|
256 |
+
|
257 |
# Set up CORS middleware
|
258 |
origins = ["*"] # or specify your list of allowed origins
|
259 |
app.add_middleware(
|