Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,6 +16,7 @@ import tempfile
|
|
| 16 |
import ast
|
| 17 |
import pytesseract
|
| 18 |
from PIL import Image
|
|
|
|
| 19 |
# Or using AudioTranscriptTool.
|
| 20 |
|
| 21 |
|
|
@@ -28,12 +29,14 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 28 |
|
| 29 |
############### tool
|
| 30 |
|
|
|
|
| 31 |
def divide(a: int|float, b: int|float) -> float:
|
| 32 |
"""Divide a and b."""
|
| 33 |
if b == 0:
|
| 34 |
return "Cannot divide by zero."
|
| 35 |
return a / b
|
| 36 |
|
|
|
|
| 37 |
def multiply(a: int|float, b: int|float) -> float:
|
| 38 |
"""Multiply a and b."""
|
| 39 |
return a * b
|
|
@@ -43,7 +46,8 @@ def add(a: int|float, b: int|float) -> float:
|
|
| 43 |
def subtract(a: int|float, b: int|float) -> float:
|
| 44 |
"""Subtract a with b."""
|
| 45 |
return a - b
|
| 46 |
-
|
|
|
|
| 47 |
def pdf_loader_tool(file_url: str) -> str:
|
| 48 |
"""Load and extract text from a PDF file downloaded from given file_url."""
|
| 49 |
try:
|
|
@@ -62,6 +66,7 @@ def pdf_loader_tool(file_url: str) -> str:
|
|
| 62 |
except Exception as e:
|
| 63 |
return f"Reading failed: {str(e)}"
|
| 64 |
|
|
|
|
| 65 |
def docx_loader_tool(file_url: str) -> str:
|
| 66 |
"""Load and extract text from a docx file downloaded from given file_url."""
|
| 67 |
try:
|
|
@@ -79,7 +84,8 @@ def docx_loader_tool(file_url: str) -> str:
|
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
return f"Reading failed: {str(e)}"
|
| 82 |
-
|
|
|
|
| 83 |
def excel_loader_tool(file_url: str) -> str:
|
| 84 |
"""Load and extract text from an Excel file downloaded from given file_url."""
|
| 85 |
try:
|
|
@@ -96,7 +102,8 @@ def excel_loader_tool(file_url: str) -> str:
|
|
| 96 |
return output
|
| 97 |
except Exception as e:
|
| 98 |
return f"Reading failed: {str(e)}"
|
| 99 |
-
|
|
|
|
| 100 |
def txt_loader_tool(file_url: str) -> str:
|
| 101 |
"""Load and extract text from a txt file downloaded from given file_url."""
|
| 102 |
try:
|
|
@@ -114,7 +121,8 @@ def txt_loader_tool(file_url: str) -> str:
|
|
| 114 |
|
| 115 |
except Exception as e:
|
| 116 |
return f"Reading failed: {str(e)}"
|
| 117 |
-
|
|
|
|
| 118 |
def read_image_text(file_URL: str) -> str:
|
| 119 |
"""Extract text from image downloaded from given file_URL using OCR."""
|
| 120 |
try:
|
|
@@ -132,12 +140,14 @@ def read_image_text(file_URL: str) -> str:
|
|
| 132 |
except Exception as e:
|
| 133 |
return f"Reading failed: {str(e)}"
|
| 134 |
|
|
|
|
| 135 |
def youtube_transcript_tool(url: str) -> str:
|
| 136 |
"""Load transcript from a YouTube video URL."""
|
| 137 |
loader = YoutubeLoader.from_youtube_url(url, add_video_info=True)
|
| 138 |
docs = loader.load()
|
| 139 |
return "\n".join([f"Title: {doc.metadata.get('title', 'Unknown')}\nTranscript: {doc.page_content}" for doc in docs])
|
| 140 |
-
|
|
|
|
| 141 |
def analyze_python_code(file_url: str) -> str:
|
| 142 |
"""Downloads an python code from a URL, then analyze it and summarize its structure."""
|
| 143 |
try:
|
|
@@ -157,7 +167,8 @@ def analyze_python_code(file_url: str) -> str:
|
|
| 157 |
|
| 158 |
except Exception as e:
|
| 159 |
return f"Reading failed: {str(e)}"
|
| 160 |
-
|
|
|
|
| 161 |
def transcribe_audio(file_url: str) -> str:
|
| 162 |
"""Downloads an audio file from a URL into a temporary file and transcribes it using SpeechRecognition."""
|
| 163 |
try:
|
|
@@ -177,7 +188,8 @@ def transcribe_audio(file_url: str) -> str:
|
|
| 177 |
|
| 178 |
except Exception as e:
|
| 179 |
return f"Transcription failed: {str(e)}"
|
| 180 |
-
|
|
|
|
| 181 |
def search_wikipedia(query: str) -> str:
|
| 182 |
"""Search English Wikipedia for the given query and return a summary within 3 sentences."""
|
| 183 |
try:
|
|
|
|
| 16 |
import ast
|
| 17 |
import pytesseract
|
| 18 |
from PIL import Image
|
| 19 |
+
from langchain_core.tools import tool
|
| 20 |
# Or using AudioTranscriptTool.
|
| 21 |
|
| 22 |
|
|
|
|
| 29 |
|
| 30 |
############### tool
|
| 31 |
|
| 32 |
+
@tool
|
| 33 |
def divide(a: int|float, b: int|float) -> float:
|
| 34 |
"""Divide a and b."""
|
| 35 |
if b == 0:
|
| 36 |
return "Cannot divide by zero."
|
| 37 |
return a / b
|
| 38 |
|
| 39 |
+
@tool
|
| 40 |
def multiply(a: int|float, b: int|float) -> float:
|
| 41 |
"""Multiply a and b."""
|
| 42 |
return a * b
|
|
|
|
| 46 |
def subtract(a: int|float, b: int|float) -> float:
|
| 47 |
"""Subtract a with b."""
|
| 48 |
return a - b
|
| 49 |
+
|
| 50 |
+
@tool
|
| 51 |
def pdf_loader_tool(file_url: str) -> str:
|
| 52 |
"""Load and extract text from a PDF file downloaded from given file_url."""
|
| 53 |
try:
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
return f"Reading failed: {str(e)}"
|
| 68 |
|
| 69 |
+
@tool
|
| 70 |
def docx_loader_tool(file_url: str) -> str:
|
| 71 |
"""Load and extract text from a docx file downloaded from given file_url."""
|
| 72 |
try:
|
|
|
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
return f"Reading failed: {str(e)}"
|
| 87 |
+
|
| 88 |
+
@tool
|
| 89 |
def excel_loader_tool(file_url: str) -> str:
|
| 90 |
"""Load and extract text from an Excel file downloaded from given file_url."""
|
| 91 |
try:
|
|
|
|
| 102 |
return output
|
| 103 |
except Exception as e:
|
| 104 |
return f"Reading failed: {str(e)}"
|
| 105 |
+
|
| 106 |
+
@tool
|
| 107 |
def txt_loader_tool(file_url: str) -> str:
|
| 108 |
"""Load and extract text from a txt file downloaded from given file_url."""
|
| 109 |
try:
|
|
|
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
return f"Reading failed: {str(e)}"
|
| 124 |
+
|
| 125 |
+
@tool
|
| 126 |
def read_image_text(file_URL: str) -> str:
|
| 127 |
"""Extract text from image downloaded from given file_URL using OCR."""
|
| 128 |
try:
|
|
|
|
| 140 |
except Exception as e:
|
| 141 |
return f"Reading failed: {str(e)}"
|
| 142 |
|
| 143 |
+
@tool
|
| 144 |
def youtube_transcript_tool(url: str) -> str:
|
| 145 |
"""Load transcript from a YouTube video URL."""
|
| 146 |
loader = YoutubeLoader.from_youtube_url(url, add_video_info=True)
|
| 147 |
docs = loader.load()
|
| 148 |
return "\n".join([f"Title: {doc.metadata.get('title', 'Unknown')}\nTranscript: {doc.page_content}" for doc in docs])
|
| 149 |
+
|
| 150 |
+
@tool
|
| 151 |
def analyze_python_code(file_url: str) -> str:
|
| 152 |
"""Downloads an python code from a URL, then analyze it and summarize its structure."""
|
| 153 |
try:
|
|
|
|
| 167 |
|
| 168 |
except Exception as e:
|
| 169 |
return f"Reading failed: {str(e)}"
|
| 170 |
+
|
| 171 |
+
@tool
|
| 172 |
def transcribe_audio(file_url: str) -> str:
|
| 173 |
"""Downloads an audio file from a URL into a temporary file and transcribes it using SpeechRecognition."""
|
| 174 |
try:
|
|
|
|
| 188 |
|
| 189 |
except Exception as e:
|
| 190 |
return f"Transcription failed: {str(e)}"
|
| 191 |
+
|
| 192 |
+
@tool
|
| 193 |
def search_wikipedia(query: str) -> str:
|
| 194 |
"""Search English Wikipedia for the given query and return a summary within 3 sentences."""
|
| 195 |
try:
|