vechism's picture
Update app.py
095eb03 verified
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
import datetime
import requests
import pytz
import yaml
import re
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI
# Below is an example of a tool that does nothing. Amaze us with your creativity !
@tool
def mutante_unicorn(animal_name:str) -> str: #it's import to specify the return type
#Keep this format for the description / args / args description but feel free to modify the tool
"""A tool that generates a close-up photograph of a mutant unicorn based on the provided animal name (e.g. tiger, duck, snake, etc).
The image features a mutant unicorn with characteristics of the specified animal, butterfly wings, and tiny paws holding a bright,
colorful orb. The unicorn is gazing with concentration at the center of the orb. The scene takes place in a
forest clearing illuminated by moonlight.
Args:
animal_name: A string representing the name of an animal (e.g., "tiger", "duck", "snake") whose features will be incorporated into the mutant unicorn.
"""
try:
# Import tool from Hub
#prompt = f"A close-up, highly detailed photograph of a mutant unicorn with {animal_name}-like torso. "\
# f"It has colorful butterfly wings spreading from its back and tiny paws holding a bright, "\
# f"glowing orb. The unicorn stares with concentration at the center of the orb. "\
# f"The scene takes place in a mystical forest clearing bathed in soft moonlight. "\
# f"Professional photography, 8k, hyper-realistic."
prompt = f"A close-up, highly detailed photograph of a mutant unicorn with {animal_name}-like features. "\
f"It displays majestic characteristics typical of a {animal_name}. "\
f"It has the body structure reminiscent of a {animal_name} but with a unicorn horn. "\
f"It has beautiful, iridescent butterfly wings spreading from its back and tiny, delicate paws "\
f"holding a small glowing orb that radiates with colorful light. "\
f"The creature is staring intensely at the center of the orb with a concentrated gaze. "\
f"The scene takes place in a magical forest clearing bathed in soft, ethereal moonlight. "\
f"The background shows silhouettes of trees and some glowing fireflies. "\
f"Photorealistic style, 8K resolution, professional photography."
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
image = image_generation_tool(prompt)
return final_answer(image)
except Exception as e:
return f"Error creating a mutant unicorn for '{animal_name}': {str(e)}"
#@tool
#def describe_animal(animal_name:str) -> str:
# """Look up for the name of the animal inwikipedia using websearch tool, and then provide a 300 characters description about that animal
# Args:
# animal_name: The name of the animal to look up for in wikipedia
# """
# query = f"{animal_name} wikipedia"
# wikipedia_results = DuckDuckGoSearchTool(query)
# return wikipedia_results
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:
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
# model_id = 'meta-llama/Llama-3.3-70B-Instruct',
# model_id = 'https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
custom_role_conversions=None,
)
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[final_answer, mutante_unicorn], # add your tools here (don't remove final answer)
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
# agent.run(
# '''This mutant unicorn has butterfly wings and is holding a small brightfull orb with its tiny paws, looking at the center
# of the orb with a concentrated gaze. The unicorn is in a clearing in the forest on a moonlit night.'''
#)
GradioUI(agent).launch()