Spaces:
Sleeping
Sleeping
File size: 3,567 Bytes
9a2d557 ab45f73 9a2d557 ab45f73 9a2d557 ab45f73 9a2d557 803c063 9a2d557 803c063 9a2d557 1314ff5 9a2d557 1314ff5 9a2d557 1314ff5 9a2d557 1314ff5 ad87128 1314ff5 ad87128 1314ff5 ad87128 1314ff5 ad87128 1314ff5 9a2d557 64b1747 1314ff5 9a2d557 afa6f9a 34bd3e2 64b1747 9a2d557 64b1747 34bd3e2 9a2d557 67336d1 9a2d557 34bd3e2 f841f64 9a2d557 |
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 |
import os
import gradio as gr
import requests
# Get API keys from Secrets
spoonacular_api_key = os.getenv('SPOONACULAR_API_KEY') # Spoonacular API key
hf_api_key = os.getenv('HF_API_KEY') # Hugging Face API key
# Translation function using Hugging Face
def translate_text_hf(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()[0]['translation_text']
print("Translated ingredients:", translation) # Debug: Print translation
return translation
else:
print("Translation failed with status:", response.status_code) # Debug: Print failure
return "Translation failed!"
# Recipe search function using Spoonacular API
def get_recipes(ingredients, max_calories=None, min_protein=None, max_fat=None, max_carbs=None):
# Translate ingredients to English
translated_ingredients = translate_text_hf(ingredients)
url = "https://api.spoonacular.com/recipes/complexSearch"
# Base parameters for the API request
params = {
"query": translated_ingredients,
"apiKey": spoonacular_api_key,
"number": 5 # Return up to 5 recipes
}
# Add optional parameters only if they are provided and not zero
if max_calories and max_calories > 0:
params["maxCalories"] = max_calories
if min_protein and min_protein > 0:
params["minProtein"] = min_protein
if max_fat and max_fat > 0:
params["maxFat"] = max_fat
if max_carbs and max_carbs > 0:
params["maxCarbs"] = max_carbs
# Send the API request
response = requests.get(url, params=params)
# Check if the request was successful
if response.status_code == 200:
recipes = response.json().get('results', [])
if recipes:
# Format and return the list of recipes as HTML links
html_links = "<br>".join(
[f'<a href="https://spoonacular.com/recipes/{recipe["title"].replace(" ", "-").lower()}-{recipe["id"]}" target="_blank">{recipe["title"]}</a>'
for recipe in recipes]
)
# Debug: Print each generated URL for the recipes
for recipe in recipes:
print(f"Generated URL: https://spoonacular.com/recipes/{recipe['title'].replace(' ', '-').lower()}-{recipe['id']}")
return html_links
else:
return "No suitable recipes found."
else:
return f"Error fetching recipes: {response.status_code}"
# Gradio Interface
def recipe_app(ingredients, max_calories, min_protein, max_fat, max_carbs):
return get_recipes(ingredients, max_calories, min_protein, max_fat, max_carbs)
# Create Gradio interface
interface = gr.Interface(
fn=recipe_app,
inputs=[
gr.Textbox(label="Sisesta koostisained"),
gr.Number(label="Maksimaalne kalorikogus (valikuline)"),
gr.Number(label="Minimaalne valk (g, valikuline)"),
gr.Number(label="Maksimaalne rasvasisaldus (g, valikuline)"),
gr.Number(label="Maksimaalne süsivesikute sisaldus (g, valikuline)"),
],
outputs=gr.HTML(label="Recipes"), # Ensure output is HTML
title="Retseptiotsija",
description="Sisestage koostisosad eesti keeles ja määrake sobivate retseptide leidmiseks valikulised kalorite, valkude, rasvade ja süsivesikute piirangud."
)
interface.launch() |