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+
import requests
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Initialize translation models
|
| 6 |
+
translate_to_english = pipeline("translation", model="Helsinki-NLP/opus-mt-et-en")
|
| 7 |
+
translate_to_estonian = pipeline("translation", model="Helsinki-NLP/opus-mt-en-et")
|
| 8 |
+
|
| 9 |
+
# Spoonacular API settings
|
| 10 |
+
API_KEY = '0bf3de98d2654cb28f139df9fedb7db2'
|
| 11 |
+
SPOONACULAR_URL = "https://api.spoonacular.com/recipes/findByIngredients"
|
| 12 |
+
RECIPE_INFO_URL = "https://api.spoonacular.com/recipes/{}/information"
|
| 13 |
+
RECIPE_SEARCH_URL = "https://api.spoonacular.com/recipes/complexSearch"
|
| 14 |
+
|
| 15 |
+
def translate_text(input_text, direction='et-en'):
|
| 16 |
+
"""Translate text between Estonian and English."""
|
| 17 |
+
if direction == 'et-en':
|
| 18 |
+
result = translate_to_english(input_text)
|
| 19 |
+
else:
|
| 20 |
+
result = translate_to_estonian(input_text)
|
| 21 |
+
return result[0]['translation_text']
|
| 22 |
+
|
| 23 |
+
def find_recipes(ingredient, calories, gluten_content):
|
| 24 |
+
"""Find recipes based on the ingredient and dietary preferences."""
|
| 25 |
+
# Translate ingredient to English
|
| 26 |
+
ingredient_en = translate_text(ingredient, direction='et-en')
|
| 27 |
+
|
| 28 |
+
# Map Estonian options to English for API
|
| 29 |
+
calories_map = {
|
| 30 |
+
"Madal kalorsus": "Low",
|
| 31 |
+
"Kõrge kalorsus": "High"
|
| 32 |
+
}
|
| 33 |
+
gluten_map = {
|
| 34 |
+
"Sisaldab gluteeni": "Contains",
|
| 35 |
+
"Gluteenivaba": "Free of"
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# Search for recipes that include the specific ingredient
|
| 39 |
+
search_params = {
|
| 40 |
+
"apiKey": API_KEY,
|
| 41 |
+
"query": ingredient_en,
|
| 42 |
+
"includeIngredients": ingredient_en,
|
| 43 |
+
"number": 5, # Fetch multiple recipes to increase chances
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
# Add diet filters
|
| 47 |
+
if gluten_content == "Gluteenivaba":
|
| 48 |
+
search_params["intolerances"] = "Gluten"
|
| 49 |
+
|
| 50 |
+
response = requests.get(RECIPE_SEARCH_URL, params=search_params)
|
| 51 |
+
|
| 52 |
+
if response.status_code == 200:
|
| 53 |
+
recipes = response.json().get('results', [])
|
| 54 |
+
|
| 55 |
+
if not recipes:
|
| 56 |
+
return "Ühtegi retsepti ei leitud.", ""
|
| 57 |
+
|
| 58 |
+
# Try to find a suitable recipe
|
| 59 |
+
for recipe in recipes:
|
| 60 |
+
# Get detailed recipe information
|
| 61 |
+
recipe_info_response = requests.get(
|
| 62 |
+
RECIPE_INFO_URL.format(recipe['id']),
|
| 63 |
+
params={
|
| 64 |
+
"apiKey": API_KEY,
|
| 65 |
+
"includeNutrition": True
|
| 66 |
+
}
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
if recipe_info_response.status_code == 200:
|
| 70 |
+
recipe_info = recipe_info_response.json()
|
| 71 |
+
|
| 72 |
+
# Verify ingredient is in the recipe
|
| 73 |
+
ingredients_list = recipe_info.get('extendedIngredients', [])
|
| 74 |
+
ingredient_names = [ing['name'].lower() for ing in ingredients_list]
|
| 75 |
+
|
| 76 |
+
# Check if the main ingredient is in the recipe
|
| 77 |
+
if any(ingredient_en.lower() in name for name in ingredient_names):
|
| 78 |
+
# Translate recipe title
|
| 79 |
+
recipe_name_en = recipe_info['title']
|
| 80 |
+
recipe_name_et = translate_text(recipe_name_en, direction='en-et')
|
| 81 |
+
|
| 82 |
+
# Create recipe link
|
| 83 |
+
recipe_link = recipe_info.get('sourceUrl', 'Link pole saadaval')
|
| 84 |
+
|
| 85 |
+
# Translate ingredients to Estonian
|
| 86 |
+
ingredients_list = [f"- {ingredient['original']}" for ingredient in ingredients_list]
|
| 87 |
+
ingredients_en = '\n'.join(ingredients_list)
|
| 88 |
+
ingredients_et = translate_text(ingredients_en, direction='en-et')
|
| 89 |
+
|
| 90 |
+
# Translate instructions
|
| 91 |
+
instructions_en = recipe_info.get('instructions', 'Instructions not available')
|
| 92 |
+
instructions_et = translate_text(instructions_en, direction='en-et')
|
| 93 |
+
|
| 94 |
+
# Calculate calories per serving
|
| 95 |
+
nutrition = recipe_info.get('nutrition', {})
|
| 96 |
+
calories_per_serving = nutrition.get('calories', 'N/A')
|
| 97 |
+
|
| 98 |
+
# Create a detailed recipe description
|
| 99 |
+
recipe_description = (
|
| 100 |
+
f"🍽️ RETSEPT: {recipe_name_et}\n\n"
|
| 101 |
+
f"🥘 Põhikoostisosa: {ingredient}\n\n"
|
| 102 |
+
f"⏲️ Valmistamisaeg: {recipe_info.get('readyInMinutes', 'N/A')} minutit\n"
|
| 103 |
+
f"👥 Portsjonite arv: {recipe_info.get('servings', 'N/A')}\n\n"
|
| 104 |
+
f"📝 KOOSTISOSAD:\n{ingredients_et}\n\n"
|
| 105 |
+
f"👩🍳 VALMISTAMISE JUHISED:\n{instructions_et}\n\n"
|
| 106 |
+
f"ℹ️ LISAINFO:\n"
|
| 107 |
+
f"Kalorsus: {calories}\n"
|
| 108 |
+
f"Gluteenisisaldus: {gluten_content}\n"
|
| 109 |
+
f"Kalorid portsjoni kohta: {calories_per_serving}\n\n"
|
| 110 |
+
f"🔗 Originaalretsept: {recipe_link}"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
return recipe_description, recipe_link
|
| 114 |
+
|
| 115 |
+
# If no recipe found with the exact ingredient
|
| 116 |
+
return "Kahjuks ei leitud retsepti täpse koostisosaga.", ""
|
| 117 |
+
else:
|
| 118 |
+
return "Viga retseptide toomisel.", ""
|
| 119 |
+
|
| 120 |
+
# Create the Gradio interface with explicit choices
|
| 121 |
+
iface = gr.Interface(
|
| 122 |
+
fn=find_recipes,
|
| 123 |
+
inputs=[
|
| 124 |
+
gr.Textbox(
|
| 125 |
+
lines=1,
|
| 126 |
+
placeholder="Sisestage koostisosa (nt lõhe, šokolaad)",
|
| 127 |
+
label="Koostisosa"
|
| 128 |
+
),
|
| 129 |
+
gr.Dropdown(
|
| 130 |
+
choices=["Madal kalorsus", "Kõrge kalorsus"],
|
| 131 |
+
label="Kalorsus",
|
| 132 |
+
type="value"
|
| 133 |
+
),
|
| 134 |
+
gr.Dropdown(
|
| 135 |
+
choices=["Sisaldab gluteeni", "Gluteenivaba"],
|
| 136 |
+
label="Gluteenisisaldus",
|
| 137 |
+
type="value"
|
| 138 |
+
)
|
| 139 |
+
],
|
| 140 |
+
outputs=[
|
| 141 |
+
gr.Textbox(lines=20, label="Genereeritud Retsept"),
|
| 142 |
+
gr.Textbox(lines=1, label="Retsepti Link")
|
| 143 |
+
],
|
| 144 |
+
title="🍳 Nutikas Retsepti Generaator",
|
| 145 |
+
description=(
|
| 146 |
+
"Sisestage koostisosa, valige kalorsus ja gluteenisisaldus. "
|
| 147 |
+
"Saate personaalse retsepti!"
|
| 148 |
+
),
|
| 149 |
+
examples=[
|
| 150 |
+
["lõhe", "Madal kalorsus", "Gluteenivaba"],
|
| 151 |
+
["šokolaadikook", "Kõrge kalorsus", "Sisaldab gluteeni"],
|
| 152 |
+
["kartul", "Madal kalorsus", "Gluteenivaba"]
|
| 153 |
+
],
|
| 154 |
+
theme="default"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# Launch the interface
|
| 158 |
+
if __name__ == "__main__":
|
| 159 |
+
iface.launch()
|