Spaces:
Sleeping
Sleeping
create get_gene_id and get_gene_location tools
Browse files
app.py
CHANGED
|
@@ -6,31 +6,67 @@ import yaml
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
@tool
|
| 11 |
-
def
|
| 12 |
-
"""A tool that
|
| 13 |
Args:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
"""
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
@tool
|
| 20 |
-
def
|
| 21 |
-
"""A tool that
|
| 22 |
Args:
|
| 23 |
-
|
| 24 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Get current time in that timezone
|
| 29 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 30 |
-
return f"The current local time in {timezone} is: {local_time}"
|
| 31 |
-
except Exception as e:
|
| 32 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
final_answer = FinalAnswerTool()
|
| 36 |
|
|
@@ -38,22 +74,18 @@ final_answer = FinalAnswerTool()
|
|
| 38 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 39 |
|
| 40 |
model = HfApiModel(
|
| 41 |
-
max_tokens=2096,
|
| 42 |
-
temperature=0.5,
|
| 43 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 44 |
-
custom_role_conversions=None,
|
| 45 |
)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
# Import tool from Hub
|
| 49 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 50 |
-
|
| 51 |
with open("prompts.yaml", 'r') as stream:
|
| 52 |
prompt_templates = yaml.safe_load(stream)
|
| 53 |
|
| 54 |
agent = CodeAgent(
|
| 55 |
model=model,
|
| 56 |
-
tools=[final_answer,
|
| 57 |
max_steps=6,
|
| 58 |
verbosity_level=1,
|
| 59 |
grammar=None,
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
+
import xml.etree.ElementTree as ET
|
| 10 |
+
|
| 11 |
+
base_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/"
|
| 12 |
|
| 13 |
@tool
|
| 14 |
+
def get_gene_id(organism:str, gene:int) -> str:
|
| 15 |
+
"""A tool that gets the gene ID from an organism with the provided gene name
|
| 16 |
Args:
|
| 17 |
+
organism: the name of the organism
|
| 18 |
+
gene: the name of the gene
|
| 19 |
"""
|
| 20 |
+
search_params = {
|
| 21 |
+
"db": "gene",
|
| 22 |
+
"term": f"{gene}[Gene Name] AND {organism}[Organism]",
|
| 23 |
+
"retmode": "xml",
|
| 24 |
+
}
|
| 25 |
+
esearch_url = base_url + "esearch.fcgi"
|
| 26 |
+
esearch_response = requests.get(esearch_url, params=search_params)
|
| 27 |
+
esearch_tree = ET.fromstring(esearch_response.text)
|
| 28 |
+
id_list = esearch_tree.find("IdList")
|
| 29 |
+
|
| 30 |
+
if id_list is None or len(id_list) == 0:
|
| 31 |
+
return "Gene not found."
|
| 32 |
+
|
| 33 |
+
return id_list.find("Id").text
|
| 34 |
|
| 35 |
@tool
|
| 36 |
+
def get_gene_location(gene_id:str) -> str:
|
| 37 |
+
"""A tool that gets the gene location from a gene ID
|
| 38 |
Args:
|
| 39 |
+
gene_id: the gene ID
|
| 40 |
"""
|
| 41 |
+
efetch_url = base_url + "efetch.fcgi"
|
| 42 |
+
fetch_params = {
|
| 43 |
+
"db": "gene",
|
| 44 |
+
"id": gene_id,
|
| 45 |
+
"retmode": "xml",
|
| 46 |
+
}
|
| 47 |
+
efetch_response = requests.get(efetch_url, params=fetch_params)
|
| 48 |
+
fetch_tree = ET.fromstring(efetch_response.text)
|
| 49 |
+
|
| 50 |
+
# Parse XML to extract chromosome, start, end
|
| 51 |
try:
|
| 52 |
+
chr_elem = fetch_tree.find(".//Gene-ref_maploc")
|
| 53 |
+
chromosome = chr_elem.text if chr_elem is not None else "Unknown"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
interval = fetch_tree.find(".//Seq-interval")
|
| 56 |
+
start = int(interval.find("Seq-interval_from").text) + 1
|
| 57 |
+
end = int(interval.find("Seq-interval_to").text) + 1
|
| 58 |
+
|
| 59 |
+
strand_elem = interval.find("Seq-interval_strand/Na-strand/value")
|
| 60 |
+
strand = strand_elem.text if strand_elem is not None else "unknown"
|
| 61 |
+
|
| 62 |
+
return {
|
| 63 |
+
"chromosome": chromosome,
|
| 64 |
+
"start": start,
|
| 65 |
+
"end": end,
|
| 66 |
+
"strand": strand
|
| 67 |
+
}
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return "Error parsing gene location:", str(e)
|
| 70 |
|
| 71 |
final_answer = FinalAnswerTool()
|
| 72 |
|
|
|
|
| 74 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 75 |
|
| 76 |
model = HfApiModel(
|
| 77 |
+
max_tokens=2096,
|
| 78 |
+
temperature=0.5,
|
| 79 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 80 |
+
custom_role_conversions=None,
|
| 81 |
)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
with open("prompts.yaml", 'r') as stream:
|
| 84 |
prompt_templates = yaml.safe_load(stream)
|
| 85 |
|
| 86 |
agent = CodeAgent(
|
| 87 |
model=model,
|
| 88 |
+
tools=[final_answer, get_gene_id, get_gene_location],
|
| 89 |
max_steps=6,
|
| 90 |
verbosity_level=1,
|
| 91 |
grammar=None,
|