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

Add Support for OpenAI Whisper API as an Additional Transcription Backend (Issue #137) #141

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
35 changes: 27 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ 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
- Flexible choice of transcription service (AWS or OpenAI)
- Summarizes transcribed text using a custom API
- Allows users to tip for the service
- Secures handling of API keys and tokens
Expand All @@ -25,7 +26,9 @@ GroupLang-secretary-bot is a Telegram bot that transcribes voice messages, summa
## Prerequisites

- Poetry for dependency management
- AWS account with Transcribe access
- Either:
- AWS account with Transcribe access, OR
- OpenAI API key for Whisper API
- Telegram Bot Token
- MarketRouter API Key

Expand Down Expand Up @@ -70,13 +73,24 @@ 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

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

2. Configure credentials based on your chosen transcription service:

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.
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 you have a valid OpenAI API key with access to the Whisper API

1. Activate the Poetry virtual environment:
```
Expand Down Expand Up @@ -139,7 +153,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 recognition model
- 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
12 changes: 9 additions & 3 deletions bot_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@
logger = logging.getLogger(__name__)

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

aws_services = AWSServices() if Config.TRANSCRIPTION_SERVICE == 'aws' else None
audio_transcriber = AudioTranscriber(
aws_services=aws_services,
openai_api_key=Config.OPENAI_API_KEY,
service=Config.TRANSCRIPTION_SERVICE
)
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'
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ requests = "^2.32.3"
nltk = "^3.9.1"
langdetect = "^1.0.9"
mangum = "^0.18.0"
openai = "^1.12.0"

[tool.poetry.dev-dependencies]
# Add any development dependencies here
Expand Down
52 changes: 50 additions & 2 deletions services.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,38 @@ 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 OpenAITranscriber:
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)

headers = {
"Authorization": f"Bearer {self.api_key}"
}

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

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

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

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

class AWSTranscriber:
def __init__(self, aws_services: AWSServices):
self.aws_services = aws_services
self.bucket_name = 'audio-transcribe-temp'
Expand All @@ -74,7 +105,7 @@ def transcribe_audio(self, file_url: str) -> str:

return transcription
except Exception as e:
logger.error(f"An error occurred: {e}")
logger.error(f"An error occurred with AWS transcription: {e}")
raise

def _download_audio(self, file_url: str) -> bytes:
Expand All @@ -95,6 +126,23 @@ def _wait_for_transcription(self, job_name: str) -> str:
else:
raise Exception("Transcription failed")

class AudioTranscriber:
def __init__(self, aws_services: Optional[AWSServices] = None, openai_api_key: Optional[str] = None, service: str = 'aws'):
self.service = service.lower()
if self.service == 'aws':
if not aws_services:
raise ValueError("AWS services required for AWS transcription")
self.transcriber = AWSTranscriber(aws_services)
elif self.service == 'openai':
if not openai_api_key:
raise ValueError("OpenAI API key required for OpenAI transcription")
self.transcriber = OpenAITranscriber(openai_api_key)
else:
raise ValueError(f"Unsupported transcription service: {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