Create voice.py
Browse files
voice.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Optional
|
2 |
+
from fastapi import HTTPException
|
3 |
+
from config import logger
|
4 |
+
import io
|
5 |
+
import speech_recognition as sr
|
6 |
+
from gtts import gTTS
|
7 |
+
from pydub import AudioSegment
|
8 |
+
import base64
|
9 |
+
|
10 |
+
def recognize_speech(audio_data: bytes, language: str = "en-US") -> str:
|
11 |
+
recognizer = sr.Recognizer()
|
12 |
+
try:
|
13 |
+
with io.BytesIO(audio_data) as audio_file:
|
14 |
+
with sr.AudioFile(audio_file) as source:
|
15 |
+
audio = recognizer.record(source)
|
16 |
+
text = recognizer.recognize_google(audio, language=language)
|
17 |
+
return text
|
18 |
+
except sr.UnknownValueError:
|
19 |
+
logger.error("Google Speech Recognition could not understand audio")
|
20 |
+
raise HTTPException(status_code=400, detail="Could not understand audio")
|
21 |
+
except sr.RequestError as e:
|
22 |
+
logger.error(f"Could not request results from Google Speech Recognition service; {e}")
|
23 |
+
raise HTTPException(status_code=503, detail="Speech recognition service unavailable")
|
24 |
+
except Exception as e:
|
25 |
+
logger.error(f"Error in speech recognition: {e}")
|
26 |
+
raise HTTPException(status_code=500, detail="Error processing speech")
|
27 |
+
|
28 |
+
def text_to_speech(text: str, language: str = "en", slow: bool = False) -> bytes:
|
29 |
+
try:
|
30 |
+
tts = gTTS(text=text, lang=language, slow=slow)
|
31 |
+
mp3_fp = io.BytesIO()
|
32 |
+
tts.write_to_fp(mp3_fp)
|
33 |
+
mp3_fp.seek(0)
|
34 |
+
return mp3_fp.read()
|
35 |
+
except Exception as e:
|
36 |
+
logger.error(f"Error in text-to-speech conversion: {e}")
|
37 |
+
raise HTTPException(status_code=500, detail="Error generating speech")
|
38 |
+
|
39 |
+
def extract_text_from_pdf(pdf_data: bytes) -> str:
|
40 |
+
try:
|
41 |
+
from PyPDF2 import PdfReader
|
42 |
+
pdf_reader = PdfReader(io.BytesIO(pdf_data))
|
43 |
+
text = ""
|
44 |
+
for page in pdf_reader.pages:
|
45 |
+
text += page.extract_text() or ""
|
46 |
+
return clean_text_response(text)
|
47 |
+
except Exception as e:
|
48 |
+
logger.error(f"Error extracting text from PDF: {e}")
|
49 |
+
raise HTTPException(status_code=400, detail="Failed to extract text from PDF")
|