-
Notifications
You must be signed in to change notification settings - Fork 2
feat(voice): implement voice mode, audio transcription endpoint, and … #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CodewithJha
wants to merge
2
commits into
LucknowAI:dev
Choose a base branch
from
CodewithJha:priyanshu/voice-feature
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import time | ||
| from fastapi import APIRouter, File, UploadFile, HTTPException | ||
| import httpx | ||
|
|
||
| from src.config.settings import Settings | ||
| from src.utils.util_logger.logger import logger | ||
|
|
||
| router = APIRouter(prefix="/voice", tags=["Voice"]) | ||
|
|
||
| GROQ_TRANSCRIPTION_URL = "https://api.groq.com/openai/v1/audio/transcriptions" | ||
|
|
||
| @router.post("/transcribe") | ||
| async def transcribe_audio(file: UploadFile = File(...)): | ||
| """ | ||
| Accepts an audio file upload, transcribes it using Groq's Whisper API, | ||
| and returns the transcribed text and metadata. | ||
| """ | ||
| if not Settings.GROQ_API_KEY: | ||
| raise HTTPException( | ||
| status_code=500, | ||
| detail="GROQ_API_KEY is not configured on the backend." | ||
| ) | ||
|
|
||
| # Read the upload file contents | ||
| try: | ||
| content = await file.read() | ||
| if not content: | ||
| raise HTTPException(status_code=400, detail="Empty audio recording.") | ||
| except Exception as exc: | ||
| logger.error(f"[voice] Failed to read uploaded audio file: {exc}") | ||
| raise HTTPException(status_code=400, detail="Invalid audio file upload.") | ||
|
|
||
| # Prepare multipart files for Groq API | ||
| files = { | ||
| "file": (file.filename, content, file.content_type or "audio/webm") | ||
| } | ||
|
|
||
| # We specify verbose_json to get language and duration | ||
| data = { | ||
| "model": "whisper-large-v3", | ||
| "prompt": "Lucknow, Varanasi, Kanpur, Awadhi, Bhojpuri, Hinglish, bhaiya, kaisan ba ho, ama yaar", | ||
| "response_format": "verbose_json" | ||
| } | ||
|
|
||
| headers = { | ||
| "Authorization": f"Bearer {Settings.GROQ_API_KEY}" | ||
| } | ||
|
|
||
| start_time = time.time() | ||
| try: | ||
| async with httpx.AsyncClient(timeout=30.0) as client: | ||
| response = await client.post( | ||
| GROQ_TRANSCRIPTION_URL, | ||
| headers=headers, | ||
| data=data, | ||
| files=files | ||
| ) | ||
|
|
||
| if not response.is_success: | ||
| logger.error(f"[voice] Groq API transcription failed: status={response.status_code} body={response.text}") | ||
| raise HTTPException( | ||
| status_code=502, | ||
| detail=f"Groq transcription provider error: {response.text}" | ||
| ) | ||
|
|
||
| result = response.json() | ||
| transcript = result.get("text", "").strip() | ||
| language = result.get("language", "unknown") | ||
| duration = result.get("duration", 0.0) | ||
|
|
||
| # Calculate backend processing time | ||
| duration_ms = int((time.time() - start_time) * 1000) | ||
|
|
||
| logger.info(f"[voice] Transcribed language={language} duration_sec={duration} in duration_ms={duration_ms}ms") | ||
|
|
||
| return { | ||
| "text": transcript, | ||
| "language": language, | ||
| "provider": "groq", | ||
| "duration_ms": duration_ms | ||
| } | ||
|
|
||
| except httpx.RequestError as exc: | ||
| logger.error(f"[voice] Network error while contacting Groq: {exc}") | ||
| raise HTTPException( | ||
| status_code=503, | ||
| detail="Transcription service is currently unreachable." | ||
| ) | ||
| except Exception as exc: | ||
| logger.exception(f"[voice] Unexpected transcription failure: {exc}") | ||
| if isinstance(exc, HTTPException): | ||
| raise exc | ||
| raise HTTPException( | ||
| status_code=500, | ||
| detail="An unexpected error occurred during transcription." | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this url to configuration.