import gradio as gr import requests import io import os from PIL import Image from googleapiclient.discovery import build # API and Model Setup HF_header = os.getenv('header') headers = {"Authorization": HF_header} YOUTUBE_DATA_API = os.getenv('YOUTUBE_API') # replace this with your own YouTube Data API key youtube = build('youtube', 'v3', developerKey=YOUTUBE_DATA_API) mealdb_base_url = os.getenv('mealdb_base_api') # Function to convert image to base64 def image_to_base64(image): buffered = io.BytesIO() image.save(buffered, format="JPEG") return base64.b64encode(buffered.getvalue()).decode('utf-8') # Function to perform inference on the image using 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." # Gradio interface function that handles image uploads and processes the data def gradio_interface(image): dish_name = perform_inference(image) youtube_links = search_youtube_videos(dish_name) recipe_details = get_recipe_details(dish_name) # Generate HTML content for embedding videos youtube_html = generate_embed_html(youtube_links) return dish_name, youtube_html, recipe_details iface = gr.Interface( fn=gradio_interface, inputs=gr.Image(type="pil", label="Upload an Image"), outputs=[ gr.Textbox(label="Predicted Dish"), gr.HTML(label="YouTube Recipe Videos"), gr.Textbox(label="Recipe Details") ], title="Dish Prediction, Recipe Videos, and Recipe Details", description="Upload an image of food, and the app will predict the dish, provide YouTube links for recipes, and fetch detailed recipe instructions." ) if __name__ == "__main__": iface.launch() def gradio_interface(image): dish_name = perform_inference(image) return dish_name, "" def show_recipe(dish_name): if dish_name: return get_recipe_details(dish_name) return "No dish predicted. Please upload an image and predict the dish first.", "" def show_videos(dish_name): if dish_name: video_links = search_youtube_videos(dish_name) return "", generate_embed_html(video_links) return "", "No dish predicted. Please upload an image and predict the dish first." iface = gr.Blocks()