Spaces:
Runtime error
Runtime error
File size: 4,163 Bytes
27df5c3 9b5b26a 27df5c3 9b5b26a c19d193 8fe992b 27df5c3 6abf713 27df5c3 ed38c0d 27df5c3 9b5b26a 27df5c3 9b5b26a 27df5c3 9b5b26a 27df5c3 9b5b26a 27df5c3 9b5b26a 27df5c3 9b5b26a 27df5c3 8c01ffb 27df5c3 8c01ffb 6aae614 ae7a494 27df5c3 13d500a 8c01ffb 27df5c3 8c01ffb 27df5c3 861422e 27df5c3 6abf713 27df5c3 6abf713 27df5c3 8c01ffb 8fe992b 27df5c3 8c01ffb 27df5c3 8c01ffb 882b4c9 27df5c3 8fe992b 9b5b26a 27df5c3 e515b2e 27df5c3 | 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
import datetime
from pathlib import Path
import pytz
import yaml
from smolagents import (
CodeAgent,
DuckDuckGoSearchTool,
HfApiModel,
load_tool,
tool,
)
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
# ============================================================
# 1. CUSTOM TOOL: HVAC AIRFLOW UNIT CONVERSION
# ============================================================
@tool
def convert_airflow_units(airflow_m3h: float) -> str:
"""
Convert HVAC airflow from cubic metres per hour into
litres per second and cubic feet per minute.
Args:
airflow_m3h: Airflow in cubic metres per hour.
"""
if airflow_m3h < 0:
return "Error: Airflow cannot be negative."
airflow_ls = airflow_m3h / 3.6
airflow_cfm = airflow_m3h * 0.588578
return (
f"Airflow: {airflow_m3h:.2f} m3/h\n"
f"Airflow: {airflow_ls:.2f} L/s\n"
f"Airflow: {airflow_cfm:.2f} CFM"
)
# ============================================================
# 2. CUSTOM TOOL: CURRENT TIME IN A TIME ZONE
# ============================================================
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""
Get the current local date and time in a specified timezone.
Args:
timezone: Valid timezone, e.g. Asia/Riyadh or America/New_York.
"""
try:
tz = pytz.timezone(timezone)
local_time = datetime.datetime.now(tz)
return (
f"Current time in {timezone}: "
f"{local_time.strftime('%Y-%m-%d %H:%M:%S')}"
)
except pytz.exceptions.UnknownTimeZoneError:
return f"Error: Unknown timezone '{timezone}'."
# ============================================================
# 3. LOAD FINAL ANSWER TOOL
# ============================================================
final_answer = FinalAnswerTool()
# ============================================================
# 4. LOAD WEB SEARCH TOOL
# ============================================================
search_tool = DuckDuckGoSearchTool(max_results=5)
# ============================================================
# 5. LOAD IMAGE GENERATION TOOL
# ============================================================
# This tool comes from the Hugging Face Hub.
# Only load it if you trust the repository's code.
image_generation_tool = load_tool(
"agents-course/text-to-image",
trust_remote_code=True,
)
# ============================================================
# 6. LOAD PROMPT TEMPLATES
# ============================================================
BASE_DIR = Path(__file__).resolve().parent
PROMPTS_FILE = BASE_DIR / "prompts.yaml"
with PROMPTS_FILE.open("r", encoding="utf-8") as stream:
prompt_templates = yaml.safe_load(stream)
if not isinstance(prompt_templates, dict):
raise ValueError("prompts.yaml must contain a YAML dictionary.")
# ============================================================
# 7. INITIALIZE HUGGING FACE MODEL
# ============================================================
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
)
# ============================================================
# 8. CREATE THE AI AGENT
# ============================================================
agent = CodeAgent(
model=model,
tools=[
final_answer,
search_tool,
convert_airflow_units,
get_current_time_in_timezone,
image_generation_tool,
],
max_steps=8,
verbosity_level=1,
planning_interval=None,
name="pdf_engineer_assistant",
description=(
"An AI assistant that searches the web, "
"converts HVAC airflow units, checks time zones, "
"and generates images."
),
prompt_templates=prompt_templates,
)
# ============================================================
# 9. LAUNCH GRADIO INTERFACE
# ============================================================
if __name__ == "__main__":
GradioUI(agent).launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
)
|