[Python] Azure AI Services for Phi-4-modal-instruct issue with audio - Audio Prompt Error:...

Discussão em 'Python' iniciado por Stack, Junho 10, 2025.

  1. Stack

    Stack Membro Participativo

    Audio Prompt Error: (Invalid input) invalid input error Code: Invalid input Message: invalid input error

    I am getting this above error while testing audio to text using Phi-4-modal-instruct in Azure AI foundry

    import base64
    import os
    from azure.ai.inference import ChatCompletionsClient
    from azure.ai.inference.models import SystemMessage, UserMessage, TextContentItem, ImageContentItem, AudioContentItem, InputAudio, AudioContentFormat
    import io
    from azure.core.credentials import AzureKeyCredential
    from azure.identity import DefaultAzureCredential
    from PIL import Image

    # Configuration
    ENDPOINT_URL = "https://myaiservices.services.ai.azure.com/models"
    MODEL_NAME = "phi-4-multimodal-instruct"
    AUDIO_PATH = "harvard.wav" # Path to a sample audio file

    def test_audio_prompt(client, audio_path):
    """Test an audio input with a prompt."""
    print("\n=== Testing Audio Prompt ===")
    try:
    # Open and encode the audio file to base64

    with open(audio_path, "rb") as audio_file:
    base64_audio = base64.b64encode(audio_file.read()).decode("utf-8")

    # Prepare the audio content
    audio_content = {
    "type": "audio",
    "audio_data": base64_audio,
    "mime_type": "audio/wav"
    }

    # Make the chat completions request
    response = client.complete(
    messages=[
    SystemMessage(content="You are an AI assistant for translating and transcribing audio clips."),
    UserMessage(content=[
    TextContentItem(text="Please print this audio snippet."),
    audio_content
    ])
    ],
    model=MODEL_NAME
    )

    # Print the response
    print("Audio Response:", response.choices[0].message.content)

    except FileNotFoundError:
    print(f"Audio file not found: {audio_path}")
    except Exception as e:
    print("Audio Prompt Error:", str(e))


    def main():
    # Initialize the client
    client = ChatCompletionsClient(
    endpoint=ENDPOINT_URL,
    credential=DefaultAzureCredential(),
    credential_scopes=["https://cognitiveservices.azure.com/.default"]
    )

    test_audio_prompt(client, AUDIO_PATH)


    if __name__ == "__main__":
    main()

    Continue reading...

Compartilhe esta Página