Spaces:
Sleeping
Sleeping
Update app.py
Browse files40 unmodified lines414243444546474849505152535455565758596061626364659 unmodified lines75767759606162636465666768697071727879806 unmodified lines87888985869091929394959697989910010110210310410588899010610710810911011111297113991141151161172 unmodified lines12012112210812340 unmodified lines return f"Error fetching time for timezone '{timezone}': {str(e)}"
@tooldef fetch_url_text(url: str) -> str: """Fetches readable text snippet from a given URL. Args: url: The HTTP/HTTPS URL to fetch. """ try: response = requests.get( url, timeout=15, headers={"User-Agent": "Mozilla/5.0"}, ) response.raise_for_status() text = response.text return text[:4000] except Exception as e: return f"Error fetching URL '{url}': {str(e)}"
final_answer = FinalAnswerTool()
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:9 unmodified lines
litellm.drop_params = True
gemini_key = (os.getenv("GEMINI_API_KEY") or "").strip(" '\"\n\r\t") or Noneif gemini_key: os.environ["GEMINI_API_KEY"] = gemini_key
moonshot_key = (os.getenv("MOONSHOT_API_KEY") or "").strip(" '\"\n\r\t") or Noneif moonshot_key: os.environ["MOONSHOT_API_KEY"] = moonshot_key
import litellmfrom smolagents import LiteLLMModel
litellm.drop_params = True
openrouter_key = (os.getenv("OPENROUTER_API_KEY") or "").strip(" '\"\n\r\t") or Noneif openrouter_key: os.environ["OPENROUTER_API_KEY"] = openrouter_key6 unmodified lines timeout=30,)
# Import tool from Hubimage_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)with open("prompts.yaml", "r") as stream: prompt_templates = yaml.safe_load(stream) or {}
exact_match_rule = ( "\n\nCRITICAL OUTPUT RULES FOR EXACT MATCH EVALUATION:\n" "- When calling final_answer, pass ONLY the concise exact answer value itself " "(e.g. a number, date, name, or short phrase).\n" "- NEVER include introductory text, explanations, markdown formatting, or the phrase 'FINAL ANSWER'.\n" "- Do not wrap the answer in a dictionary like {'answer': ...}; pass the plain answer string directly.")
if isinstance(prompt_templates, dict) and "system_prompt" in prompt_templates: prompt_templates["system_prompt"] = str(prompt_templates["system_prompt"]) + exact_match_ruleelse: prompt_templates = {"system_prompt": exact_match_rule.strip()}
with open("prompts.yaml", 'r') as stream: prompt_templates = yaml.safe_load(stream) agent = CodeAgent( model=model, tools=[ final_answer, DuckDuckGoSearchTool(), fetch_url_text, get_current_time_in_timezone, image_generation_tool, ], max_steps=6, max_steps=10, verbosity_level=1, grammar=None, planning_interval=None,2 unmodified lines prompt_templates=prompt_templates,)
GradioUI(agent).launch()app.py+36-22