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

Support OpenAI Whisper API Integration for Enhanced Transcription Flexibility #139

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
38 changes: 29 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ GroupLang-secretary-bot is a Telegram bot that transcribes voice messages, summa

## Features

- Transcribes voice messages using AWS Transcribe
- Transcribes voice messages using either AWS Transcribe or OpenAI Whisper API
- Summarizes transcribed text using a custom API
- Allows users to tip for the service
- Secures handling of API keys and tokens
- Is deployable as an AWS Lambda function
- Configurable transcription service selection

## Prerequisites

- Poetry for dependency management
- AWS account with Transcribe access
- One of the following transcription services:
- AWS account with Transcribe access
- OpenAI API key with Whisper API access
- Telegram Bot Token
- MarketRouter API Key

Expand Down Expand Up @@ -70,13 +73,25 @@ To quickly get started with the GroupLang-secretary-bot, follow these steps:

1. Set up environment variables:
- `TELEGRAM_BOT_TOKEN`: Your Telegram Bot Token
- `MARKETROUTER_API_KEY`: Your MarketRouter API Key
- `TRANSCRIPTION_SERVICE`: Choose between 'aws' or 'openai' (default: 'aws')

For AWS Transcribe:
- `AWS_ACCESS_KEY_ID`: Your AWS Access Key ID
- `AWS_SECRET_ACCESS_KEY`: Your AWS Secret Access Key
- `MARKETROUTER_API_KEY`: Your MarketRouter API Key

2. Configure AWS credentials:
- Either set up the AWS CLI with `aws configure` or use environment variables as mentioned above.
- Ensure that your AWS IAM user has the necessary permissions for AWS Transcribe.
- `AWS_REGION`: AWS region (default: 'us-east-1')

For OpenAI Whisper:
- `OPENAI_API_KEY`: Your OpenAI API Key

2. Configure credentials based on your chosen transcription service:

For AWS Transcribe:
- Either set up the AWS CLI with `aws configure` or use environment variables as mentioned above
- Ensure that your AWS IAM user has the necessary permissions for AWS Transcribe

For OpenAI Whisper:
- Ensure your OpenAI API key has access to the Whisper API

1. Activate the Poetry virtual environment:
```
Expand Down Expand Up @@ -139,7 +154,12 @@ poetry update package_name

The bot uses the following external APIs:

- AWS Transcribe: For audio transcription
- For audio transcription (configurable):
- AWS Transcribe: Amazon's speech-to-text service
- OpenAI Whisper API: OpenAI's speech-to-text service
- MarketRouter API: For text summarization and reward submission

Refer to the respective documentation for more details on these APIs.
Refer to the respective documentation for more details:
- [AWS Transcribe Documentation](https://docs.aws.amazon.com/transcribe/)
- [OpenAI Whisper API Documentation](https://platform.openai.com/docs/guides/speech-to-text)
- MarketRouter API Documentation
8 changes: 4 additions & 4 deletions bot_handlers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import logging
import os
from typing import Dict, Any
from services import AWSServices, AudioTranscriber, TextSummarizer
from services import TranscriberFactory, 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)
text_summarizer = TextSummarizer(os.environ.get('MARKETROUTER_API_KEY'))
audio_transcriber = TranscriberFactory.create_transcriber(Config)
text_summarizer = TextSummarizer(Config.MARKETROUTER_API_KEY)

def handle_update(update: Dict[str, Any]) -> None:
if 'message' in update:
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'
83 changes: 77 additions & 6 deletions services.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import time
import uuid
import logging
import os
from io import BytesIO
from botocore.exceptions import ClientError
from abc import ABC, abstractmethod

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -50,7 +52,19 @@ 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:
from abc import ABC, abstractmethod

class BaseTranscriber(ABC):
@abstractmethod
def transcribe_audio(self, file_url: str) -> str:
pass

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 +91,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 +104,68 @@ 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'),
}

# Make the request to OpenAI's Whisper API
response = requests.post(
self.api_url,
headers=headers,
files=files
)
response.raise_for_status()

# Clean up the temporary file
os.remove(temp_file)

result = response.json()
return result.get('text', '')

except Exception as e:
logger.error(f"An error occurred with OpenAI transcription: {e}")
raise
finally:
# Ensure temp file is removed even if an error occurs
if 'temp_file' in locals() and os.path.exists(temp_file):
os.remove(temp_file)

class TranscriberFactory:
@staticmethod
def create_transcriber(config) -> BaseTranscriber:
service_type = config.TRANSCRIPTION_SERVICE.lower()

if service_type == 'aws':
aws_services = AWSServices(region_name=config.AWS_REGION)
return AWSTranscriber(aws_services)
elif service_type == 'openai':
if not config.OPENAI_API_KEY:
raise ValueError("OpenAI API key is required for OpenAI transcription service")
return OpenAITranscriber(config.OPENAI_API_KEY)
else:
raise ValueError(f"Unsupported transcription service: {service_type}")


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