final_assignment_agentic_course / tools_langchain_audio.py
JPM34's picture
Created distincts file for langchain and smolagent tools
3b013ba
Raw
History Blame Contribute Delete
1.35 kB
# 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)}"