0

I am making a function for voice cloning. It takes parameters these parameter from the user: model name, Audio url.

The function will first download the file and then apply the specified model on the audio. Then the function will upload the result on server and return the url of the file.

I am using Azure function and when i run it locally for testing. The function start okay but when i make a request to it it runs into an error:

RuntimeError: Can't listen for client connections: [Errno 98] Address already in use
[2023-07-04T17:56:49.679Z] Language Worker Process exited. Pid=128697.
[2023-07-04T17:56:49.679Z] python3 exited with code 1 (0x1).     log.reraise_exception("{0}() failed:", func.__name__, level="info"),    raise RuntimeError(str(endpoints["error"])),RuntimeError: Can't listen for client connections: [Errno 98] Address already in use.
[2023-07-04T17:56:49.697Z] Failed to start a new language worker for runtime: python.
[2023-07-04T17:56:49.697Z] System.Private.CoreLib: A task was canceled.

After tracing the error i found that the code runs okay till this function is called

f0, pd = torchcrepe.predict(
                audio,
                self.sr,
                self.window,
                f0_min,
                f0_max,
                model,
                batch_size=batch_size,
                device=self.device,
                return_periodicity=True,
            )

The i got into the torchcrepe library and found that these lines gives me this error.

    # Move model to correct device (no-op if devices are the same)
    infer.model = infer.model.to(frames.device)

    # Apply model
    return infer.model(frames, embed=embed)
John Hanley
  • 74,467
  • 6
  • 95
  • 159
teewhy
  • 15
  • 4

1 Answers1

0

It looks like the server that is running locally is already in use.

You can refer this SO thread answer by Bartosz

Add this line of code to resuse the same address in your code that is running the server:-


udp  = socket.socket(socket.AF_INET,

socket.SOCK_DGRAM)

udp.bind((UDP_IP, UDP_PORT))

udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

Another option is to kill the process of running address according to the answer by MohitGhodasara, Ayub laziz and Abdul Basit:-

$ ps -fA | grep python 501 81211 12368   0  10:11PM ttys000    0:03.12
python -m SimpleHTTPServer 
$ kill 81211

My apply_model_server.py code:-

from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def root():
    return "Welcome to the Voice Cloning API"

@app.route('/apply_model', methods=['POST'])
def apply_model():
    # Get the audio data and model name from the request
    audio_data = request.json['audio']
    model_name = request.json['model_name']

    # Apply the specified model on the audio data
    processed_audio = process_audio(audio_data, model_name)

    # Return the processed audio as the response
    return processed_audio

def process_audio(audio_data, model_name):
    # Your code to apply the model on the audio goes here
    # Implement the logic to apply the specified model on the audio data
    # Example: You can use your voice cloning model here

    # Replace the following line with your actual model application logic
    processed_audio = f"Model: {model_name}, Audio: {audio_data}"

    return processed_audio

if __name__ == '__main__':
    app.run(debug=True)

My apply_model server is running on a different port in local host and my function trigger is running on a different host, Refer below:-

enter image description here

My init.py that runs the function trigger:-

import logging

import azure.functions as func

import requests

# Function to apply the specified model on the audio
def apply_model_on_audio(audio_data, model_name):
    # Your code to apply the model on the audio goes here
    # Example: Assuming you're using a POST request to a local API endpoint for applying the model
    model_api_url = f"http://127.0.0.1:5000/apply_model"
    headers = {"Content-Type": "application/json"}
    data = {
        "audio": audio_data,
        "model_name": model_name
    }
    response = requests.post(model_api_url, json=data, headers=headers)
    processed_audio = response.content
    return processed_audio

def download_audio_file(audio_url):
    response = requests.get(audio_url)
    audio_data = response.content
    return audio_data

# Function to upload the result to the server and get the file URL
def upload_result(result_data):
    # Your code to upload the result to the server and get the file URL goes here
    # Example: Assuming you're using a POST request to a local API endpoint for uploading the result and getting the file URL
    upload_url = "http://localhost:8000/upload"
    headers = {"Content-Type": "application/json"}
    data = {
        "result": result_data
    }
    response = requests.post(upload_url, json=data, headers=headers)
    file_url = response.json().get("file_url")
    return file_url


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

    try:
        req_body = req.get_json()
        model_name = req_body.get('model_name')
        audio_url = req_body.get('audio_url')

        audio_data = download_audio_file(audio_url)
        processed_audio = apply_model_on_audio(audio_data, model_name)
        file_url = upload_result(processed_audio)

        return func.HttpResponse(f"Processed audio file URL: {file_url}", status_code=200)

    except Exception as e:
        logging.error(str(e))
        return func.HttpResponse("Internal Server Error", status_code=500)

Output:-

enter image description here

Both my server and Http Function trigger is running separately in different ports and addresses. Make sure you kill the on running server in the port or close the terminal and run the server code again.

SiddheshDesai
  • 3,668
  • 1
  • 2
  • 11