Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181911 unmodified lines3132333435363 unmodified lines404142434445464748495051524 unmodified lines57585960616263646566676869707172737475767778from smolagents import CodeAgent, InferenceClientModel, load_tool, toolimport datetimeimport pytzfrom tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUI@tooldef get_current_time_in_timezone(timezone: str) -> str:"""Get the current local time in the requested timezone.Args:timezone: IANA timezone name (e.g. 'Asia/Kolkata', 'America/New_York')Returns:Human-readable string with current time or error message"""try:tz = pytz.timezone(timezone)11 unmodified linesreturn f"Tool called → arg1 = {arg1!r} | arg2 = {arg2} → What would you like to build?"def main():final_answer_tool = FinalAnswerTool()3 unmodified linesmax_tokens=2048,)# Optional text-to-image toolimage_tool = Nonetry:image_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)print("Text-to-image tool loaded")except Exception as e:print(f"Text-to-image tool failed to load: {e}")# Tools listtools = [4 unmodified linesif image_tool is not None:tools.append(image_tool)# Create agent – using library defaults for promptsagent = CodeAgent(tools=tools,model=model,max_steps=8,verbosity_level=1,stream_outputs=True,# planning_interval=4, # optional – uncomment if desired# additional_authorized_imports=["math"], # only if your code needs it)# Launch UIGradioUI(agent).launch(server_name="0.0.0.0",server_port=7860,# share=True, # uncomment for public link (temporary))12345678910111213141516171819202111 unmodified lines333435363738394041423 unmodified lines464748495051525354555657585960616263646566676869707172737475767778794 unmodified lines84858687888990919293949596979899100101102103104105from smolagents import CodeAgent, InferenceClientModel, load_tool, toolimport datetimeimport pytzimport yamlfrom tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUI# ────────────────────────────────────────────────# Custom Tools# ────────────────────────────────────────────────@tooldef get_current_time_in_timezone(timezone: str) -> str:"""Get the current local time in the requested timezone.Args:timezone: IANA timezone name (e.g. 'Asia/Kolkata', 'America/New_York')"""try:tz = pytz.timezone(timezone)11 unmodified linesreturn f"Tool called → arg1 = {arg1!r} | arg2 = {arg2} → What would you like to build?"# ────────────────────────────────────────────────# Main# ────────────────────────────────────────────────def main():final_answer_tool = FinalAnswerTool()3 unmodified linesmax_tokens=2048,)# Optional text-to-image tool from Hubimage_tool = Nonetry:image_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)print("✅ Text-to-image tool loaded from Hub")except Exception as e:print(f"⚠️ Text-to-image tool failed to load: {e}")# ── Load your custom prompts.yaml + FIX the missing final_answer key ──prompt_templates = Nonetry:with open("prompts.yaml", encoding="utf-8") as f:prompt_templates = yaml.safe_load(f)print("✅ Custom prompt templates loaded")# CRITICAL FIX for latest smolagents (v1.17+)# The library now strictly requires the "final_answer" keyif prompt_templates and "final_answer" not in prompt_templates:prompt_templates["final_answer"] = {"pre_messages": "Here is the final answer:\n","post_messages": ""}print("✅ Added missing 'final_answer' template (required by latest smolagents)")except FileNotFoundError:print("⚠️ prompts.yaml not found → using default system prompt")except Exception as e:print(f"⚠️ Failed to load prompts.yaml: {e}")# Tools listtools = [4 unmodified linesif image_tool is not None:tools.append(image_tool)# Create the agentagent = CodeAgent(tools=tools,model=model,max_steps=10, # increased for better performanceverbosity_level=1,stream_outputs=True,# planning_interval=4, # uncomment if you want planning modeprompt_templates=prompt_templates, # now guaranteed to be valid)# Launch Gradio UI (perfect for HF Spaces / local)GradioUI(agent).launch(server_name="0.0.0.0",server_port=7860,# share=True, # uncomment for temporary public link)
app.py+39-12