Retseptikogu / app.py
Pilleliis's picture
Update app.py
df88637 verified
import torch
import tensorflow as tf
import streamlit as st
from transformers import pipeline
import requests
# Hugging Face'i tõlkemudel
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-et-en")
# Spoonacular API
API_KEY = "063a7388c8094613b724beda1804059e"
API_URL = "https://api.spoonacular.com/recipes/findByIngredients"
DETAIL_URL = "https://api.spoonacular.com/recipes/{id}/information" # Parandatud DETAIL_URL määratlus
# Streamlit kasutajaliides
st.title("Retseptide generaator")
ingredients = st.text_area("Sisesta koostisosad eesti keeles (nt kinoa, tomat)")
max_calories = st.number_input("Maksimaalsed kalorid (valikuline)", min_value=0, step=1, value=0)
min_protein = st.number_input("Minimaalne proteiin (valikuline)", min_value=0, step=1, value=0)
if st.button("Otsi retsepte"):
if ingredients:
# Tõlgi koostisosad
translated_ingredients = translator(ingredients)[0]['translation_text']
# Tee päring Spoonacular API-le
params = {
"ingredients": translated_ingredients,
"number": 5,
"apiKey": API_KEY,
}
response = requests.get(API_URL, params=params)
if response.status_code == 200:
recipes = response.json()
st.write("Leitud retseptid:")
if not recipes:
st.warning("Ühtegi retsepti ei leitud nende koostisosade põhjal.")
else:
for recipe in recipes:
recipe_id = recipe['id']
detail_response = requests.get(
DETAIL_URL.format(id=recipe_id),
params={"apiKey": API_KEY}
)
if detail_response.status_code == 200:
details = detail_response.json()
recipe_link = details.get('sourceUrl', 'Link puudub')
st.write(f"- **{details['title']}**")
st.write(f"[Vaata retsepti siin]({recipe_link})")
else:
st.warning(f"Ei saanud üksikasju retsepti jaoks: {recipe['title']}")
else:
st.error(f"Viga retseptide leidmisel! Status code: {response.status_code}")
st.write("Vastus serverist:")
st.write(response.json()) # Kuvame vastuse tõrkeotsinguks
else:
st.warning("Palun sisesta koostisosad.")