import gradio as gr from services.search_service import FoodSearchService class GradioUI: def __init__(self): self.food_service = FoodSearchService() def search_food_products(self, query: str, max_results: int) -> str: """ Recherche des produits alimentaires dans la base de données OpenFoodFacts. Cette fonction effectue une recherche intelligente de produits alimentaires en analysant la requête utilisateur avec Mistral AI pour extraire le nom du produit et la marque, puis interroge la base DuckDB. Args: query (str): Terme de recherche saisi par l'utilisateur. Peut contenir un nom de produit, une marque, ou une combinaison. Exemples: "pepito", "biscuits lu", "pizza picard", "yaourt danone" max_results (int): Nombre maximum de résultats à retourner. Doit être compris entre 1 et 20. Returns: str: Résultats de recherche formatés en Markdown contenant : - Informations nutritionnelles (Nutri-Score, Eco-Score, NOVA) - Liste des ingrédients et additifs - Allergènes potentiels - Analyse détaillée par Mistral AI """ return self.food_service.search_and_format_products(query, max_results) def create_interface(self): custom_css = """ .footer { margin-top: 40px; padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 12px; text-align: center; color: white; } .footer h3 { margin: 0 0 10px 0; font-size: 18px; font-weight: 600; } .footer p { margin: 5px 0; font-size: 14px; opacity: 0.9; } .footer a { color: #ffd700; text-decoration: none; font-weight: 500; } .footer a:hover { text-decoration: underline; } """ with gr.Blocks(title="🤖 Healthify me", theme=gr.themes.Soft(), css=custom_css) as demo: # En-tête gr.Markdown(""" # 🤖 Healthify me """) with gr.Row(): with gr.Column(scale=3): search_input = gr.Textbox( label="🔍 Search for product", placeholder="Ex: pepito, biscuits lu, pizza picard, yaourt danone..." ) with gr.Column(scale=1): max_results = gr.Slider( minimum=1, maximum=20, value=5, step=1, label="📊 Number of results", info="How many results ?" ) search_btn = gr.Button("🤖 Search", variant="primary", size="lg") search_output = gr.Markdown(""" ### Welcome to Healthify me! This interface allows you to search for food products and obtain detailed nutritional information. **How to use:** - Enter the name of a product (e.g., "Nutella", "pizza Picard", "Fuzetea drink") - Select the number of results you want - Click "Search" **Available information:** - Nutri-Score and NOVA - Complete list of ingredients - Allergens present - Detailed nutritional values *Ready to start your search...* """) search_btn.click( fn=self.search_food_products, inputs=[search_input, max_results], outputs=[search_output] ) gr.HTML(""" """) return demo