File size: 5,037 Bytes
69881ac c2685c2 69881ac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
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()
|