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 Additional Transcription Backend #153

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
29 changes: 21 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 based on configuration
- 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
- One of the following transcription services:
- AWS account with Transcribe access, OR
- OpenAI API key for Whisper API access
- Telegram Bot Token
- MarketRouter API Key

Expand Down Expand Up @@ -70,13 +73,18 @@ 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' (default) or 'openai'

2. Configure transcription service credentials:
For AWS Transcribe (if using 'aws' service):
- `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
- `AWS_REGION`: AWS region (defaults to 'us-east-1')
- Ensure that your AWS IAM user has the necessary permissions for AWS Transcribe

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 OpenAI Whisper (if using 'openai' service):
- `OPENAI_API_KEY`: Your OpenAI API key with access to the Whisper API

1. Activate the Poetry virtual environment:
```
Expand Down Expand Up @@ -139,7 +147,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 on these APIs:
- [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 (refer to your API provider)
22 changes: 18 additions & 4 deletions bot_handlers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import logging
import os
from typing import Dict, Any
from services import AWSServices, AudioTranscriber, TextSummarizer
from services import (
AWSServices, AWSTranscriber, OpenAITranscriber,
TextSummarizer, BaseTranscriber
)
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'))
def get_transcriber() -> BaseTranscriber:
if Config.TRANSCRIPTION_SERVICE == 'openai':
if not Config.OPENAI_API_KEY:
raise ValueError("OpenAI API key is required when using OpenAI transcription service")
return OpenAITranscriber(Config.OPENAI_API_KEY)
else: # default to AWS
if not (Config.AWS_ACCESS_KEY_ID and Config.AWS_SECRET_ACCESS_KEY):
raise ValueError("AWS credentials are required when using AWS transcription service")
aws_services = AWSServices(Config.AWS_REGION)
return AWSTranscriber(aws_services)

audio_transcriber = get_transcriber()
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
48 changes: 42 additions & 6 deletions services.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,20 @@ 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
import openai

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 +90,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 +103,34 @@ def _wait_for_transcription(self, job_name: str) -> str:
else:
raise Exception("Transcription failed")

class OpenAITranscriber(BaseTranscriber):
def __init__(self, api_key: str):
self.client = openai.OpenAI(api_key=api_key)

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)

# Transcribe using OpenAI Whisper API
with open(temp_file, 'rb') as audio_file:
transcription = self.client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="text"
)

# Clean up temporary file
os.remove(temp_file)
return transcription
except Exception as e:
logger.error(f"An error occurred during OpenAI transcription: {e}")
raise

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