Spaces:
No application file
No application file
Merge pull request #3 from mathysgrapotte/add-initial-tools
Browse files- main.py +9 -0
- tools/meta_yml_tools.py +89 -0
main.py
CHANGED
|
@@ -2,6 +2,15 @@ from smolagents import CodeAgent, LiteLLMModel
|
|
| 2 |
from smolagents.tools import ToolCollection
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
def chat_with_agent(message, history):
|
| 6 |
"""Initialize MCP client for each request to avoid connection issues"""
|
| 7 |
try:
|
|
|
|
| 2 |
from smolagents.tools import ToolCollection
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
+
from tools.meta_yml_tools import get_meta_yml_file, extract_tools_from_meta_json, extract_information_from_meta_json
|
| 6 |
+
|
| 7 |
+
def main(module_name):
|
| 8 |
+
meta_yml = get_meta_yml_file(module_name=module_name)
|
| 9 |
+
module_tools = extract_tools_from_meta_json(meta_file=meta_yml)
|
| 10 |
+
# TODO: agent to choose the right tool
|
| 11 |
+
tool_name = "fastqc"
|
| 12 |
+
meta_info = extract_information_from_meta_json(meta_file=meta_yml, tool_name=tool_name)
|
| 13 |
+
|
| 14 |
def chat_with_agent(message, history):
|
| 15 |
"""Initialize MCP client for each request to avoid connection issues"""
|
| 16 |
try:
|
tools/meta_yml_tools.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import yaml
|
| 4 |
+
|
| 5 |
+
def get_meta_yml_file(module_name: str) -> dict:
|
| 6 |
+
"""
|
| 7 |
+
Access the nf-core/modules repository and return the meta.yml file of the given module.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
module_name (str): The name of the module to get the meta.yml file for.
|
| 11 |
+
The module_name must be provided in the format <tool>_<subtool> or <tool>/<subtool> or <tool> <subtool>.
|
| 12 |
+
The subtool is optional.
|
| 13 |
+
For example, "bwa_align" or "fastqc" or "bwa align" or "bwa/align".
|
| 14 |
+
Returns:
|
| 15 |
+
dict: The meta.yml file of the given module in json/yaml format as a dictionary.
|
| 16 |
+
"""
|
| 17 |
+
if "_" in module_name:
|
| 18 |
+
tool, subtool = module_name.split("_")
|
| 19 |
+
elif "/" in module_name:
|
| 20 |
+
tool, subtool = module_name.split("/")
|
| 21 |
+
elif " " in module_name:
|
| 22 |
+
tool, subtool = module_name.split(" ")
|
| 23 |
+
else:
|
| 24 |
+
tool, subtool = module_name, ""
|
| 25 |
+
|
| 26 |
+
if subtool:
|
| 27 |
+
url = f"https://raw.githubusercontent.com/nf-core/modules/refs/heads/master/modules/nf-core/{tool}/{subtool}/meta.yml"
|
| 28 |
+
else:
|
| 29 |
+
url = f"https://raw.githubusercontent.com/nf-core/modules/refs/heads/master/modules/nf-core/{tool}/meta.yml"
|
| 30 |
+
try:
|
| 31 |
+
response = requests.get(url)
|
| 32 |
+
response.raise_for_status() # Raise an error for bad status codes
|
| 33 |
+
return yaml.safe_load(response.text)
|
| 34 |
+
except requests.exceptions.RequestException as e:
|
| 35 |
+
raise RuntimeError(f"An error occurred while connecting to the URL: {url}. Error message: {e}")
|
| 36 |
+
|
| 37 |
+
def extract_tools_from_meta_json(meta_file: dict) -> list:
|
| 38 |
+
"""
|
| 39 |
+
Extract the tools from the meta.yml file.
|
| 40 |
+
|
| 41 |
+
Args:
|
| 42 |
+
meta_file (str): The content of the module meta.yml file in json format.
|
| 43 |
+
|
| 44 |
+
Returns:
|
| 45 |
+
list: A list of tools.
|
| 46 |
+
"""
|
| 47 |
+
tools_names = []
|
| 48 |
+
tools_list = meta_file.get("tools", [])
|
| 49 |
+
for tool in tools_list:
|
| 50 |
+
tools_names.append(list(tool.keys())[0])
|
| 51 |
+
return tools_names
|
| 52 |
+
|
| 53 |
+
def extract_information_from_meta_json(meta_file: dict, tool_name: str) -> dict:
|
| 54 |
+
"""
|
| 55 |
+
Extract information metadata from an nf-core module meta.yml file.
|
| 56 |
+
Information extracted:
|
| 57 |
+
- inputs
|
| 58 |
+
- outputs
|
| 59 |
+
- homepage URL
|
| 60 |
+
- documentation URL
|
| 61 |
+
- bio.tools ID
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
Args:
|
| 65 |
+
meta_file (str): The content of the module meta.yml file in json format.
|
| 66 |
+
tool_name (str): The name of the tool to extract information for.
|
| 67 |
+
|
| 68 |
+
Returns:
|
| 69 |
+
dict: A dictionary with the extracted metadata.
|
| 70 |
+
Each file or term can also contain additional metadata like 'description' or 'type'.
|
| 71 |
+
|
| 72 |
+
Example output:
|
| 73 |
+
{
|
| 74 |
+
"inputs": [...],
|
| 75 |
+
"outputs": [...],
|
| 76 |
+
"homepage": "https://www.bioinformatics.babraham.ac.uk/projects/fastqc/",
|
| 77 |
+
"documentation": "https://www.bioinformatics.babraham.ac.uk/projects/fastqc/Help/",
|
| 78 |
+
"bio_tools_id": "biotools:fastqc"
|
| 79 |
+
}
|
| 80 |
+
"""
|
| 81 |
+
inputs = meta_file.get("input", [])
|
| 82 |
+
outputs = meta_file.get("output", [])
|
| 83 |
+
for tool in meta_file.get("tools", []):
|
| 84 |
+
if list(tool.keys())[0] == tool_name:
|
| 85 |
+
homepage_url = tool.get("homepage", "")
|
| 86 |
+
documentation_rul = tool.get("documentation", "")
|
| 87 |
+
bio_tools_id = tool.get("identifier", "")
|
| 88 |
+
print("Extracted metadata information from nf-core module meta.yml")
|
| 89 |
+
return {"inputs": inputs, "outputs": outputs, "homepage": homepage_url, "documentation": documentation_rul, "bio_tools_id": bio_tools_id}
|