import os from fastapi import APIRouter, File, UploadFile, HTTPException from groq import Groq from dotenv import load_dotenv # Load environment variables from .env file load_dotenv() GROQ_API_KEY = os.getenv("GROQ_API_KEY") if not GROQ_API_KEY: raise Exception("GROQ_API_KEY is not set in the environment.") router = APIRouter() @router.post("/transcribe") async def transcribe_audio(file: UploadFile = File(...)): """ Transcribe an uploaded audio file using the Groq client with the Whisper model. Returns the transcribed text. """ try: # Initialize the Groq client with the API key from the environment. client = Groq(api_key=GROQ_API_KEY) file_bytes = await file.read() transcription = client.audio.transcriptions.create( file=(file.filename, file_bytes), model="whisper-large-v3", response_format="verbose_json", ) return {"transcription": transcription.text} except Exception as e: raise HTTPException(status_code=500, detail=str(e))