SHERVIOR's picture
Update app.py
293635c verified
raw
history blame contribute delete
3.04 kB
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()