Created
May 3, 2024 02:12
-
-
Save lox/1689cdb9971734cb37a2cdfe20626a27 to your computer and use it in GitHub Desktop.
Experimental client to test Whisper on GroqCloud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import argparse | |
import requests | |
from datetime import datetime | |
# Define the parser for command line arguments | |
parser = argparse.ArgumentParser(description='Groq Whisper transcribe') | |
parser.add_argument('file', type=str, help='Audio file to transcribe') | |
parser.add_argument('--output_file', type=str, help='File to write output', default="transcription.txt") | |
args = parser.parse_args() | |
# Set the API endpoint for Groq's OpenAI-compatible Whisper service | |
whisper_api_endpoint = "https://api.groq.com/openai/v1/whisper" | |
# Read the API key from an environment variable | |
api_key = os.getenv("GROQ_API_KEY") | |
if not api_key: | |
raise ValueError("Please set the GROQ_API_KEY environment variable.") | |
# Read the audio file | |
with open(args.file, "rb") as audio_file: | |
audio_data = audio_file.read() | |
# Prepare the headers for the HTTP request | |
headers = { | |
"Authorization": f"Bearer {api_key}", | |
"Content-Type": "application/octet-stream" | |
} | |
# Make the request to the Whisper API | |
before = datetime.now() | |
response = requests.post(whisper_api_endpoint, headers=headers, data=audio_data) | |
after = datetime.now() | |
# Check if the request was successful | |
if response.status_code != 200: | |
raise Exception(f"Whisper API request failed with status code {response.status_code}: {response.text}") | |
# Parse the response | |
transcription = response.json().get('transcription') | |
if not transcription: | |
raise Exception("No transcription found in the response.") | |
# Write the transcription to the output file | |
with open(args.output_file, "w") as output_file: | |
output_file.write(transcription) | |
# Calculate the time taken for the transcription | |
difference = after - before | |
print(f"Wrote {args.output_file} in {difference.total_seconds()} seconds") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment