import gradio as gr import requests import io import os from PIL import Image from googleapiclient.discovery import build # API and Model Setup using environment variables for security headers = {"Authorization": os.getenv('HF_API_KEY')} YOUTUBE_DATA_API = os.getenv('YOUTUBE_API_KEY') mealdb_base_url = os.getenv('MEALDB_BASE_URL') youtube = build('youtube', 'v3', developerKey=YOUTUBE_DATA_API) # Function to convert image to base64 (unused in this script but defined) def image_to_base64(image): buffered = io.BytesIO() image.save(buffered, format="JPEG") import base64 return base64.b64encode(buffered.getvalue()).decode('utf-8') # Function to perform inference on the image using a Hugging Face model def perform_inference(image): buffered = io.BytesIO() image.save(buffered, format="JPEG") data = buffered.getvalue() model_url = "https://api-inference.huggingface.co/models/juliensimon/autotrain-food101-1471154053" response = requests.post(model_url, headers=headers, data=data) result = response.json() return result[0]['label'] # Function to search YouTube videos based on a query def search_youtube_videos(query): search_params = { 'q': query + " recipe", 'part': 'snippet', 'maxResults': 5, 'type': 'video', } search_response = youtube.search().list(**search_params).execute() video_ids = [item['id']['videoId'] for item in search_response['items']] return [f"https://www.youtube.com/embed/{video_id}" for video_id in video_ids] # Function to get recipe details from TheMealDB def get_recipe_details(query): response = requests.get(f"{mealdb_base_url}search.php?s={query}") data = response.json() meals = data.get('meals', []) if meals: meal_id = meals[0]['idMeal'] details_response = requests.get(f"{mealdb_base_url}lookup.php?i={meal_id}") details_data = details_response.json() recipe = details_data['meals'][0] ingredients = "\n".join([f"{recipe[f'strIngredient{i}']}: {recipe[f'strMeasure{i}']}" for i in range(1, 21) if recipe[f'strIngredient{i}']]) return f"{recipe['strMeal']} -\n\nSteps:\n{recipe['strInstructions']}\n\nIngredients:\n{ingredients}" return "Recipe details not found." # Function to generate HTML content for embedding videos def generate_embed_html(video_links): html_content = "