File size: 3,719 Bytes
a4e68e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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()