Spaces:
Sleeping
Sleeping
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 | |
if max_calories is not None: | |
params["maxCalories"] = max_calories | |
if min_protein is not None: | |
params["minProtein"] = min_protein | |
if max_fat is not None: | |
params["maxFat"] = max_fat | |
if max_carbs is not None: | |
params["maxCarbs"] = max_carbs | |
# Debugging: Print request parameters | |
print("Request Params:", params) | |
# Send the API request | |
response = requests.get(url, params=params) | |
# Debugging: Print response status | |
print("Response Status:", response.status_code) | |
# Check if the request was successful | |
if response.status_code == 200: | |
# Debugging: Print the full API response | |
print("API Response:", response.json()) | |
# Extract recipes from the API response | |
recipes = response.json().get('results', []) | |
if recipes: | |
# Format and return the list of recipes | |
return "\n".join( | |
[f"Recipe: {recipe['title']} (Link: https://spoonacular.com/recipes/{recipe['id']})" | |
for recipe in recipes] | |
) | |
else: | |
return "No suitable recipes found." | |
else: | |
# Handle API errors | |
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="Enter ingredients in Estonian"), | |
gr.Number(label="Maximum calories (optional)"), | |
gr.Number(label="Minimum protein (g, optional)"), | |
gr.Number(label="Maximum fat (g, optional)"), | |
gr.Number(label="Maximum carbs (g, optional)"), | |
], | |
outputs="text", | |
title="Estonian Recipe Search", | |
description="Enter ingredients in Estonian and set optional limits for calories, protein, fat, and carbs to find suitable recipes." | |
) | |
interface.launch() |