-
Notifications
You must be signed in to change notification settings - Fork 0
OpenAI Audio implementation #1133
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| from ..openRouter.openRouter_modelrun import openrouter_modelrun | ||
| from ....configs.constant import service_name | ||
| from ..openAI.image_model import OpenAIImageModel | ||
| from ..openAI.audio_model import OpenAIAudioModel | ||
| from ..Google.gemini_image_model import gemini_image_model | ||
| from ..Google.gemini_video_model import gemini_video_model | ||
| from ..AiMl.ai_ml_model_run import ai_ml_model_run | ||
|
|
@@ -70,12 +71,14 @@ def __init__(self, params): | |
| self.function_time_logs = params.get('function_time_logs') | ||
| self.files = params.get('files') or [] | ||
| self.file_data = params.get('file_data') | ||
| self.audio_file = params.get('audio_file') | ||
|
Collaborator
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. file_data, file check kro and isme hi audio send krne ka option hona chahiye. |
||
| self.youtube_url = params.get('youtube_url') | ||
| self.web_search_filters = params.get('web_search_filters') | ||
| self.folder_id = params.get('folder_id') | ||
| self.bridge_configurations = params.get('bridge_configurations') | ||
|
|
||
|
|
||
|
|
||
| def aiconfig(self): | ||
| return self.customConfig | ||
|
|
||
|
|
@@ -378,4 +381,19 @@ async def video(self, configuration, apikey, service): | |
| } | ||
| except Exception as e: | ||
| logger.error(f"chats error in video=>, {str(e)}, {traceback.format_exc()}") | ||
| raise ValueError(f"error occurs from {self.service} api {e.args[0]}") | ||
| raise ValueError(f"error occurs from {self.service} api {e.args[0]}") | ||
|
|
||
| async def audio(self, configuration, apikey, service, audio_file): | ||
| try: | ||
| response = {} | ||
| if service == service_name['openai']: | ||
| response = await OpenAIAudioModel(configuration, apikey, self.execution_time_logs, self.timer, audio_file) | ||
| if not response['success']: | ||
| raise ValueError(response['error']) | ||
| return { | ||
| 'success': True, | ||
| 'modelResponse': response['response'] | ||
| } | ||
| except Exception as e: | ||
| logger.error(f"chats error in audio=>, {str(e)}, {traceback.format_exc()}") | ||
| raise ValueError(f"error occurs from {self.service} api {e.args[0]}") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import traceback | ||
| from openai import AsyncOpenAI | ||
| import base64 | ||
| import io | ||
|
|
||
|
|
||
| async def OpenAIAudioModel(configuration, apiKey, execution_time_logs, timer, audio_file): | ||
| try: | ||
| openai_config = AsyncOpenAI(api_key=apiKey) | ||
|
Collaborator
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. rearrage the code |
||
| timer.start() | ||
|
|
||
| model = configuration.get('model') | ||
|
|
||
| # Handle base64 encoded audio - strictly require data:audio format | ||
| if isinstance(audio_file, str): | ||
| if not audio_file.startswith('data:audio'): | ||
| raise ValueError("Audio file must be in 'data:audio' format (e.g., 'data:audio/mp3;base64,...')") | ||
|
|
||
| # Extract base64 data after the comma | ||
| base64_data = audio_file.split(',', 1)[1] | ||
| audio_bytes = base64.b64decode(base64_data) | ||
| audio_file = io.BytesIO(audio_bytes) | ||
| audio_file.name = "audio.mp3" # OpenAI requires a filename | ||
|
|
||
| # Process transcription | ||
| chat_completion = await openai_config.audio.transcriptions.create( | ||
| model=model, | ||
| file=audio_file, | ||
| ) | ||
| execution_time_logs.append({ | ||
| "step": "OpenAI audio transcription processing time", | ||
| "time_taken": timer.stop("OpenAI audio transcription processing time") | ||
| }) | ||
|
|
||
| # Extract token-based usage from newer audio models | ||
| usage_data = {} | ||
| if hasattr(chat_completion, 'usage') and chat_completion.usage: | ||
| usage = chat_completion.usage | ||
| if hasattr(usage, 'input_tokens'): | ||
| usage_data = { | ||
| "input_tokens": usage.input_tokens, | ||
| "input_token_details": dict(usage.input_token_details) if hasattr(usage, 'input_token_details') else {}, | ||
| "output_tokens": usage.output_tokens, | ||
| "total_tokens": usage.total_tokens | ||
| } | ||
|
|
||
| response = { | ||
| 'text': chat_completion.text, | ||
| 'operation': 'transcription', | ||
| 'model': model, | ||
| 'usage': usage_data | ||
| } | ||
|
|
||
| return { | ||
| 'success': True, | ||
| 'response': response | ||
| } | ||
| except Exception as error: | ||
| execution_time_logs.append({ | ||
| "step": "OpenAI audio processing time", | ||
| "time_taken": timer.stop("OpenAI audio processing time") | ||
| }) | ||
| print("audio model error=>", error) | ||
| traceback.print_exc() | ||
| return { | ||
| 'success': False, | ||
| 'error': str(error) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,7 +184,8 @@ def parse_request_body(request_body): | |
| "testcase_data" : body.get('testcase_data') or {}, | ||
| "is_embed" : body.get('is_embed'), | ||
| "user_id" : body.get('user_id'), | ||
| "file_data" : body.get('video_data') or {}, | ||
| "file_data" : body.get('file_data') or body.get('video_data') or {}, | ||
| "audio_file" : body.get('audio_file') or {}, | ||
|
Comment on lines
+187
to
+188
Collaborator
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. . |
||
| "youtube_url" : body.get('youtube_url') or None, | ||
| "folder_id": body.get('folder_id'), | ||
| "web_search_filters" : body.get('web_search_filters') or None, | ||
|
|
@@ -423,6 +424,7 @@ def build_service_params(parsed_data, custom_config, model_output_config, thread | |
| "built_in_tools" : parsed_data['built_in_tools'], | ||
| "files" : parsed_data['files'], | ||
| "file_data" : parsed_data['file_data'], | ||
| "audio_file" : parsed_data['audio_file'], | ||
| "youtube_url" : parsed_data['youtube_url'], | ||
| "web_search_filters" : parsed_data['web_search_filters'], | ||
| "folder_id": parsed_data.get('folder_id'), | ||
|
|
||
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.
loop kyu bna rhe h?