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 Alternative Transcription Service (Issue #137) #150

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

## Features

- Transcribes voice messages using AWS Transcribe
- Supports multiple transcription services:
- AWS Transcribe for reliable, enterprise-grade transcription
- OpenAI Whisper API for high-accuracy, multilingual transcription
- Summarizes transcribed text using a custom API
- Allows users to tip for the service
- Secures handling of API keys and tokens
Expand All @@ -25,9 +27,11 @@ GroupLang-secretary-bot is a Telegram bot that transcribes voice messages, summa
## Prerequisites

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

## Installation

Expand Down Expand Up @@ -69,14 +73,24 @@ To quickly get started with the GroupLang-secretary-bot, follow these steps:
## Configuration

1. Set up environment variables:
- `TELEGRAM_BOT_TOKEN`: Your Telegram Bot Token
- `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
- Required for all configurations:
- `TELEGRAM_BOT_TOKEN`: Your Telegram Bot Token
- `MARKETROUTER_API_KEY`: Your MarketRouter API Key
- `TRANSCRIPTION_SERVICE`: Choose the transcription service ('aws' or 'openai', defaults to 'aws')

- Required for AWS Transcribe:
- `AWS_ACCESS_KEY_ID`: Your AWS Access Key ID
- `AWS_SECRET_ACCESS_KEY`: Your AWS Secret Access Key

- Required for OpenAI Whisper:
- `OPENAI_API_KEY`: Your OpenAI 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.
2. Configure transcription service:
- For AWS Transcribe:
- Set up the AWS CLI with `aws configure` or use environment variables as mentioned above
- Ensure 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
- Transcription Services:
- AWS Transcribe: Enterprise-grade audio transcription service
- OpenAI Whisper API: High-accuracy, multilingual transcription service
- MarketRouter API: For text summarization and reward submission

Refer to the respective documentation for more details on these APIs.
For more details, refer to:
- [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 (contact provider)
30 changes: 25 additions & 5 deletions bot_handlers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import logging
import os
from typing import Dict, Any
from services import AWSServices, AudioTranscriber, TextSummarizer
from services import (
AWSServices,
AWSTranscriptionService,
OpenAITranscriptionService,
TextSummarizer,
TranscriptionService
)
from utils.telegram_utils import send_message, get_telegram_file_url
from utils.message_utils import format_response, create_tip_button

logger = logging.getLogger(__name__)

def get_transcription_service() -> TranscriptionService:
service_type = os.environ.get('TRANSCRIPTION_SERVICE', 'aws').lower()

if service_type == 'aws':
aws_services = AWSServices()
return AWSTranscriptionService(aws_services)
elif service_type == 'openai':
openai_api_key = os.environ.get('OPENAI_API_KEY')
if not openai_api_key:
raise ValueError("OPENAI_API_KEY environment variable is required for OpenAI transcription service")
return OpenAITranscriptionService(openai_api_key)
else:
raise ValueError(f"Unsupported transcription service: {service_type}")

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

def handle_update(update: Dict[str, Any]) -> None:
Expand All @@ -30,12 +49,13 @@ def handle_voice_message(message: Dict[str, Any], chat_id: int) -> None:
file_id = message['voice']['file_id']
file_url = get_telegram_file_url(file_id)

transcription = audio_transcriber.transcribe_audio(file_url)
transcription = transcription_service.transcribe_audio(file_url)
summary, conversation_id = text_summarizer.summarize_text(transcription)

logger.info(f"Processed voice message: file_id={file_id}, "
f"transcription_length={len(transcription)}, "
f"summary_length={len(summary)}")
f"summary_length={len(summary)}, "
f"service_type={type(transcription_service).__name__}")

response = format_response(transcription, summary)
reply_markup = create_tip_button(conversation_id)
Expand Down
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
41 changes: 34 additions & 7 deletions services.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import boto3
from typing import Optional, Tuple, Dict
from typing import Optional, Tuple, Dict, Protocol, Union
from abc import ABC, abstractmethod
import requests
import time
import uuid
import logging
from io import BytesIO
from botocore.exceptions import ClientError
import openai

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -50,7 +52,17 @@ 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 TranscriptionService(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 AWSTranscriptionService(TranscriptionService):
def __init__(self, aws_services: AWSServices):
self.aws_services = aws_services
self.bucket_name = 'audio-transcribe-temp'
Expand All @@ -77,11 +89,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 +102,26 @@ def _wait_for_transcription(self, job_name: str) -> str:
else:
raise Exception("Transcription failed")

class OpenAITranscriptionService(TranscriptionService):
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)
with BytesIO(audio_content) as audio_file:
audio_file.name = "audio.ogg" # OpenAI needs a filename
response = self.client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="text"
)
return response

except Exception as e:
logger.error(f"OpenAI transcription error: {e}")
raise

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