Spaces:
Sleeping
Sleeping
File size: 2,232 Bytes
a4fcb76 12f6ebf 5d62e8c a4fcb76 3b2e549 a4fcb76 6aae614 9fb9c7f f69d477 a4fcb76 ecc27e8 9b5b26a a4fcb76 eb04e0a 5d62e8c a4fcb76 8c01ffb a4fcb76 6aae614 9fb9c7f f69d477 235c220 a4fcb76 e121372 a4fcb76 13d500a 8c01ffb a4fcb76 9b5b26a 8c01ffb a4fcb76 861422e a4fcb76 8c01ffb 8fe992b 84e766b 9fb9c7f f69d477 f145669 80c9281 84e766b 8c01ffb a4fcb76 8fe992b a4fcb76 | 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 | from __future__ import annotations
import os
import shutil
from pathlib import Path
import gradio as gr
import yaml
from smolagents import CodeAgent, HfApiModel, load_tool
from tools.final_answer import FinalAnswerTool
from tools.web_search import DuckDuckGoSearchTool
from tools.visit_webpage import VisitWebpageTool
from tools.disk_free import disk_free
from tools.timezone_time import get_current_time_in_timezone
from src.first_agent.ui import GradioUI
print("Gradio version:", gr.__version__)
# --- Load .env locally (HF Spaces will use Settings → Secrets instead) ---
try:
from dotenv import load_dotenv
load_dotenv()
except Exception:
pass
# --- Required env ---
if not os.getenv("HF_TOKEN"):
raise RuntimeError(
"HF_TOKEN is not set. "
"Create a .env file locally or configure it in Hugging Face Spaces → Settings → Secrets."
)
# --- Runtime output directory setup ---
BASE_DIR = Path.cwd()
OUTPUT_DIR = BASE_DIR / "outputs" / "final_answers"
if OUTPUT_DIR.exists():
shutil.rmtree(OUTPUT_DIR)
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
# Make FinalAnswerTool write into the same folder
os.environ["FINAL_ANSWER_DIR"] = str(OUTPUT_DIR)
# --- Tools ---
final_answer = FinalAnswerTool()
web_search = DuckDuckGoSearchTool(max_results=10)
visit_webpage = VisitWebpageTool()
# --- Model ---
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id="Qwen/Qwen2.5-Coder-32B-Instruct", # may be overloaded sometimes
custom_role_conversions=None,
)
# Tool from Hub
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
# Prompts
with open("prompts.yaml", "r", encoding="utf-8") as stream:
prompt_templates = yaml.safe_load(stream)
# --- Agent ---
agent = CodeAgent(
model=model,
tools=[
final_answer,
get_current_time_in_timezone,
web_search,
visit_webpage,
image_generation_tool,
disk_free,
],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates,
)
# On HF Spaces, share=True is not supported; force share=False
GradioUI(agent).launch()
|