Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from langchain_core.tools import tool
|
| 2 |
-
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 3 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 4 |
-
from
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
import whisper
|
| 7 |
import os
|
|
@@ -129,33 +129,21 @@ def read_excel_summary(file_path: str) -> str:
|
|
| 129 |
except Exception as e:
|
| 130 |
return f"Error reading Excel file: {str(e)}"
|
| 131 |
|
| 132 |
-
@tool
|
| 133 |
-
def web_search(query: str) -> str:
|
| 134 |
-
"""Search Tavily for a query and return maximum 3 results.
|
| 135 |
-
|
| 136 |
-
Args:
|
| 137 |
-
query: The search query."""
|
| 138 |
-
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
| 139 |
-
formatted_search_docs = "\n\n---\n\n".join(
|
| 140 |
-
[
|
| 141 |
-
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 142 |
-
for doc in search_docs
|
| 143 |
-
])
|
| 144 |
-
return {"web_results": formatted_search_docs}
|
| 145 |
-
|
| 146 |
@tool
|
| 147 |
def wiki_search(query: str) -> str:
|
| 148 |
-
"""
|
| 149 |
|
| 150 |
Args:
|
| 151 |
-
query: The search query."""
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
| 159 |
|
| 160 |
@tool
|
| 161 |
def transcribe_audio(file_path: str) -> str:
|
|
@@ -172,21 +160,6 @@ def transcribe_audio(file_path: str) -> str:
|
|
| 172 |
except Exception as e:
|
| 173 |
return f"Error during transcription: {str(e)}"
|
| 174 |
|
| 175 |
-
@tool
|
| 176 |
-
def arvix_search(query: str) -> str:
|
| 177 |
-
"""Search Arxiv for a query and return maximum 3 result.
|
| 178 |
-
This tool allows for searching papers and articles
|
| 179 |
-
|
| 180 |
-
Args:
|
| 181 |
-
query: The search query."""
|
| 182 |
-
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 183 |
-
formatted_search_docs = "\n\n---\n\n".join(
|
| 184 |
-
[
|
| 185 |
-
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
|
| 186 |
-
for doc in search_docs
|
| 187 |
-
])
|
| 188 |
-
return {"arvix_results": formatted_search_docs}
|
| 189 |
-
|
| 190 |
@tool
|
| 191 |
def fetch_youtube_transcript(video_url: str) -> str:
|
| 192 |
"""
|
|
|
|
| 1 |
from langchain_core.tools import tool
|
|
|
|
| 2 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 3 |
+
from SPARQLWrapper import SPARQLWrapper, JSON
|
| 4 |
+
import jsonr
|
| 5 |
import pandas as pd
|
| 6 |
import whisper
|
| 7 |
import os
|
|
|
|
| 129 |
except Exception as e:
|
| 130 |
return f"Error reading Excel file: {str(e)}"
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
@tool
|
| 133 |
def wiki_search(query: str) -> str:
|
| 134 |
+
"""Query semantic knowledge bases using SPARQL
|
| 135 |
|
| 136 |
Args:
|
| 137 |
+
query: The search query for SPARQL."""
|
| 138 |
+
try:
|
| 139 |
+
endpoint = SPARQLWrapper("https://query.wikidata.org/sparql")
|
| 140 |
+
endpoint.setQuery(query)
|
| 141 |
+
endpoint.setReturnFormat(JSON)
|
| 142 |
+
|
| 143 |
+
results = endpoint.query().convert()
|
| 144 |
+
return json.dumps(results, indent=2)
|
| 145 |
+
except Exception as e:
|
| 146 |
+
return f"Error executing SPARQL query: {str(e)}"
|
| 147 |
|
| 148 |
@tool
|
| 149 |
def transcribe_audio(file_path: str) -> str:
|
|
|
|
| 160 |
except Exception as e:
|
| 161 |
return f"Error during transcription: {str(e)}"
|
| 162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
@tool
|
| 164 |
def fetch_youtube_transcript(video_url: str) -> str:
|
| 165 |
"""
|