Spaces:
Build error
Build error
| import yaml | |
| import os | |
| import gradio as gr | |
| from huggingface_hub import login | |
| from smolagents import tool, CodeAgent, InferenceClientModel | |
| from typing import Dict, List | |
| # Activity from the second part of the Hugging Face Smolagents course | |
| # student BettoEsteves | |
| CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| # Fixed calculation settings (can be adjusted) | |
| MEAT_PER_PERSON = { | |
| "Picanha": 0.3, # grams per person | |
| "Ribs": 0.2, | |
| "Sausage": 0.1, | |
| "Chicken": 0.2, | |
| "Garlic bread": 0.1 | |
| } | |
| DRINKS_PER_PERSON = { | |
| "Beer": 3, # cans | |
| "Soda": 1, # 2L bottles | |
| "Water": 1, # 500ml bottles | |
| "Juice": 0.5 # liters | |
| } | |
| def calcular_carnes(persons: int) -> float: | |
| """ | |
| Calculates the amount of meat needed for the barbecue. | |
| Args: | |
| persons (int): Number of people attending the barbecue. | |
| Returns: | |
| float: Total amount of meat in kg. | |
| """ | |
| return sum(MEAT_PER_PERSON.values()) * persons | |
| def calcular_bebidas(persons: int) -> Dict[str, int]: | |
| """ | |
| Calculates the amount of drinks for the given number of people. | |
| Args: | |
| persons (int): Number of people attending the barbecue. | |
| Returns: | |
| Dict[str, int]: Amount of each drink needed. | |
| """ | |
| return {drink: round(qt * persons) for drink, qt in DRINKS_PER_PERSON.items()} | |
| def sugerir_playlist(persons: int) -> List[str]: | |
| """ | |
| Suggests a barbecue playlist based on the number of people. | |
| Args: | |
| persons (int): Number of people attending the barbecue. | |
| Returns: | |
| List[str]: Suggested list of songs. | |
| """ | |
| try: | |
| from ddgs import DDGS | |
| query = "Brazilian barbecue songs samba pagode mpb funk sertanejo" | |
| with DDGS() as ddgs: | |
| resultados = [r['title'] for r in ddgs.text(query, max_results=10) if 'title' in r] | |
| if resultados: | |
| return resultados | |
| except Exception: | |
| pass | |
| return [ | |
| "Cheia de Manias – Raça Negra", | |
| "Que Se Chama Amor – Só Pra Contrariar.", | |
| "Pimpolho – Art Popular.", | |
| "Deixa Acontecer – Grupo Revelação,", | |
| "Derê – Belo", | |
| "O Show Tem Que Continuar – Grupo Fundo de Quintal.", | |
| "Foi Um Rio Que Passou Em Minha Vida – Paulinho da Viola.", | |
| "Verdade – Zeca Pagodinho", | |
| "É Tarde Demais – Grupo Raça.", | |
| "Fio de Cabelo - Bruno & Marrone" | |
| ] | |
| model = InferenceClientModel( | |
| model_id='Qwen/Qwen2.5-Coder-32B-Instruct', | |
| provider=None, | |
| ) | |
| with open(os.path.join(CURRENT_DIR, "prompts.yaml"), 'r') as stream: | |
| prompt_templates = yaml.safe_load(stream) | |
| token = os.getenv("HUGGINGFACE_TOKEN") | |
| if token: | |
| login(token=token) | |
| agent = CodeAgent( | |
| model=model, | |
| tools=[calcular_bebidas, calcular_carnes, sugerir_playlist], | |
| managed_agents=[], | |
| max_steps=10, | |
| verbosity_level=2, | |
| grammar=None, | |
| planning_interval=None, | |
| name=None, | |
| description=None, | |
| prompt_templates=prompt_templates | |
| ) | |
| def greet(user_input, history=[]): | |
| # Se for a primeira pergunta, espera um número de pessoas | |
| if not history: | |
| try: | |
| persons = int(user_input) | |
| if persons <= 0: | |
| return [(user_input, "❌ Please enter a number greater than zero.")] | |
| carnes_total = calcular_carnes(persons) | |
| bebidas = calcular_bebidas(persons) | |
| playlist = sugerir_playlist(persons) | |
| # Lista de carnes detalhada | |
| carnes_lista = [f"🥩 {meat}: {qt*persons:.2f} kg" for meat, qt in MEAT_PER_PERSON.items()] | |
| carnes_str = '\n'.join(carnes_lista) | |
| # Lista de bebidas detalhada | |
| bebidas_lista = [f"🍺 {drink}: {qt}" if drink=="Beer" else f"🥤 {drink}: {qt}" for drink, qt in bebidas.items()] | |
| bebidas_str = '\n'.join(bebidas_lista) | |
| # Playlist formatada | |
| playlist_formatada = '\n'.join([f"🎶 {song}" for song in playlist]) | |
| resposta = ( | |
| f"🛒 Shopping List for {persons} people:\n" | |
| f"{carnes_str}\n" | |
| f"{bebidas_str}\n\n" | |
| f"Total Meat: {carnes_total:.2f} kg\n\n" | |
| f"🎵 Playlist:\n{playlist_formatada}" | |
| ) | |
| history.append((user_input, resposta)) | |
| return history | |
| except ValueError: | |
| return [(user_input, "❌ Please enter a valid number of people.")] | |
| # Para perguntas subsequentes, chama o agente | |
| resposta = agent.run(user_input) | |
| history.append((user_input, resposta)) | |
| return history | |
| demo = gr.Interface( | |
| fn=greet, | |
| inputs=gr.Textbox(label="How many people for the barbecue?"), | |
| outputs=gr.Chatbot(label="Conversation History"), | |
| title="Barbecue Agent", | |
| description="Enter the number of people and get a shopping list and playlist!" | |
| ) | |
| demo.launch() | |
| #if __name__ == "__main__": | |
| # main() | |