Commit ·
98e87f9
1
Parent(s): d1b9537
add image transcriber function
Browse files12345613 unmodified lines20212223242526from smolagents import ToolCallingAgent, DuckDuckGoSearchTool, VisitWebpageTool, AmazonBedrockServerModelfrom tools import AudioTranscriber, txt_reader, pdf_reader, excel_reader, math_calculatorMAIN_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:13 unmodified lines"""Main agent that orchestrates file handling and web search tasks."""def __init__(self):model = AmazonBedrockServerModel(model_id="anthropic.claude-3-5-sonnet-20241022-v2:0")file_handler_agent = ToolCallingAgent(name="FileHandlerAgent",description="Handles file-related tasks such as reading Excel, text, PDF files, etc.",123456713 unmodified lines21222324252627from smolagents import ToolCallingAgent, DuckDuckGoSearchTool, VisitWebpageTool, AmazonBedrockServerModelfrom tools import AudioTranscriber, txt_reader, pdf_reader, excel_reader, math_calculatorfrom utils import BEDROCK_MODEL_IDMAIN_PROMPT = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template:13 unmodified lines"""Main agent that orchestrates file handling and web search tasks."""def __init__(self):model = AmazonBedrockServerModel(model_id=BEDROCK_MODEL_ID)file_handler_agent = ToolCallingAgent(name="FileHandlerAgent",description="Handles file-related tasks such as reading Excel, text, PDF files, etc.",
1 unmodified line2345673 unmodified lines111213141516171819202122232425262728108 unmodified lines1371381391 unmodified lineimport astimport jsonimport osfrom time import sleepfrom uuid import uuid43 unmodified linesfrom smolagents import tool, Toolfrom definitions import TranscriptionJobfrom utils import get_file, s3_upload_file, s3_download_file@tooldef math_calculator(query: str) -> str:"""A simple calculator tool that evaluates mathematical expressions.Args:query (str): A mathematical expression as a string, e.g., "2 + 2 * 3"."""try:result = ast.literal_eval(query)return str(result)except Exception as e:return f"Error evaluating expression: {e}"108 unmodified linesreturn transcriptionexcept Exception as e:return f"Error starting transcription job for {file_name}: {e}"1 unmodified line23456783 unmodified lines121314151617181920212223242526272829108 unmodified lines1381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821 unmodified lineimport astimport jsonimport osimport base64from time import sleepfrom uuid import uuid43 unmodified linesfrom smolagents import tool, Toolfrom definitions import TranscriptionJobfrom utils import get_file, s3_upload_file, s3_download_file, bedrock_runtime, BEDROCK_MODEL_ID@tooldef math_calculator(expression: str) -> str:"""A simple calculator tool that evaluates mathematical expressions.Args:expression (str): A mathematical expression as a string, e.g., "2 + 2 * 3"."""try:result = ast.literal_eval(expression)return str(result)except Exception as e:return f"Error evaluating expression: {e}"108 unmodified linesreturn transcriptionexcept Exception as e:return f"Error starting transcription job for {file_name}: {e}"@tooldef image_transcriber(text_prompt: str, task_id: str, file_name: str) -> str:"""Transcribes text from an image fileArgs:text_prompt (str): The text prompt to guide the transcription.task_id (str): The ID of the task associated with the image file.file_name (str): The name of the image file to transcribe."""try:file_content = get_file(task_id)base64_image = base64.b64encode(file_content).decode("utf-8")response = bedrock_runtime.invoke_model(modelId=BEDROCK_MODEL_ID,body=json.dumps({"anthropic_version": "bedrock-2023-05-31","max_tokens": 4096,"messages": [{"role": "user","content": [{"type": "image","source": {"type": "base64","media_type": f"image/{file_name.split('.')[-1]}","data": base64_image,},},{"type": "text", "text": text_prompt},],}],}),).read()return json.loads(response)["message"]["content"][0]["text"]except Exception as e:return f"Error processing image file {file_name}: {e}"
5 unmodified lines67891011121314151617181920215 unmodified linesfrom dotenv import load_dotenvDEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"load_dotenv()bedrock_client = boto3.client("bedrock-runtime",region_name=os.getenv("AWS_REGION"),aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),)def get_file(task_id: str) -> BytesIO:5 unmodified lines6789101112131415165 unmodified linesfrom dotenv import load_dotenvDEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"BEDROCK_MODEL_ID = "anthropic.claude-3-5-sonnet-20241022-v2:0"load_dotenv()bedrock_runtime = boto3.client("bedrock-runtime", region_name=os.getenv("AWS_REGION"))def get_file(task_id: str) -> BytesIO:
agent.py+2-1
tools.py+47-4
utils.py+2-7