Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
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 |
+
|
| 14 |
+
def translate_text(input_text, direction='et-en'):
|
| 15 |
+
"""Translate text between Estonian and English."""
|
| 16 |
+
if direction == 'et-en':
|
| 17 |
+
result = translate_to_english(input_text)
|
| 18 |
+
else:
|
| 19 |
+
result = translate_to_estonian(input_text)
|
| 20 |
+
return result[0]['translation_text']
|
| 21 |
+
|
| 22 |
+
def find_recipes(ingredient, calories, gluten_content):
|
| 23 |
+
"""Find recipes based on the ingredient and dietary preferences."""
|
| 24 |
+
# Translate ingredient to English
|
| 25 |
+
ingredient_en = translate_text(ingredient, direction='et-en')
|
| 26 |
+
|
| 27 |
+
# Map Estonian options to English for API
|
| 28 |
+
calories_map = {
|
| 29 |
+
"Madal kalorsus": "Low",
|
| 30 |
+
"Kõrge kalorsus": "High"
|
| 31 |
+
}
|
| 32 |
+
gluten_map = {
|
| 33 |
+
"Sisaldab gluteeni": "Contains",
|
| 34 |
+
"Gluteenivaba": "Free of"
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
calories_en = calories_map.get(calories, "")
|
| 38 |
+
gluten_en = gluten_map.get(gluten_content, "")
|
| 39 |
+
|
| 40 |
+
params = {
|
| 41 |
+
"ingredients": ingredient_en,
|
| 42 |
+
"apiKey": API_KEY,
|
| 43 |
+
"number": 1 # Get one recipe
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
response = requests.get(SPOONACULAR_URL, params=params)
|
| 47 |
+
|
| 48 |
+
if response.status_code == 200:
|
| 49 |
+
recipes = response.json()
|
| 50 |
+
|
| 51 |
+
if not recipes:
|
| 52 |
+
return "Ühtegi retsepti ei leitud.", ""
|
| 53 |
+
|
| 54 |
+
recipe = recipes[0]
|
| 55 |
+
recipe_id = recipe['id']
|
| 56 |
+
|
| 57 |
+
# Get detailed recipe information
|
| 58 |
+
recipe_info_response = requests.get(
|
| 59 |
+
RECIPE_INFO_URL.format(recipe_id),
|
| 60 |
+
params={"apiKey": API_KEY}
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
if recipe_info_response.status_code == 200:
|
| 64 |
+
recipe_info = recipe_info_response.json()
|
| 65 |
+
|
| 66 |
+
# Translate recipe title
|
| 67 |
+
recipe_name_en = recipe['title']
|
| 68 |
+
recipe_name_et = translate_text(recipe_name_en, direction='en-et')
|
| 69 |
+
|
| 70 |
+
# Create recipe link
|
| 71 |
+
recipe_link = recipe_info.get('sourceUrl', 'Link pole saadaval')
|
| 72 |
+
|
| 73 |
+
# Translate ingredients to Estonian
|
| 74 |
+
ingredients_list = [f"- {ingredient['original']}" for ingredient in recipe_info.get('extendedIngredients', [])]
|
| 75 |
+
ingredients_en = '\n'.join(ingredients_list)
|
| 76 |
+
ingredients_et = translate_text(ingredients_en, direction='en-et')
|
| 77 |
+
|
| 78 |
+
# Translate instructions
|
| 79 |
+
instructions_en = recipe_info.get('instructions', 'Instructions not available')
|
| 80 |
+
instructions_et = translate_text(instructions_en, direction='en-et')
|
| 81 |
+
|
| 82 |
+
# Create a detailed recipe description
|
| 83 |
+
recipe_description = (
|
| 84 |
+
f"🍽️ RETSEPT: {recipe_name_et}\n\n"
|
| 85 |
+
f"⏲️ Valmistamisaeg: {recipe_info.get('readyInMinutes', 'N/A')} minutit\n"
|
| 86 |
+
f"👥 Portsjonite arv: {recipe_info.get('servings', 'N/A')}\n\n"
|
| 87 |
+
f"📝 KOOSTISOSAD:\n{ingredients_et}\n\n"
|
| 88 |
+
f"👩🍳 VALMISTAMISE JUHISED:\n{instructions_et}\n\n"
|
| 89 |
+
f"ℹ️ LISAINFO:\n"
|
| 90 |
+
f"Kalorsus: {calories}\n"
|
| 91 |
+
f"Gluteenisisaldus: {gluten_content}\n"
|
| 92 |
+
f"Kalorid portsjoni kohta: {recipe_info.get('nutrition', {}).get('calories', 'N/A')}\n\n"
|
| 93 |
+
f"🔗 Originaalretsept: {recipe_link}"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
return recipe_description, recipe_link
|
| 97 |
+
else:
|
| 98 |
+
return "Viga retsepti detailide toomisel.", ""
|
| 99 |
+
else:
|
| 100 |
+
return "Viga retseptide toomisel.", ""
|
| 101 |
+
|
| 102 |
+
# Create the Gradio interface with explicit choices
|
| 103 |
+
iface = gr.Interface(
|
| 104 |
+
fn=find_recipes,
|
| 105 |
+
inputs=[
|
| 106 |
+
gr.Textbox(
|
| 107 |
+
lines=1,
|
| 108 |
+
placeholder="Sisestage koostisosa (nt lõhe, šokolaad)",
|
| 109 |
+
label="Koostisosa"
|
| 110 |
+
),
|
| 111 |
+
gr.Dropdown(
|
| 112 |
+
choices=["Madal kalorsus", "Kõrge kalorsus"],
|
| 113 |
+
label="Kalorsus",
|
| 114 |
+
type="value"
|
| 115 |
+
),
|
| 116 |
+
gr.Dropdown(
|
| 117 |
+
choices=["Sisaldab gluteeni", "Gluteenivaba"],
|
| 118 |
+
label="Gluteenisisaldus",
|
| 119 |
+
type="value"
|
| 120 |
+
)
|
| 121 |
+
],
|
| 122 |
+
outputs=[
|
| 123 |
+
gr.Textbox(lines=20, label="Genereeritud Retsept"),
|
| 124 |
+
gr.Textbox(lines=1, label="Retsepti Link")
|
| 125 |
+
],
|
| 126 |
+
title="🍳 Nutikas Retsepti Generaator",
|
| 127 |
+
description=(
|
| 128 |
+
"Sisestage koostisosa, valige kalorsus ja gluteenisisaldus. "
|
| 129 |
+
"Saate personaalse retsepti!"
|
| 130 |
+
),
|
| 131 |
+
examples=[
|
| 132 |
+
["lõhe", "Madal kalorsus", "Gluteenivaba"],
|
| 133 |
+
["šokolaadikook", "Kõrge kalorsus", "Sisaldab gluteeni"],
|
| 134 |
+
["kartul", "Madal kalorsus", "Gluteenivaba"]
|
| 135 |
+
],
|
| 136 |
+
theme="default"
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# Launch the interface
|
| 140 |
+
if __name__ == "__main__":
|
| 141 |
+
iface.launch()
|