Repository navigation
audio upload extension with gdrive credentials #175
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
Closed
01PrathamS
wants to merge
11
commits into
SimpleOpenSoftware:main
from
01PrathamS:audio_upload_extend
Closed
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
44beeac
audio upload extension with gdrive credentials
01PrathamS d5b9518
FIX: API parameters
01PrathamS 9392989
Merge branch 'main' into audio_upload_extend
AnkushMalaker 5b5ea64
UPDATE: tmp files cleanup n code refactored as per review
01PrathamS 5abd99d
REFACTOR: minor refactor as per review
01PrathamS 3d00bac
REFACTOR: minor update as per review
01PrathamS b036185
UPDATE: gdrive sync logic
01PrathamS cff1a4c
REFACTOR: code update as per gdrive and update credential client
01PrathamS 6534288
REFACTOR: validation updated - as per review from CR
01PrathamS 1ff28cb
UPDATE: code has been refactore for UUID for diffrent audio upload so…
01PrathamS 8e5a6b2
REFACTOR: updated code as per review
01PrathamS 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,10 +11,26 @@ | |
| from advanced_omi_backend.auth import current_superuser, current_active_user_optional, get_user_from_token_param | ||
| from advanced_omi_backend.controllers import audio_controller | ||
| from advanced_omi_backend.models.user import User | ||
| from advanced_omi_backend.app_config import get_audio_chunk_dir | ||
| from advanced_omi_backend.utils.gdrive_audio_utils import download_audio_files_from_drive | ||
|
|
||
| router = APIRouter(prefix="/audio", tags=["audio"]) | ||
|
|
||
|
|
||
| @router.post("/upload_audio_from_url") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this router name suggests I can use any url, but looks like it's just Gdrive? I'm not super familiar with how gdrive works though |
||
| async def upload_audio_from_drive_folder( | ||
| drive_folder_id: str = Query(...,alias="url", description="Google Drive Folder ID containing audio files (e.g., the string after /folders/ in the URL)"), | ||
| current_user: User = Depends(current_superuser), | ||
| device_name: str = Query(default="upload"), | ||
| auto_generate_client: bool = Query(default=True), | ||
| ): | ||
| files = await download_audio_files_from_drive(drive_folder_id) | ||
|
|
||
| return await audio_controller.upload_and_process_audio_files( | ||
| current_user, files, device_name, auto_generate_client | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @router.get("/get_audio/{conversation_id}") | ||
| async def get_conversation_audio( | ||
| conversation_id: str, | ||
|
|
||
94 changes: 94 additions & 0 deletions
94
backends/advanced/src/advanced_omi_backend/utils/gdrive_audio_utils.py
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,94 @@ | ||
| import os | ||
| import io | ||
| import tempfile | ||
| import atexit | ||
| from typing import List | ||
| from starlette.datastructures import UploadFile as StarletteUploadFile | ||
| from googleapiclient.http import MediaIoBaseDownload | ||
| from advanced_omi_backend.app_config import get_app_config | ||
|
|
||
| AUDIO_EXTENSIONS = (".wav", ".mp3", ".flac", ".ogg", ".m4a") | ||
| FOLDER_MIMETYPE = "application/vnd.google-apps.folder" | ||
|
|
||
|
|
||
| class AudioValidationError(Exception): | ||
| pass | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| def download_and_wrap_drive_file(service, file_item): | ||
| file_id = file_item["id"] | ||
| name = file_item["name"] | ||
|
|
||
| request = service.files().get_media(fileId=file_id) | ||
|
|
||
| fh = io.BytesIO() | ||
| downloader = MediaIoBaseDownload(fh, request) | ||
|
|
||
| done = False | ||
| while not done: | ||
| _status, done = downloader.next_chunk() | ||
|
|
||
| content = fh.getvalue() | ||
|
|
||
| if not content: | ||
| raise AudioValidationError(f"Downloaded Google Drive file '{name}' was empty") | ||
|
|
||
| tmp_file = tempfile.SpooledTemporaryFile(max_size=10*1024*1024) # 10 MB | ||
| tmp_file.write(content) | ||
| tmp_file.seek(0) | ||
| upload_file = StarletteUploadFile(filename=name, file=tmp_file) | ||
|
|
||
| original_close = upload_file.close | ||
|
|
||
| def wrapped_close(): | ||
| try: | ||
| original_close() | ||
| finally: | ||
| # SpooledTemporaryFile auto-cleans when closed; no unlink needed | ||
| pass | ||
|
|
||
| upload_file.close = wrapped_close | ||
|
|
||
| return upload_file | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| # ------------------------------------------------------------- | ||
| # LIST + DOWNLOAD FILES IN FOLDER (OAUTH) | ||
| # ------------------------------------------------------------- | ||
| def download_audio_files_from_drive(folder_id: str) -> List[StarletteUploadFile]: | ||
| if not folder_id: | ||
| raise AudioValidationError("Google Drive folder ID is required.") | ||
|
|
||
| service = get_app_config().get_gdrive_service() | ||
|
|
||
| try: | ||
| escaped_folder_id = folder_id.replace("\\", "\\\\").replace("'", "\\'") | ||
| query = f"'{escaped_folder_id}' in parents and trashed = false" | ||
|
|
||
| response = service.files().list( | ||
| q=query, | ||
| fields="files(id, name, mimeType)", | ||
| includeItemsFromAllDrives=False, | ||
| supportsAllDrives=False, | ||
| ).execute() | ||
|
|
||
| all_files = response.get("files", []) | ||
|
|
||
| audio_files = [ | ||
| f for f in all_files | ||
| if f["name"].lower().endswith(AUDIO_EXTENSIONS) | ||
| ] | ||
|
|
||
| if not audio_files: | ||
| raise AudioValidationError("No audio files found in folder.") | ||
|
|
||
| wrapped_files = [] | ||
| for item in audio_files: | ||
| # synchronous call now | ||
| wrapped_files.append(download_and_wrap_drive_file(service, item)) | ||
|
|
||
| return wrapped_files | ||
|
|
||
| except Exception as e: | ||
| if isinstance(e, AudioValidationError): | ||
| raise | ||
| raise AudioValidationError(f"Google Drive API Error: {e}") from e | ||
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
Oops, something went wrong.
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.
This probably shouldn't be in app_config. can have a gdrive service in services or have a google_client in clients. Then we initialise it at the point of use