Spaces:
Runtime error
Runtime error
File size: 3,042 Bytes
293635c 9b5b26a c19d193 6aae614 9b5b26a 293635c 9b5b26a 293635c 9b5b26a 293635c 9b5b26a 293635c 9b5b26a 293635c 9b5b26a 293635c 9b5b26a 8c01ffb 293635c e121372 293635c 13d500a 8c01ffb 293635c 861422e 293635c 8c01ffb 8fe992b 293635c 8c01ffb 293635c 861422e 8fe992b 293635c |
1 2 3 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
# Medical tool: Fetch research papers from PubMed
@tool
def get_pubmed_articles(query: str) -> str:
"""Fetches the latest research papers from PubMed based on a query.
Args:
query: The medical or biological topic to search for.
"""
url = f"https://api.ncbi.nlm.nih.gov/lit/ctxp/v1/pubmed/?format=summary&term={query}"
response = requests.get(url)
if response.status_code == 200:
return response.json() # Return article summary
else:
return "Failed to fetch articles."
# Medical tool: Explain medical terminology
@tool
def explain_medical_term(term: str) -> str:
"""Provides a simple explanation of a medical term.
Args:
term: The medical term to explain.
"""
explanations = {
"hypertension": "Hypertension, or high blood pressure, is a condition where the force of blood against the artery walls is too high.",
"diabetes": "Diabetes is a chronic disease where the body either doesn't produce enough insulin or can't use it effectively.",
"anemia": "Anemia is a condition in which the blood lacks enough healthy red blood cells to carry oxygen.",
}
return explanations.get(term.lower(), "No definition found. Try a different term.")
# Tool for fetching current time in a timezone
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""Fetches the current local time in a specified timezone.
Args:
timezone: A string representing a valid timezone (e.g., 'America/New_York').
"""
try:
tz = pytz.timezone(timezone)
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"The current local time in {timezone} is: {local_time}"
except Exception as e:
return f"Error fetching time for timezone '{timezone}': {str(e)}"
# Load the medical AI model (BioBERT for medical text processing)
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='dmis-lab/biobert-v1.1', # BioBERT for medical NLP tasks
custom_role_conversions=None,
)
# Load prompt templates
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
# Define the AI agent with medical expertise
agent = CodeAgent(
model=model,
tools=[FinalAnswerTool(), get_pubmed_articles, explain_medical_term, get_current_time_in_timezone],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name="Medical Bio Expert",
description="An AI agent specialized in medical biology, capable of answering biomedical questions, retrieving PubMed articles, and explaining medical terms.",
prompt_templates=prompt_templates
)
# Launch the Gradio UI for interaction
GradioUI(agent, title="Medical Biology AI Expert", description="Ask me anything about medical biology!").launch() |