[Python] Google's Gemini on local audio files

Discussão em 'Python' iniciado por Stack, Dezembro 22, 2024.

  1. Stack

    Stack Membro Participativo

    Google has a page describing how to use one of their Gemini-1.5 models to transcribe audio. They include a sample script (see below). The script grabs the audio file from Google Storage via the Part.from_uri() command.

    I would, instead, like to use a local file. Setting the URI to "file:///..." does not work. How can I do that?

    Google's (working, cloud-based) code is here:

    import vertexai
    from vertexai.generative_models import GenerativeModel, GenerationConfig, Part

    # TODO(developer): Update and un-comment below line
    # PROJECT_ID = "your-project-id"

    vertexai.init(project=PROJECT_ID, location="us-central1")

    model = GenerativeModel("gemini-1.5-flash-002")

    prompt = """
    Can you transcribe this interview, in the format of timecode, speaker, caption.
    Use speaker A, speaker B, etc. to identify speakers.
    """

    audio_file_uri = "gs://cloud-samples-data/generative-ai/audio/pixel.mp3"
    audio_file = Part.from_uri(audio_file_uri, mime_type="audio/mpeg")

    contents = [audio_file, prompt]

    response = model.generate_content(contents, generation_config=GenerationConfig(audio_timestamp=True))

    print(response.text)
    # Example response:
    # [00:00:00] Speaker A: Your devices are getting better over time...
    # [00:00:16] Speaker B: Welcome to the Made by Google podcast, ...
    # [00:01:00] Speaker A: So many features. I am a singer. ...
    # [00:01:33] Speaker B: Amazing. DeCarlos, same question to you, ...

    Continue reading...

Compartilhe esta Página