import gradio as gr import requests from transformers import pipeline # Initialize translation models translate_to_english = pipeline("translation", model="Helsinki-NLP/opus-mt-et-en") translate_to_estonian = pipeline("translation", model="Helsinki-NLP/opus-mt-en-et") # Spoonacular API settings API_KEY = '0bf3de98d2654cb28f139df9fedb7db2' SPOONACULAR_URL = "https://api.spoonacular.com/recipes/findByIngredients" RECIPE_INFO_URL = "https://api.spoonacular.com/recipes/{}/information" def translate_text(input_text, direction='et-en'): """Translate text between Estonian and English.""" if direction == 'et-en': result = translate_to_english(input_text) else: result = translate_to_estonian(input_text) return result[0]['translation_text'] def find_recipes(ingredient, calories, gluten_content): """Find recipes based on the ingredient and dietary preferences.""" # Translate ingredient to English ingredient_en = translate_text(ingredient, direction='et-en') # Map Estonian options to English for API calories_map = { "Madal kalorsus": "Low", "Kõrge kalorsus": "High" } gluten_map = { "Sisaldab gluteeni": "Contains", "Gluteenivaba": "Free of" } calories_en = calories_map.get(calories, "") gluten_en = gluten_map.get(gluten_content, "") params = { "ingredients": ingredient_en, "apiKey": API_KEY, "number": 1 # Get one recipe } response = requests.get(SPOONACULAR_URL, params=params) if response.status_code == 200: recipes = response.json() if not recipes: return "Ühtegi retsepti ei leitud.", "" recipe = recipes[0] recipe_id = recipe['id'] # Get detailed recipe information recipe_info_response = requests.get( RECIPE_INFO_URL.format(recipe_id), params={"apiKey": API_KEY} ) if recipe_info_response.status_code == 200: recipe_info = recipe_info_response.json() # Translate recipe title recipe_name_en = recipe['title'] recipe_name_et = translate_text(recipe_name_en, direction='en-et') # Create recipe link recipe_link = recipe_info.get('sourceUrl', 'Link pole saadaval') # Translate ingredients to Estonian ingredients_list = [f"- {ingredient['original']}" for ingredient in recipe_info.get('extendedIngredients', [])] ingredients_en = '\n'.join(ingredients_list) ingredients_et = translate_text(ingredients_en, direction='en-et') # Translate instructions instructions_en = recipe_info.get('instructions', 'Instructions not available') instructions_et = translate_text(instructions_en, direction='en-et') # Create a detailed recipe description recipe_description = ( f"🍽️ RETSEPT: {recipe_name_et}\n\n" f"⏲️ Valmistamisaeg: {recipe_info.get('readyInMinutes', 'N/A')} minutit\n" f"👥 Portsjonite arv: {recipe_info.get('servings', 'N/A')}\n\n" f"📝 KOOSTISOSAD:\n{ingredients_et}\n\n" f"👩‍🍳 VALMISTAMISE JUHISED:\n{instructions_et}\n\n" f"ℹ️ LISAINFO:\n" f"Kalorsus: {calories}\n" f"Gluteenisisaldus: {gluten_content}\n" f"Kalorid portsjoni kohta: {recipe_info.get('nutrition', {}).get('calories', 'N/A')}\n\n" ) return recipe_description, recipe_link else: return "Viga retsepti detailide toomisel.", "" else: return "Viga retseptide toomisel.", "" # Create the Gradio interface with explicit choices iface = gr.Interface( fn=find_recipes, inputs=[ gr.Textbox( lines=1, placeholder="Sisestage koostisosa (nt lõhe, šokolaad)", label="Koostisosa" ), gr.Dropdown( choices=["Madal kalorsus", "Kõrge kalorsus"], label="Kalorsus", type="value" ), gr.Dropdown( choices=["Sisaldab gluteeni", "Gluteenivaba"], label="Gluteenisisaldus", type="value" ) ], outputs=[ gr.Textbox(lines=20, label="Genereeritud Retsept"), gr.Textbox(lines=1, label="Retsepti Link") ], title="🍳 Nutikas Retsepti Generaator", description=( "Sisestage koostisosa, valige kalorsus ja gluteenisisaldus. " "Saate personaalse retsepti!" ), examples=[ ["lõhe", "Madal kalorsus", "Gluteenivaba"], ["šokolaadikook", "Kõrge kalorsus", "Sisaldab gluteeni"], ["kartul", "Madal kalorsus", "Gluteenivaba"] ], theme="default" ) # Launch the interface if __name__ == "__main__": iface.launch()