| import gradio as gr |
| from smolagents import ( |
| CodeAgent, |
| DuckDuckGoSearchTool, |
| FinalAnswerTool, |
| InferenceClientModel, |
| Tool, |
| tool, |
| VisitWebpageTool |
| ) |
|
|
| |
| |
| |
|
|
| @tool |
| def suggest_menu(occasion: str) -> str: |
| """ |
| Suggère un menu de fête adapté en fonction de l'occasion. |
| Args: |
| occasion: Le type de fête (casual, formal, superhero). |
| """ |
| if occasion == "casual": |
| return "Pizza, collations et boissons fraîches." |
| elif occasion == "formal": |
| return "Dîner gastronomique 3 services." |
| elif occasion == "superhero": |
| return "Buffet ultra-protéiné et boissons saines." |
| else: |
| return "Choix personnalisé pour le majordome." |
|
|
| @tool |
| def catering_service_tool(query: str) -> str: |
| """ |
| Retourne le prestataire de traiteur le mieux noté de Gotham City. |
| Args: |
| query: Requête de recherche textuelle. |
| """ |
| services = { |
| "Gotham Catering Co.": 4.9, |
| "Wayne Manor Catering": 4.8, |
| "Gotham City Events": 4.7, |
| } |
| return max(services, key=services.get) |
|
|
| class SuperheroPartyThemeTool(Tool): |
| name = "superhero_party_theme_generator" |
| description = "Suggère des thèmes créatifs pour fêtes de super-héros selon une catégorie." |
| |
| inputs = { |
| "category": { |
| "type": "string", |
| "description": "Catégorie ('classic heroes', 'villain masquerade', 'futuristic Gotham').", |
| } |
| } |
| output_type = "string" |
|
|
| def forward(self, category: str): |
| themes = { |
| "classic heroes": "Gala Justice League : Costumes de héros DC et cocktails kryptoniques.", |
| "villain masquerade": "Bal des Voyous : Mascarade mystérieuse sur le thème des super-vilains de Gotham.", |
| "futuristic Gotham": "Nuit Neo-Gotham : Esthétique cyberpunk inspirée de Batman Beyond avec néons." |
| } |
| return themes.get(category.lower(), "Thème non trouvé. Options : 'classic heroes', 'villain masquerade', 'futuristic Gotham'.") |
|
|
| |
| |
| |
|
|
| |
| agent = CodeAgent( |
| tools=[ |
| DuckDuckGoSearchTool(), |
| VisitWebpageTool(), |
| suggest_menu, |
| catering_service_tool, |
| SuperheroPartyThemeTool(), |
| FinalAnswerTool() |
| ], |
| model=InferenceClientModel(), |
| max_steps=10, |
| verbosity_level=2 |
| ) |
|
|
| |
| |
| |
|
|
| def interact_with_agent(prompt): |
| try: |
| |
| response = agent.run(prompt) |
| return response |
| except Exception as e: |
| return f"Une erreur s'est produite : {str(e)}" |
|
|
| |
| demo = gr.Interface( |
| fn=interact_with_agent, |
| inputs=gr.Textbox(lines=5, placeholder="Ex: Trouve la playlist idéale pour une fête thématique 'mascarade de méchants' au manoir Wayne..."), |
| outputs=gr.Markdown(label="Réponse d'Alfred"), |
| title="🦇 Alfred, le CodeAgent du Manoir Wayne", |
| description="Posez une question à Alfred pour organiser la prochaine fête. Il peut chercher sur le web, suggérer des menus, des traiteurs et des thèmes !", |
| examples=[ |
| ["Prépare un menu formel pour la fête."], |
| ["Trouve le meilleur service traiteur de Gotham."], |
| ["Suggère un thème pour une fête 'classic heroes'."] |
| ] |
| ) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch() |