File size: 1,351 Bytes
9505e38
6c9d559
 
9505e38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Audio Transcription Tool
import os

from google import genai
from google.genai import types
from langchain_core.tools import tool


@tool
def transcribe_audio(audio_file_path: str, mime_type: str) -> str:
    """Transcribes an audio file using Gemini's audio capabilities.
    Args:
        audio_file_path (str): the path to the audio file to transcribe.
        mime_type (str): the mime type of the audio file.
    Returns:
        str: The transcript of the audio file.
    """
    try:
        # Initialize the model
        client = genai.Client(api_key=os.getenv("GEMINI_KEY"))
        model = "models/gemini-1.5-flash-8b"

        # Read and encode the audio file
        with open(audio_file_path, "rb") as audio_file:
            audio_data = audio_file.read()

        # Create the content with audio data
        contents = types.Content(
            parts=[
                types.Part.from_bytes(
                    data=audio_data,
                    mime_type=mime_type,
                ),
                types.Part(text="Please transcribe this audio file."),
            ]
        )

        # Generate transcription
        response = client.models.generate_content(
            model=model, contents=contents
        )
        return response.text
    except Exception as e:
        return f"Error transcribing audio: {str(e)}"