Slfagrouche commited on
Commit
a4e68e4
·
verified ·
1 Parent(s): ea7626d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import io
4
+ import os
5
+ from PIL import Image
6
+ from googleapiclient.discovery import build
7
+
8
+ # API and Model Setup
9
+ HF_header = os.getenv('header')
10
+ headers = {"Authorization": HF_header}
11
+ YOUTUBE_DATA_API = os.getenv('YOUTUBE_API') # replace this with your own YouTube Data API key
12
+ youtube = build('youtube', 'v3', developerKey=YOUTUBE_DATA_API)
13
+ mealdb_base_url = os.getenv('mealdb_base_api')
14
+
15
+ # Function to convert image to base64
16
+ def image_to_base64(image):
17
+ buffered = io.BytesIO()
18
+ image.save(buffered, format="JPEG")
19
+ return base64.b64encode(buffered.getvalue()).decode('utf-8')
20
+
21
+ # Function to perform inference on the image using Hugging Face model
22
+ def perform_inference(image):
23
+ buffered = io.BytesIO()
24
+ image.save(buffered, format="JPEG")
25
+ data = buffered.getvalue()
26
+ model_url = "https://api-inference.huggingface.co/models/juliensimon/autotrain-food101-1471154053"
27
+ response = requests.post(model_url, headers=headers, data=data)
28
+ result = response.json()
29
+ return result[0]['label']
30
+
31
+ # Function to search YouTube videos based on a query
32
+ def search_youtube_videos(query):
33
+ search_params = {
34
+ 'q': query + " recipe",
35
+ 'part': 'snippet',
36
+ 'maxResults': 5,
37
+ 'type': 'video',
38
+ }
39
+ search_response = youtube.search().list(**search_params).execute()
40
+ video_ids = [item['id']['videoId'] for item in search_response['items']]
41
+ return [f"https://www.youtube.com/embed/{video_id}" for video_id in video_ids]
42
+
43
+ # Function to get recipe details from TheMealDB
44
+ def get_recipe_details(query):
45
+ response = requests.get(f"{mealdb_base_url}search.php?s={query}")
46
+ data = response.json()
47
+ meals = data.get('meals', [])
48
+ if meals:
49
+ meal_id = meals[0]['idMeal']
50
+ details_response = requests.get(f"{mealdb_base_url}lookup.php?i={meal_id}")
51
+ details_data = details_response.json()
52
+ recipe = details_data['meals'][0]
53
+ ingredients = "\n".join([f"{recipe[f'strIngredient{i}']}: {recipe[f'strMeasure{i}']}" for i in range(1, 21) if recipe[f'strIngredient{i}']])
54
+ return f"{recipe['strMeal']} -\n\nSteps:\n{recipe['strInstructions']}\n\nIngredients:\n{ingredients}"
55
+ return "Recipe details not found."
56
+
57
+ # Gradio interface function that handles image uploads and processes the data
58
+
59
+ def gradio_interface(image):
60
+ dish_name = perform_inference(image)
61
+ youtube_links = search_youtube_videos(dish_name)
62
+ recipe_details = get_recipe_details(dish_name)
63
+ # Generate HTML content for embedding videos
64
+ youtube_html = generate_embed_html(youtube_links)
65
+ return dish_name, youtube_html, recipe_details
66
+
67
+ iface = gr.Interface(
68
+ fn=gradio_interface,
69
+ inputs=gr.Image(type="pil", label="Upload an Image"),
70
+ outputs=[
71
+ gr.Textbox(label="Predicted Dish"),
72
+ gr.HTML(label="YouTube Recipe Videos"),
73
+ gr.Textbox(label="Recipe Details")
74
+ ],
75
+ title="Dish Prediction, Recipe Videos, and Recipe Details",
76
+ description="Upload an image of food, and the app will predict the dish, provide YouTube links for recipes, and fetch detailed recipe instructions."
77
+ )
78
+
79
+ if __name__ == "__main__":
80
+ iface.launch()
81
+
82
+
83
+ def gradio_interface(image):
84
+ dish_name = perform_inference(image)
85
+ return dish_name, ""
86
+
87
+ def show_recipe(dish_name):
88
+ if dish_name:
89
+ return get_recipe_details(dish_name)
90
+ return "No dish predicted. Please upload an image and predict the dish first.", ""
91
+
92
+ def show_videos(dish_name):
93
+ if dish_name:
94
+ video_links = search_youtube_videos(dish_name)
95
+ return "", generate_embed_html(video_links)
96
+ return "", "No dish predicted. Please upload an image and predict the dish first."
97
+
98
+ iface = gr.Blocks()
99
+