navodit17/Final_Assignment_Template
0
1from smolagents import Tool, tool2from youtube_transcript_api import YouTubeTranscriptApi3from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline4import torch5 6@tool7def fetch_webpage(url: str, convert_to_markdown: bool = True) -> str:8 """9 Visit a website / url and fetch the content of the webpage. 10 if markdown conversion is enabled, it will remove script and style and return the text content as markdown else return raw unfiltered HTML11 Args:12 url (str): The URL to fetch.13 convert_to_markdown (bool): If True, convert the HTML content to Markdown format. else return the raw HTML.14 Returns:15 str: The HTML content of the URL.16 """17 import requests18 from bs4 import BeautifulSoup19 from markdownify import markdownify as md20 21 content = None22 headers = {23 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'24 }25 response = requests.get(url, timeout=30, headers=headers)26 27 if (convert_to_markdown):28 soup = BeautifulSoup(response.text, "html.parser")29 30 # remove script and style tags31 for script in soup(["script", "style"]):32 script.extract()33 34 # for wikipedia only keep the main content35 if "wikipedia.org" in url:36 37 elements_to_remove = [38 # Navigation and reference elements39 {'class': 'navbox'},40 {'class': 'navbox-group'},41 {'class': 'reflist'},42 {'class': 'navigation-box'},43 {'class': 'sister-project'},44 {'class': 'metadata'},45 {'class': 'interlanguage-link'},46 {'class': 'catlinks'},47 {'id': 'References'},48 {'id': 'External_links'},49 {'id': 'Further_reading'},50 {'id': 'See_also'},51 {'id': 'Notes'},52 ]53 54 for selector in elements_to_remove:55 elements = soup.find_all(attrs=selector)56 for element in elements:57 # For ID-based elements, remove the parent section58 if 'id' in selector:59 parent = element.parent60 if parent and parent.name in ['h2', 'h3', 'h4']:61 # Remove heading and all content until next heading62 current = parent63 while current and current.next_sibling:64 next_elem = current.next_sibling65 if (hasattr(next_elem, 'name') and 66 next_elem.name in ['h2', 'h3', 'h4']):67 break68 if hasattr(next_elem, 'decompose'):69 next_elem.decompose()70 else:71 current = next_elem72 parent.decompose()73 else:74 element.decompose()75 76 main_content = soup.find("main",{"id":"content"})77 if main_content:78 content = md(str(main_content),strip=['script', 'style'], heading_style="ATX").strip()79 else:80 content = md(response.text,strip=['script', 'style'], heading_style="ATX").strip()81 else:82 # Fallback for all other sites - from chatgpt - not tested83 content = md(str(soup), strip=['script', 'style'], heading_style="ATX").strip()84 else:85 content = response.text86 87 return content88 89 90@tool91def read_file_tool(file_path: str) -> str:92 """93 Tool to read a file and return its content.94 95 Args:96 file_path (str): Path to the file to read.97 98 Returns:99 str: Content of the file or error message.100 """101 try:102 with open(file_path, "r") as file:103 return file.read()104 except Exception as e:105 return f"Error reading file: {str(e)}"106 107 108@tool109def get_youtube_transcript(video_id: str) -> str:110 """111 Fetches the transcript of a YouTube video given its video ID. 112 Args:113 video_id (str): The ID of the YouTube video. Pass in the video ID, NOT the video URL. For a video with the URL https://www.youtube.com/watch?v=12345 the ID is 12345.114 Returns:115 str: The transcript of the YouTube video. as a single string with each line separated by a newline character.116 """117 # Initialize the YouTubeTranscriptApi118 ytt_api = YouTubeTranscriptApi()119 fetched_transcript = ytt_api.fetch(video_id)120 raw_data = fetched_transcript.to_raw_data()121 # raw data is in the form of [{ 'text': 'Hey there', 'start': 0.0, 'duration': 1.54 }, { 'text': 'how are you',, 'start': 1.54, 'duration': 4.16 }, ... ] we will return ony the text element as lines122 transcript = "\n".join([item['text'] for item in raw_data])123 return transcript124 125 126@tool127def transcribe_audio(audio_path: str) -> str:128 """129 Speech to Text - transcribes audio file and returns the text130 131 Args:132 audio_path (str): Local file path to the audio133 134 Returns:135 str: The transcript of the audio file136 """137 138 device = "cuda:0" if torch.cuda.is_available() else "cpu"139 torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32140 141 model_id = "openai/whisper-small"142 143 model = AutoModelForSpeechSeq2Seq.from_pretrained(144 model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True145 )146 model.to(device)147 148 processor = AutoProcessor.from_pretrained(model_id)149 150 pipe = pipeline(151 "automatic-speech-recognition",152 model=model,153 tokenizer=processor.tokenizer,154 feature_extractor=processor.feature_extractor,155 torch_dtype=torch_dtype,156 device=device,157 chunk_length_s=30,158 )159 160 result = pipe(audio_path)161 return result162 