Spaces:
Sleeping
Sleeping
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, toolimport datetimeimport requestsimport pytzimport yamlfrom tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUI# Tool personalizzato per ottenere l'ora attuale a Milano@tooldef get_current_time_milan() -> str:"""Restituisce l'ora attuale a Milano."""try:tz = pytz.timezone('Europe/Rome') # Milano è nella timezone di Romalocal_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")return f"L'ora attuale a Milano è: {local_time}"except Exception as e:return f"Errore nel recupero dell'ora per Milano: {str(e)}"final_answer = FinalAnswerTool()model = HfApiModel(max_tokens=2096,temperature=0.5,model_id='Qwen/Qwen2.5-Coder-32B-Instruct',custom_role_conversions=None,)# Importa lo strumento di generazione immagini dal tool 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)agent = CodeAgent(model=model,tools=[final_answer, get_current_time_milan], # Aggiunto il nuovo toolmax_steps=6,verbosity_level=1,grammar=None,planning_interval=None,name=None,description=None,prompt_templates=prompt_templates)GradioUI(agent).launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465da smolagents importa CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, strumentoimporta datetimeimporta richiesteimporta pytzimporta yamlda tools.final_answer importa FinalAnswerToolda Gradio_UI importa GradioUI# Di seguito è riportato un esempio di uno strumento che non fa nulla. Stupiscici con la tua creatività!def my_custom_tool ( arg1: str , arg2: int )-> str : # è importante specificare il tipo di ritorno# Mantieni questo formato per la descrizione dello strumento / descrizione degli argomenti ma sentiti libero di modificare lo strumento"""Uno strumento che non fa ancora nullaArgomenti:arg1: il primo argomentoarg2: il secondo argomento"""ritorna "Quale magia costruirai?"def get_current_time_in_timezone ( timezone: str ) -> str :"""Uno strumento che recupera l'ora locale corrente in un fuso orario specificato.Argomenti:fuso orario: una stringa che rappresenta un fuso orario valido (ad esempio, 'America/New_York')."""prova :# Crea oggetto fuso orariotz = pytz.timezone(fuso orario)# Ottieni l'ora corrente in quel fuso orariolocal_time = datetime.datetime.now(tz).strftime( "%Y-%m-%d %H:%M:%S" )return f"L'ora locale corrente in {timezone} è: {local_time} "except Exception as e:return f"Errore durante il recupero dell'ora per il fuso orario ' {timezone} ': { str (e)} "risposta_finale = StrumentoRispostaFinale()modello = HfApiModel(numero massimo di token = 2096 ,temperatura= 0,5 ,model_id= 'Qwen/Qwen2.5-Coder-32B-Istruzione' ,custom_role_conversions= Nessuno ,)# Strumento di importazione dall'hubimage_generation_tool = load_tool( "agents-course/text-to-image" , trust_remote_code= True )con open ( "prompts.yaml" , 'r' ) come flusso:prompt_templates = yaml.safe_load(flusso)agente = CodeAgent(modello=modello,tools=[final_answer], # aggiungi i tuoi strumenti qui (non rimuovere final_answer)max_steps= 6 ,livello_di_verbosità= 1 ,grammatica= Nessuna ,planning_interval= Nessuno ,nome= Nessuno ,descrizione= Nessuno ,prompt_templates=template_prompt)GradioUI(agente).launch()
app.py+56-38