Skip to content
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

Integrate OpenAI Whisper API Support for Enhanced Transcription Options #137 #142

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bot_handlers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import logging
import os
from typing import Dict, Any
from services import AWSServices, AudioTranscriber, TextSummarizer
from services import AudioTranscriber, TextSummarizer
from utils.telegram_utils import send_message, get_telegram_file_url
from utils.message_utils import format_response, create_tip_button
from config import Config

logger = logging.getLogger(__name__)

# Initialize services
aws_services = AWSServices()
audio_transcriber = AudioTranscriber(aws_services)
audio_transcriber = AudioTranscriber(Config)
text_summarizer = TextSummarizer(os.environ.get('MARKETROUTER_API_KEY'))

def handle_update(update: Dict[str, Any]) -> None:
Expand Down
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ class Config:
MARKETROUTER_API_KEY = os.environ.get('MARKETROUTER_API_KEY')
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
TRANSCRIPTION_SERVICE = os.environ.get('TRANSCRIPTION_SERVICE', 'aws') # 'aws' or 'openai'
68 changes: 62 additions & 6 deletions services.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ def start_transcription_job(self, job_name, media_uri, media_format='ogg', langu
def get_transcription_job_status(self, job_name):
return self.transcribe_client.get_transcription_job(TranscriptionJobName=job_name)

class AudioTranscriber:
class BaseTranscriber:
def _download_audio(self, file_url: str) -> bytes:
response = requests.get(file_url)
response.raise_for_status()
return response.content

class AWSTranscriber(BaseTranscriber):
def __init__(self, aws_services: AWSServices):
self.aws_services = aws_services
self.bucket_name = 'audio-transcribe-temp'
Expand All @@ -77,11 +83,6 @@ def transcribe_audio(self, file_url: str) -> str:
logger.error(f"An error occurred: {e}")
raise

def _download_audio(self, file_url: str) -> bytes:
response = requests.get(file_url)
response.raise_for_status()
return response.content

def _wait_for_transcription(self, job_name: str) -> str:
while True:
status = self.aws_services.get_transcription_job_status(job_name)
Expand All @@ -95,6 +96,61 @@ def _wait_for_transcription(self, job_name: str) -> str:
else:
raise Exception("Transcription failed")

class OpenAITranscriber(BaseTranscriber):
def __init__(self, api_key: str):
self.api_key = api_key
self.api_url = "https://api.openai.com/v1/audio/transcriptions"

def transcribe_audio(self, file_url: str) -> str:
try:
audio_content = self._download_audio(file_url)

# Save audio content to a temporary file
temp_file = f'/tmp/audio_{uuid.uuid4()}.ogg'
with open(temp_file, 'wb') as f:
f.write(audio_content)

# Prepare the request
headers = {
'Authorization': f'Bearer {self.api_key}'
}

files = {
'file': ('audio.ogg', open(temp_file, 'rb'), 'audio/ogg'),
'model': (None, 'whisper-1'),
}

response = requests.post(
self.api_url,
headers=headers,
files=files
)
response.raise_for_status()

# Clean up temporary file
os.remove(temp_file)

return response.json()['text']
except Exception as e:
logger.error(f"An error occurred with OpenAI transcription: {e}")
raise

class AudioTranscriber:
def __init__(self, config):
self.config = config
if config.TRANSCRIPTION_SERVICE == 'aws':
aws_services = AWSServices(region_name=config.AWS_REGION)
self.transcriber = AWSTranscriber(aws_services)
elif config.TRANSCRIPTION_SERVICE == 'openai':
if not config.OPENAI_API_KEY:
raise ValueError("OpenAI API key is required for OpenAI transcription service")
self.transcriber = OpenAITranscriber(config.OPENAI_API_KEY)
else:
raise ValueError(f"Unsupported transcription service: {config.TRANSCRIPTION_SERVICE}")

def transcribe_audio(self, file_url: str) -> str:
return self.transcriber.transcribe_audio(file_url)

class TextSummarizer:
def __init__(self, api_key: str):
self.api_key = api_key
Expand Down