Update tools.py
Browse files
tools.py
CHANGED
|
@@ -1,57 +1,38 @@
|
|
| 1 |
-
from
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
]
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"description": "The username of the model author/organization to find models from."
|
| 40 |
-
}
|
| 41 |
-
}
|
| 42 |
-
output_type = "string"
|
| 43 |
-
|
| 44 |
-
def forward(self, author: str):
|
| 45 |
-
try:
|
| 46 |
-
# List models from the specified author, sorted by downloads
|
| 47 |
-
models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
|
| 48 |
-
|
| 49 |
-
if models:
|
| 50 |
-
model = models[0]
|
| 51 |
-
return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
|
| 52 |
-
else:
|
| 53 |
-
return f"No models found for author {author}."
|
| 54 |
-
except Exception as e:
|
| 55 |
-
return f"Error fetching models for {author}: {str(e)}"
|
| 56 |
-
|
| 57 |
-
|
|
|
|
| 1 |
+
from langchain.tools import DuckDuckGoSearchRun
|
| 2 |
+
import pytesseract
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import whisper
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# --- Web Search Tool ---
|
| 8 |
+
def web_search_tool(question: str) -> str:
|
| 9 |
+
try:
|
| 10 |
+
return DuckDuckGoSearchRun().run(question)
|
| 11 |
+
except Exception as e:
|
| 12 |
+
return f"Web search failed: {str(e)}"
|
| 13 |
+
|
| 14 |
+
# --- Image Analysis Tool ---
|
| 15 |
+
def image_analysis_tool(image_path: str) -> str:
|
| 16 |
+
try:
|
| 17 |
+
image = Image.open(image_path)
|
| 18 |
+
text = pytesseract.image_to_string(image)
|
| 19 |
+
return f"Detected text in image:\n{text.strip()}"
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Image analysis failed: {str(e)}"
|
| 22 |
+
|
| 23 |
+
# --- Audio Transcription Tool ---
|
| 24 |
+
def audio_transcription_tool(audio_path: str) -> str:
|
| 25 |
+
try:
|
| 26 |
+
model = whisper.load_model("base")
|
| 27 |
+
result = model.transcribe(audio_path)
|
| 28 |
+
return result["text"].strip()
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Audio transcription failed: {str(e)}"
|
| 31 |
+
|
| 32 |
+
# --- Document Tool ---
|
| 33 |
+
def document_analysis_tool(text: str) -> str:
|
| 34 |
+
return f"Document contains {len(text.split())} words.\n\nSummary not yet implemented."
|
| 35 |
+
|
| 36 |
+
# --- General LLM Logic Tool ---
|
| 37 |
+
def llm_tool(question: str, llm) -> str:
|
| 38 |
+
return llm(f"Answer this question: {question}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|