Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,103 @@
|
|
| 1 |
|
| 2 |
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from transformers import pipeline
|
| 5 |
-
import torch
|
| 6 |
|
| 7 |
-
# Initialize
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
recipe =
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Create the Gradio interface
|
| 39 |
iface = gr.Interface(
|
| 40 |
-
fn=
|
| 41 |
-
inputs=
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
# Launch the interface
|
| 49 |
if __name__ == "__main__":
|
| 50 |
iface.launch()
|
|
|
|
|
|
| 1 |
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
+
import requests
|
| 5 |
from transformers import pipeline
|
|
|
|
| 6 |
|
| 7 |
+
# Initialize translation models
|
| 8 |
+
translate_to_english = pipeline("translation", model="Helsinki-NLP/opus-mt-et-en")
|
| 9 |
+
translate_to_estonian = pipeline("translation", model="Helsinki-NLP/opus-mt-en-et")
|
| 10 |
|
| 11 |
+
# Spoonacular API settings
|
| 12 |
+
API_KEY = '0bf3de98d2654cb28f139df9fedb7db2'
|
| 13 |
+
SPOONACULAR_URL = "https://api.spoonacular.com/recipes/findByIngredients"
|
| 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, wheat_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 |
+
params = {
|
| 29 |
+
"ingredients": ingredient_en,
|
| 30 |
+
"apiKey": API_KEY
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
response = requests.get(SPOONACULAR_URL, params=params)
|
| 34 |
+
|
| 35 |
+
if response.status_code == 200:
|
| 36 |
+
recipes = response.json()
|
| 37 |
|
| 38 |
+
if not recipes:
|
| 39 |
+
return "Ühtegi retsepti ei leitud.", ""
|
| 40 |
|
| 41 |
+
# Select the first recipe for demonstration
|
| 42 |
+
recipe = recipes[0]
|
| 43 |
|
| 44 |
+
# Prepare recipe details
|
| 45 |
+
recipe_name_en = recipe['title']
|
| 46 |
+
recipe_name_et = translate_text(recipe_name_en, direction='en-et')
|
| 47 |
+
recipe_link = f"https://spoonacular.com/recipes/{recipe['id']}-{recipe_name_en.replace(' ', '-')}"
|
| 48 |
+
|
| 49 |
+
# Translate ingredients to Estonian
|
| 50 |
+
missed_ingredients_en = ', '.join([ingredient['name'] for ingredient in recipe['missedIngredients']])
|
| 51 |
+
missed_ingredients_et = translate_text(missed_ingredients_en, direction='en-et')
|
| 52 |
+
|
| 53 |
+
# Create a recipe description
|
| 54 |
+
recipe_description = (
|
| 55 |
+
f"Retsept: {recipe_name_et}\n\n"
|
| 56 |
+
f"Link: {recipe_link}\n\n"
|
| 57 |
+
f"Koostisosad: {missed_ingredients_et}\n\n"
|
| 58 |
+
f"Valmistamise juhised: [Link to instructions]"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
return recipe_description, recipe_link
|
| 62 |
+
else:
|
| 63 |
+
return "Viga retseptide toomisel.", ""
|
| 64 |
|
| 65 |
# Create the Gradio interface
|
| 66 |
iface = gr.Interface(
|
| 67 |
+
fn=find_recipes,
|
| 68 |
+
inputs=[
|
| 69 |
+
gr.Textbox(
|
| 70 |
+
lines=1,
|
| 71 |
+
placeholder="Sisestage koostisosa (nt lõhe, šokolaad)",
|
| 72 |
+
label="Koostisosa"
|
| 73 |
+
),
|
| 74 |
+
gr.Radio(
|
| 75 |
+
["Low Calories", "High Calories"],
|
| 76 |
+
label="Kalorsus"
|
| 77 |
+
),
|
| 78 |
+
gr.Radio(
|
| 79 |
+
["Contains Wheat", "Wheat Free"],
|
| 80 |
+
label="Nisusisaldus"
|
| 81 |
+
)
|
| 82 |
+
],
|
| 83 |
+
outputs=[
|
| 84 |
+
gr.Textbox(lines=15, label="Genereeritud Retsept"),
|
| 85 |
+
gr.Textbox(lines=1, label="Retsepti Link")
|
| 86 |
+
],
|
| 87 |
+
title="🍳 Nutikas Retsepti Generator",
|
| 88 |
+
description=(
|
| 89 |
+
"Sisestage koostisosa, valige kalorsus ja nisusisaldus. "
|
| 90 |
+
"Saate personaalse retsepti!"
|
| 91 |
+
),
|
| 92 |
+
examples=[
|
| 93 |
+
["lõhe", "Low Calories", "Wheat Free"],
|
| 94 |
+
["šokolaadikook", "High Calories", "Contains Wheat"],
|
| 95 |
+
["kartul", "Low Calories", "Wheat Free"]
|
| 96 |
+
],
|
| 97 |
+
theme="default"
|
| 98 |
)
|
| 99 |
|
| 100 |
# Launch the interface
|
| 101 |
if __name__ == "__main__":
|
| 102 |
iface.launch()
|
| 103 |
+
|