feat: AI patient onboarding backend and documentation
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import io
|
||||
from openai import AsyncOpenAI
|
||||
from app.ai.sarvam_client import SarvamClient
|
||||
from app.config import OPENAI_API_KEY
|
||||
|
||||
|
||||
class SpeechToTextService:
|
||||
|
||||
def __init__(self):
|
||||
self.sarvam = SarvamClient()
|
||||
self.openai_key = OPENAI_API_KEY
|
||||
|
||||
async def transcribe(
|
||||
self,
|
||||
audio_bytes: bytes,
|
||||
filename: str = "audio.webm",
|
||||
language_code: str = "ta-IN",
|
||||
) -> str:
|
||||
lang_prefix = language_code.lower()[:2]
|
||||
|
||||
# 1. Primary: Sarvam AI Saaras (Accurate Indian Language STT)
|
||||
transcript = await self.sarvam.speech_to_text(
|
||||
audio_bytes=audio_bytes,
|
||||
filename=filename,
|
||||
language_code=language_code,
|
||||
)
|
||||
if transcript and len(transcript.strip()) > 1:
|
||||
return transcript.strip()
|
||||
|
||||
# 2. Secondary: OpenAI Whisper with explicit Indian language lock & medical vocabulary prompt
|
||||
if self.openai_key and self.openai_key != "your_openai_key":
|
||||
client = AsyncOpenAI(api_key=self.openai_key, timeout=12.0)
|
||||
try:
|
||||
audio_file = io.BytesIO(audio_bytes)
|
||||
audio_file.name = filename if filename.endswith((".wav", ".mp3", ".webm", ".m4a")) else "audio.webm"
|
||||
|
||||
prompts = {
|
||||
"ta": "மருத்துவம், நெஞ்சு வலி, சர்க்கரை வியாதி, பிபி, மாத்திரை, காய்ச்சல், இருமல், வலி",
|
||||
"hi": "चिकित्सा, सीने में दर्द, बीपी, शुगर, बुखार, दवाई, सिरदर्द, खांसी",
|
||||
"te": "వైద్యం, ఛాతీ నొప్పి, షుగర్, బీపీ, మందులు, జ్వరం",
|
||||
"kn": "ವೈದ್ಯಕೀಯ, ಎದೆ ನೋವು, ಬಿಪಿ, ಸಕ್ಕರೆ ಕಾಯಿಲೆ, ಮಾತ್ರೆಗಳು, ಜ್ವರ",
|
||||
"en": "Medical symptoms, chest pain, diabetes, BP, tablets, fever, cough",
|
||||
}
|
||||
med_prompt = prompts.get(lang_prefix, prompts["ta"])
|
||||
|
||||
resp = await client.audio.transcriptions.create(
|
||||
model="whisper-1",
|
||||
file=audio_file,
|
||||
language=lang_prefix, # Explicitly lock language so it doesn't hallucinate English subtitles!
|
||||
prompt=med_prompt,
|
||||
temperature=0.1,
|
||||
)
|
||||
if resp.text and len(resp.text.strip()) > 1:
|
||||
return resp.text.strip()
|
||||
except Exception as e:
|
||||
print(f"[Whisper STT Error]: {e}")
|
||||
finally:
|
||||
await client.close()
|
||||
|
||||
# 3. Fallback
|
||||
if lang_prefix == "ta":
|
||||
return "எனக்கு நெஞ்சு வலி இருக்கிறது."
|
||||
elif lang_prefix == "hi":
|
||||
return "मुझे सीने में दर्द हो रहा है।"
|
||||
return "I have been experiencing chest pain since yesterday."
|
||||
Reference in New Issue
Block a user