|
import streamlit as st |
|
import requests |
|
import os |
|
|
|
spoonacular_api_key = os.getenv('SP_KEY') |
|
hf_api_key = os.getenv('HF_KEY') |
|
|
|
|
|
def translate_from_estonian(text): |
|
url = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-et-en" |
|
headers = {"Authorization": f"Bearer {hf_api_key}"} |
|
payload = {"inputs": text} |
|
|
|
response = requests.post(url, headers=headers, json=payload) |
|
if response.status_code == 200: |
|
translation = response.json() |
|
return translation[0]['translation_text'] |
|
else: |
|
print(f"Error: {response.status_code} - {response.text}") |
|
return None |
|
|
|
|
|
def get_recipes(ingredients, calorie_limit, protein_limit, fat_limit): |
|
|
|
translated_ingredients = translate_from_estonian(ingredients) |
|
url = "https://api.spoonacular.com/recipes/complexSearch" |
|
|
|
params = { |
|
"query": translated_ingredients, |
|
"apiKey": spoonacular_api_key, |
|
"number": 10 |
|
} |
|
|
|
if calorie_limit and calorie_limit > 0: |
|
params["maxCalories"] = calorie_limit |
|
if protein_limit and protein_limit > 0: |
|
params["minProtein"] = protein_limit |
|
if fat_limit and fat_limit > 0: |
|
params["maxFat"] = fat_limit |
|
|
|
|
|
response = requests.get(url) |
|
|
|
|
|
if response.status_code != 200: |
|
st.error("Ebaõnnestumine. Proovi uuesti!") |
|
return [] |
|
|
|
|
|
recipes = response.json().get('results', []) |
|
|
|
|
|
filtered_recipes = [] |
|
for recipe in recipes: |
|
recipe_id = recipe['id'] |
|
|
|
|
|
detail_url = f"https://api.spoonacular.com/recipes/{recipe_id}/information?apiKey={api_key_spoonacular}" |
|
detail_response = requests.get(detail_url) |
|
|
|
if detail_response.status_code == 200: |
|
recipe_detail = detail_response.json() |
|
calories = recipe_detail['nutrition']['nutrients'][0]['amount'] |
|
protein = recipe_detail['nutrition']['nutrients'][1]['amount'] |
|
fat = recipe_detail['nutrition']['nutrients'][1]['amount'] |
|
|
|
|
|
if calories <= calorie_limit and protein >= protein_limit and fat<=fat_limit: |
|
translated_title = translate_to_estonian(recipe_detail['title']) |
|
translated_instructions = translate_to_estonian(recipe_detail['instructions']) |
|
|
|
filtered_recipes.append({ |
|
'name': translated_title if translated_title else recipe_detail['title'], |
|
'calories': calories, |
|
'protein': protein, |
|
'fat':fat, |
|
'instructions': translated_instructions if translated_instructions else recipe_detail['instructions'], |
|
'image': recipe_detail['image'] |
|
}) |
|
|
|
return filtered_recipes |
|
|
|
|
|
st.title("Retseptide generaator") |
|
|
|
|
|
ingredients = st.text_input("Lisa koostisosad:") |
|
|
|
|
|
calorie_limit = st.slider("Vali maksmimaalne kaloraaž:", min_value=100, max_value=1000, value=500) |
|
|
|
|
|
protein_limit = st.slider("Vali minimaalne proteiini kogus (g):", min_value=0, max_value=100, value=10) |
|
|
|
|
|
fat_limit = st.slider("Vali maksimaalne rasva kogus (g):", min_value=0, max_value=500, value=50) |
|
|
|
|
|
if st.button("Palun retseptisoovitust"): |
|
if ingredients: |
|
recipes = get_recipes(ingredients, calorie_limit, protein_limit, fat_limit) |
|
|
|
|
|
st.subheader("Genereerime retsepte:") |
|
for recipe in recipes: |
|
st.write(f"**{recipe['name']}** - Calories: {recipe['calories']}, Protein: {recipe['protein']}g") |
|
st.image(recipe['image']) |
|
st.write(recipe['instructions']) |
|
else: |
|
st.error("Palun sisesta vähemalt üks koostisosa.") |
|
|
|
|