Step 1: Setting Up API Authentication
Before using Whisper AI, authenticating with the OpenAI API is very important. To authenticate, you need an API Key and a few Python packages.
- Open .env file: You need an API Key in .env file
.
- Copy the Key: Copy the .env file key, that is the API Key from the Open AI platform.
- Add to your code: Add the copied code to the application file. With proper authentification, the next step of translation will begin.
Step 2: Preparing the Files
Now you need to create the 'requirements.txt' file and insert the OpenAI and Python dependencies in that code. Additionally, you need to select which .mp3 files to use for the program. These are all files required in order for the code to proceed.
Step 3: Running Whisper Model
In app.py, add following requirements
:
import openai
import os
from dotenv import load_dotenv
load_dotenv()
openai.api_key = os.getenv('OPENAI_API_KEY')
To get the transcript, use the following code:
audio_file= open("static/Recording.mp3", "rb")
transcript = openai.Audio.translate("whisper-1", audio_file)
print (transcript)
Step 4: Translating the .txt
openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"You will be provided with a sentence in English, and your task is to translate it into {language}"},
{"role": "user", "content": transcript.text}
],
temperature=0,
max_tokens=256
)
Step 5: Run the app in Flask
Following code runs the HTML template in Flask:
from flask import Flask, request, jsonify, render_template
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=8080)