Exception: InvalidRequestError: Resource not found

333 Views Asked by At

I want to transcribe a audio file using openai whisper model. And I am able to do it locally. But when the same code I am trying to run azure functions by creating python api. I am getting this error Exception: InvalidRequestError: Resource not found I tried resolving it. but not able find why I am getting this. Anyone if has any idea then please help me.

Error: Executed 'Functions.audio_translation' (Failed, Id=c7ae69cf-5090-46c6-b2c4-169a70e1b3ec, Duration=30574ms) [2023-11-18T06:14:13.621Z] System.Private.CoreLib: Exception while executing function: Functions.audio_translation. System.Private.CoreLib: Result: Failure [2023-11-18T06:14:13.621Z] Exception: InvalidRequestError: Resource not found

init.py

import logging
import openai
import azure.functions as func
from shared_code.helper_translation import process_audio,read_audio_from_azure,read_local_files
import os
import os
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
from pydub import AudioSegment
from pydub.utils import make_chunks
from moviepy.editor import AudioFileClip
# from openai import OpenAI
from azure.storage.blob import BlobClient
import requests
import openai
import logging

openai.api_key = os.getenv("OPENAI_API_KEY")
openai.api_version = os.getenv("OPENAI_API_VERSION")
openai.api_base = os.getenv("OPENAI_API_BASE")
openai.api_type = os.getenv("OPENAI_API_TYPE")


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    if req.method != "POST":
        return func.HttpResponse("Only POST requests are allowed.", status_code=405)
    elif req.method=="POST":

        #input_json = req.get_json()
        response_status = {}
        status_code_resp =200
        data  = read_audio_from_azure()
        logging.info(data)
        return func.HttpResponse("Request processed sucessfully", status_code=405)

helper_translation.py

import os
from datetime import datetime
from concurrent.futures import ThreadPoolExecutor
from pydub import AudioSegment
from pydub.utils import make_chunks
from moviepy.editor import AudioFileClip
# from openai import OpenAI
from azure.storage.blob import BlobClient
import requests
import openai
import logging

openai.api_key = os.getenv("OPENAI_API_KEY")
openai.api_version = os.getenv("OPENAI_API_VERSION")
openai.api_base = os.getenv("OPENAI_API_BASE")
openai.api_type = os.getenv("OPENAI_API_TYPE")


output_folder = "audio_chunk"
def convert_to_mp3(audio_file_path):
    mp3_file_path = os.path.join(output_folder, os.path.splitext(os.path.basename(audio_file_path))[0] + ".mp3")

    if audio_file_path.lower().endswith(".mp4"):
        # Convert MP4 to MP3
        file_to_convert = AudioFileClip(audio_file_path)
        file_to_convert.write_audiofile(mp3_file_path)
        file_to_convert.close()
    elif audio_file_path.lower().endswith(".m4a"):
        # Convert M4A to MP3
        track = AudioSegment.from_file(audio_file_path, format='m4a')
        file_handle = track.export(mp3_file_path, format='mp3')
    else:
        raise ValueError("Unsupported audio file format for conversion. Please provide an MP4 or M4A file.")

    return mp3_file_path

def audio_chunk(audio_file_path):
        chunk_names = []
        my_audio = AudioSegment.from_file(audio_file_path)
        chunks_length_ms = 600000
        chunks = make_chunks(my_audio, chunks_length_ms)

        for i, chunk in enumerate(chunks):
            chunk_name = "{0}_{1}".format(i, os.path.basename(audio_file_path))
            logging.info(chunk_name)
            if(os.path.exists(output_folder)):      
                file_path = os.path.join(output_folder, chunk_name)
            else:
                os.mkdir(output_folder)
                file_path = os.path.join(output_folder, chunk_name)

            chunk_names.append(file_path)
            chunk.export(file_path, format="mp3")

        return chunk_names

def whisper_transcription(file):
    # client = OpenAI()
    defaults = {
        "engine": "whisper",
    }

    options = defaults.copy()
    logging.info(options)
    audio_file = open(file, "rb")
    transcript = openai.Audio.transcribe(
        model='whisper',
        file=audio_file,
        # **options
        )
    return transcript


def process_audio(audio_file_path):
        starttime = datetime.now()
        if audio_file_path.lower().endswith(".mp3"):
            # No need to convert, directly process the MP3 file
            audio_file_path = audio_file_path
        elif audio_file_path.lower().endswith((".m4a", ".mp4")):
            # Convert M4A or MP4 file to MP3
            audio_file_path = convert_to_mp3(audio_file_path)
        else:
            raise ValueError("Unsupported video/audio file format. Please provide an MP3, M4A, or MP4 file.")

        chunk_names = audio_chunk(audio_file_path)
        
        with ThreadPoolExecutor() as executor:
            futures = [executor.submit(whisper_transcription,chunk) for chunk in chunk_names]

        transcript_file_path = os.path.join(output_folder, "transcript.txt")
        with open(transcript_file_path, "w+") as transcript_file:
            for future in futures:
                transcript = future.result()
                transcript_file.write(transcript + ".")
        file_data = open(transcript_file_path,"r")
        end_t = datetime.now() - starttime
        logging.info("Total Process Time")
        logging.info(end_t)
        return file_data

def read_audio_from_azure():
    blob = BlobClient.from_connection_string(conn_str="", container_name="outputfiles", blob_name="GMT20231107-075636_Recording.m4a")

    downloaded_file_path = "downloaded_audio.mp3"

    with open(downloaded_file_path, "wb") as audio_file:
        audio_file.write(blob.download_blob().readall())
    logging.info("File downloaded calling proces audio now!")
    data = process_audio(downloaded_file_path)
    return data
1

There are 1 best solutions below

0
On

To use Whisper Model in Open AI and Azure Open AI-API with Azure Functions to transcribe speech to text, Refer this SO thread answer by Fabien Snauwaert.

For Just OPEN AI you need Open AI API key and you can transcribe the audio like below:-

My init.py:-

import logging
import azure.functions as func
import os
import time
import openai

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    audio_file = open("temp.wav", "rb")

    start_time = time.time()
    transcript = openai.Audio.transcribe(
        # per-method override settings for OpenAI:
        api_type="openai",
        api_key="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",
        # regular settings:
        model="whisper-1",
        file=audio_file,
        response_format="verbose_json",
        language="en",
    )
    end_time = time.time()
    logging.info(f"API call took {end_time - start_time:.2f} seconds")

    logging.info('Transcript: %s', str(transcript)) 

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        if transcript and 'text' in transcript:
            return func.HttpResponse(transcript['text'], status_code=200)
        else:
            return func.HttpResponse(
                "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
                status_code=200
            )

Output:-

enter image description here

enter image description here

For Azure Open AI API, Resource not found error occurs when your Open AI Base or Key is incorrect or contains any incorrect character. Refer my SO thread answer on same error.

Refer this MS document and check if your Deployed whisper-1 model is correct and you are passing a correct name. Here's an Azure Functions init.py with Azure OpenAI:-

init.py:-

import logging
import azure.functions as func
import os
import time
import openai

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    audio_file = open("temp.wav", "rb")

    start_time = time.time()
    transcript = openai.Audio.transcribe(
        # per-method override settings for Azure:
        api_type="azure",
        api_base=os.getenv("AZURE_OPENAI_WESTEUROPE_ENDPOINT"),
        api_key=os.getenv("AZURE_OPENAI_WESTEUROPE_KEY"),
        api_version="2023-09-01-preview",
        deployment_id="whisper",
        # regular settings:
        model="whisper-1",
        file=audio_file,
        response_format="verbose_json",
        language="en",
    )

    end_time = time.time()
    logging.info(f"API call took {end_time - start_time:.2f} seconds")

    logging.info('Transcript: %s', str(transcript)) 

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        if transcript and 'text' in transcript:
            return func.HttpResponse(transcript['text'], status_code=200)
        else:
            return func.HttpResponse(
                "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
                status_code=200
            )