Download tools.py from ESkuratov/Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 13.6 kB
-
https://huggingface.co/spaces/ESkuratov/Final_Assignment_Template/resolve/main/tools.py
- Command line
-
hf download hf://spaces/ESkuratov/Final_Assignment_Template/tools.py
-
curl -L -o tools.py https://huggingface.co/spaces/ESkuratov/Final_Assignment_Template/resolve/main/tools.py
13.6 kB
| import os | |
| from dotenv import load_dotenv | |
| from logger import logger | |
| import base64 | |
| from PIL import Image | |
| from io import BytesIO | |
| import pytesseract | |
| from typing import Dict, Any, Optional | |
| from langchain_core.tools import tool | |
| from langchain_community.tools.tavily_search import TavilySearchResults | |
| from langchain_community.tools import DuckDuckGoSearchRun | |
| from langchain_community.document_loaders import WikipediaLoader | |
| from langchain_community.document_loaders import ArxivLoader | |
| load_dotenv() | |
| tavily_api_key = os.getenv("TAVILY_API_KEY") | |
| if not tavily_api_key: | |
| logger.warning("TAVILY_API_KEY not found in environment variables.") | |
| # 1. Web search tool (using the @tool decorator) | |
| def web_search(query: str) -> str: | |
| """Search the web for information. | |
| Args: | |
| query: The search query. | |
| Returns: | |
| str: Search results. | |
| """ | |
| search_tool = TavilySearchResults(api_key=tavily_api_key) | |
| return search_tool.invoke(query) | |
| # 2. Wiki search tool | |
| def wiki_search(query: str) -> str: | |
| """Search Wikipedia for information. | |
| Args: | |
| query: The search topic. | |
| Returns: | |
| str: Wikipedia article content. | |
| """ | |
| try: | |
| loader = WikipediaLoader(query=query, load_max_docs=2) | |
| docs = loader.load() | |
| return "\n\n".join([doc.page_content for doc in docs]) | |
| except Exception as e: | |
| return f"Error searching Wikipedia: {str(e)}" | |
| def arxiv_search(query: str) -> str: | |
| """Search Arxiv for a query and return maximum 3 result. | |
| Args: | |
| query: The search query. | |
| Returns: | |
| An array documents | |
| """ | |
| print("arxiv_search") | |
| search_docs = ArxivLoader(query=query, load_max_docs=3).load() | |
| formatted_search_docs = "\n\n---\n\n".join( | |
| [ | |
| f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>' | |
| for doc in search_docs | |
| ]) | |
| return {"arvix_results": formatted_search_docs} | |
| # 3. Basic Math Operations | |
| def multiply(a: float, b: float) -> float: | |
| """Multiply two numbers. | |
| Args: | |
| a: The first number. | |
| b: The second number. | |
| Returns: | |
| float: The product of the two numbers. | |
| """ | |
| return a * b | |
| def add(a: float, b: float) -> float: | |
| """Add two numbers. | |
| Args: | |
| a: The first number. | |
| b: The second number. | |
| Returns: | |
| float: The sum of the two numbers. | |
| """ | |
| return a + b | |
| def subtract(a: float, b: float) -> float: | |
| """Subtract two numbers. | |
| Args: | |
| a: The first number. | |
| b: The second number. | |
| Returns: | |
| float: The difference between the two numbers. | |
| """ | |
| return a - b | |
| def divide(a: float, b: float) -> float: | |
| """Divide two numbers. | |
| Args: | |
| a: The first number. | |
| b: The second number. | |
| Returns: | |
| float: The quotient of the division. | |
| """ | |
| if b == 0: | |
| raise ValueError("Cannot divide by zero.") | |
| return a / b | |
| def square_root(a: float) -> float | complex: | |
| """ | |
| Get the square root of a number. | |
| Args: | |
| a (float): the number to get the square root of | |
| """ | |
| if a >= 0: | |
| return a**0.5 | |
| return cmath.sqrt(a) | |
| # 4. File Operations | |
| def save_file(content: str, file_path: str) -> str: | |
| """Save content to a file. | |
| Args: | |
| content: Content to save. | |
| file_path: Path to save the file. | |
| Returns: | |
| str: Status message. | |
| """ | |
| try: | |
| with open(file_path, 'w') as f: | |
| f.write(content) | |
| return f"Successfully saved content to {file_path}" | |
| except Exception as e: | |
| return f"Error saving file: {str(e)}" | |
| def read_file(file_path: str) -> str: | |
| """Read content from a file. | |
| Args: | |
| file_path: Path to the file. | |
| Returns: | |
| str: File content. | |
| """ | |
| try: | |
| with open(file_path, 'r') as f: | |
| content = f.read() | |
| return content | |
| except Exception as e: | |
| return f"Error reading file: {str(e)}" | |
| # 5. OCR for images | |
| def extract_text_from_image(image_path_or_base64: str) -> str: | |
| """Extract text from an image using OCR. | |
| Args: | |
| image_path_or_base64: Path to image file or base64 encoded image. | |
| Returns: | |
| str: Extracted text. | |
| """ | |
| try: | |
| # Check if input is a file path or base64 | |
| if os.path.exists(image_path_or_base64): | |
| image = Image.open(image_path_or_base64) | |
| else: | |
| # Assume it's base64 | |
| try: | |
| image_data = base64.b64decode(image_path_or_base64) | |
| image = Image.open(BytesIO(image_data)) | |
| except: | |
| return "Error: Invalid image path or base64 string" | |
| # Extract text | |
| text = pytesseract.image_to_string(image) | |
| return text | |
| except Exception as e: | |
| return f"Error extracting text from image: {str(e)}" | |
| def get_youtube_transcript(page_url: str) -> str: | |
| """Get the transcript of a YouTube video | |
| Args: | |
| page_url (str): YouTube URL of the video | |
| """ | |
| try: | |
| # get video ID from URL | |
| video_id = extract.video_id(page_url) | |
| # get transcript | |
| ytt_api = YouTubeTranscriptApi() | |
| transcript = ytt_api.fetch(video_id) | |
| # keep only text | |
| txt = '\n'.join([s.text for s in transcript.snippets]) | |
| return txt | |
| except Exception as e: | |
| return f"get_youtube_transcript failed: {e}" | |
| def transcribe_audio(file_path: str): | |
| """ | |
| Transcribes an audio file to text using local Whisper model. | |
| Args: | |
| file_path: Path to the audio file | |
| Returns: | |
| A dictionary containing the transcription and metadata | |
| """ | |
| try: | |
| print(f"Transcribing audio file: {file_path}") | |
| # Validate file exists | |
| if not os.path.exists(file_path): | |
| return { | |
| "status": "error", | |
| "message": f"File not found: {file_path}" | |
| } | |
| # Load a Whisper model - we'll use the small model for better performance | |
| # Options include: tiny, base, small, medium, large | |
| model = whisper.load_model("small") | |
| # Transcribe the audio | |
| result = model.transcribe(file_path) | |
| print({ | |
| "status": "success", | |
| "transcription": result["text"], | |
| "language": result.get("language", "unknown"), | |
| "file_path": file_path | |
| }) | |
| # Return the transcription and metadata | |
| return { | |
| "status": "success", | |
| "transcription": result["text"], | |
| "language": result.get("language", "unknown"), | |
| "file_path": file_path | |
| } | |
| except Exception as e: | |
| print({ | |
| "status": "error", | |
| "message": f"Error transcribing audio: {str(e)}" | |
| }) | |
| return { | |
| "status": "error", | |
| "message": f"Error transcribing audio: {str(e)}" | |
| } | |
| def execute_code_multilang(code: str, language: str = "python") -> str: | |
| """Execute code in multiple languages (Python, Bash, SQL, C, Java) and return results. | |
| Args: | |
| code (str): The source code to execute. | |
| language (str): The language of the code. Supported: "python", "bash", "sql", "c", "java". | |
| Returns: | |
| A string summarizing the execution results (stdout, stderr, errors, plots, dataframes if any). | |
| """ | |
| supported_languages = ["python", "bash", "sql", "c", "java"] | |
| language = language.lower() | |
| if language not in supported_languages: | |
| return f"❌ Unsupported language: {language}. Supported languages are: {', '.join(supported_languages)}" | |
| result = interpreter_instance.execute_code(code, language=language) | |
| response = [] | |
| if result["status"] == "success": | |
| response.append(f"✅ Code executed successfully in **{language.upper()}**") | |
| if result.get("stdout"): | |
| response.append( | |
| "\n**Standard Output:**\n```\n" + result["stdout"].strip() + "\n```" | |
| ) | |
| if result.get("stderr"): | |
| response.append( | |
| "\n**Standard Error (if any):**\n```\n" | |
| + result["stderr"].strip() | |
| + "\n```" | |
| ) | |
| if result.get("result") is not None: | |
| response.append( | |
| "\n**Execution Result:**\n```\n" | |
| + str(result["result"]).strip() | |
| + "\n```" | |
| ) | |
| if result.get("dataframes"): | |
| for df_info in result["dataframes"]: | |
| response.append( | |
| f"\n**DataFrame `{df_info['name']}` (Shape: {df_info['shape']})**" | |
| ) | |
| df_preview = pd.DataFrame(df_info["head"]) | |
| response.append("First 5 rows:\n```\n" + str(df_preview) + "\n```") | |
| if result.get("plots"): | |
| response.append( | |
| f"\n**Generated {len(result['plots'])} plot(s)** (Image data returned separately)" | |
| ) | |
| else: | |
| response.append(f"❌ Code execution failed in **{language.upper()}**") | |
| if result.get("stderr"): | |
| response.append( | |
| "\n**Error Log:**\n```\n" + result["stderr"].strip() + "\n```" | |
| ) | |
| return "\n".join(response) | |
| def download_file_from_url(url: str, filename: Optional[str] = None) -> str: | |
| """ | |
| Download a file from a URL and save it to a temporary location. | |
| Args: | |
| url (str): the URL of the file to download. | |
| filename (str, optional): the name of the file. If not provided, a random name file will be created. | |
| """ | |
| try: | |
| # Parse URL to get filename if not provided | |
| if not filename: | |
| path = urlparse(url).path | |
| filename = os.path.basename(path) | |
| if not filename: | |
| filename = f"downloaded_{uuid.uuid4().hex[:8]}" | |
| # Create temporary file | |
| temp_dir = tempfile.gettempdir() | |
| filepath = os.path.join(temp_dir, filename) | |
| # Download the file | |
| response = requests.get(url, stream=True) | |
| response.raise_for_status() | |
| # Save the file | |
| with open(filepath, "wb") as f: | |
| for chunk in response.iter_content(chunk_size=8192): | |
| f.write(chunk) | |
| return f"File downloaded to {filepath}. You can read this file to process its contents." | |
| except Exception as e: | |
| return f"Error downloading file: {str(e)}" | |
| def analyze_csv_file(file_path: str, query: str) -> str: | |
| """ | |
| Analyze a CSV file using pandas and answer a question about it. | |
| Args: | |
| file_path (str): the path to the CSV file. | |
| query (str): Question about the data | |
| """ | |
| try: | |
| # Read the CSV file | |
| df = pd.read_csv(file_path) | |
| # Run various analyses based on the query | |
| result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n" | |
| result += f"Columns: {', '.join(df.columns)}\n\n" | |
| # Add summary statistics | |
| result += "Summary statistics:\n" | |
| result += str(df.describe()) | |
| return result | |
| except Exception as e: | |
| return f"Error analyzing CSV file: {str(e)}" | |
| def analyze_excel_file(file_path: str, query: str) -> str: | |
| """ | |
| Analyze an Excel file using pandas and answer a question about it. | |
| Args: | |
| file_path (str): the path to the Excel file. | |
| query (str): Question about the data | |
| """ | |
| try: | |
| # Read the Excel file | |
| df = pd.read_excel(file_path) | |
| # Run various analyses based on the query | |
| result = ( | |
| f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n" | |
| ) | |
| result += f"Columns: {', '.join(df.columns)}\n\n" | |
| # Add summary statistics | |
| result += "Summary statistics:\n" | |
| result += str(df.describe()) | |
| return result | |
| except Exception as e: | |
| return f"Error analyzing Excel file: {str(e)}" | |
| def analyze_image(image_base64: str) -> Dict[str, Any]: | |
| """ | |
| Analyze basic properties of an image (size, mode, color analysis, thumbnail preview). | |
| Args: | |
| image_base64 (str): Base64 encoded image string | |
| Returns: | |
| Dictionary with analysis result | |
| """ | |
| try: | |
| img = decode_image(image_base64) | |
| width, height = img.size | |
| mode = img.mode | |
| if mode in ("RGB", "RGBA"): | |
| arr = np.array(img) | |
| avg_colors = arr.mean(axis=(0, 1)) | |
| dominant = ["Red", "Green", "Blue"][np.argmax(avg_colors[:3])] | |
| brightness = avg_colors.mean() | |
| color_analysis = { | |
| "average_rgb": avg_colors.tolist(), | |
| "brightness": brightness, | |
| "dominant_color": dominant, | |
| } | |
| else: | |
| color_analysis = {"note": f"No color analysis for mode {mode}"} | |
| thumbnail = img.copy() | |
| thumbnail.thumbnail((100, 100)) | |
| thumb_path = save_image(thumbnail, "thumbnails") | |
| thumbnail_base64 = encode_image(thumb_path) | |
| return { | |
| "dimensions": (width, height), | |
| "mode": mode, | |
| "color_analysis": color_analysis, | |
| "thumbnail": thumbnail_base64, | |
| } | |
| except Exception as e: | |
| return {"error": str(e)} | |