Update app.py
Browse files
app.py
CHANGED
@@ -14,25 +14,6 @@ def encode_image_to_base64(image_path):
|
|
14 |
with open(image_path, "rb") as image_file:
|
15 |
return base64.b64encode(image_file.read()).decode('utf-8')
|
16 |
|
17 |
-
def save_uploaded_image(image):
|
18 |
-
"""Save uploaded image to a temporary file and return the path"""
|
19 |
-
if image is None:
|
20 |
-
return None
|
21 |
-
|
22 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
|
23 |
-
if isinstance(image, dict) and "path" in image:
|
24 |
-
with open(image["path"], "rb") as img_file:
|
25 |
-
temp_file.write(img_file.read())
|
26 |
-
elif isinstance(image, Image.Image):
|
27 |
-
image.save(temp_file.name, format="JPEG")
|
28 |
-
else:
|
29 |
-
try:
|
30 |
-
Image.open(image).save(temp_file.name, format="JPEG")
|
31 |
-
except Exception:
|
32 |
-
return None
|
33 |
-
|
34 |
-
return temp_file.name
|
35 |
-
|
36 |
def analyze_single_image(client, img_path):
|
37 |
"""Analyze a single image to identify ingredients"""
|
38 |
system_prompt = """You are a culinary expert AI assistant that specializes in identifying ingredients in images.
|
@@ -69,28 +50,14 @@ def analyze_single_image(client, img_path):
|
|
69 |
except Exception as e:
|
70 |
return f"Error analyzing image: {str(e)}"
|
71 |
|
72 |
-
def get_recipe_suggestions(api_key,
|
73 |
"""Get recipe suggestions based on the uploaded images of ingredients"""
|
74 |
if not api_key:
|
75 |
return "Please provide your Together API key."
|
76 |
|
77 |
-
if not
|
78 |
return "Please upload at least one image of ingredients."
|
79 |
|
80 |
-
valid_images = [img for img in images if img is not None]
|
81 |
-
|
82 |
-
if len(valid_images) == 0:
|
83 |
-
return "No valid images were uploaded. Please try again."
|
84 |
-
|
85 |
-
image_paths = []
|
86 |
-
for img in valid_images:
|
87 |
-
img_path = save_uploaded_image(img)
|
88 |
-
if img_path:
|
89 |
-
image_paths.append(img_path)
|
90 |
-
|
91 |
-
if not image_paths:
|
92 |
-
return "Failed to process the uploaded images."
|
93 |
-
|
94 |
try:
|
95 |
client = Together(api_key=api_key)
|
96 |
|
@@ -136,12 +103,6 @@ def get_recipe_suggestions(api_key, images, num_recipes=3, dietary_restrictions=
|
|
136 |
temperature=0.7
|
137 |
)
|
138 |
|
139 |
-
for img_path in image_paths:
|
140 |
-
try:
|
141 |
-
os.unlink(img_path)
|
142 |
-
except:
|
143 |
-
pass
|
144 |
-
|
145 |
result = "## π Ingredients Identified\n\n"
|
146 |
result += combined_ingredients
|
147 |
result += "\n\n---\n\n"
|
@@ -149,15 +110,21 @@ def get_recipe_suggestions(api_key, images, num_recipes=3, dietary_restrictions=
|
|
149 |
result += response.choices[0].message.content
|
150 |
|
151 |
return result
|
152 |
-
|
153 |
except Exception as e:
|
154 |
-
for img_path in image_paths:
|
155 |
-
try:
|
156 |
-
os.unlink(img_path)
|
157 |
-
except:
|
158 |
-
pass
|
159 |
return f"Error: {str(e)}"
|
160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
custom_css = """
|
162 |
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');
|
163 |
:root {
|
@@ -472,19 +439,8 @@ with gr.Blocks(css=custom_css) as app:
|
|
472 |
|
473 |
gr.HTML(html_footer)
|
474 |
|
475 |
-
def update_gallery(files):
|
476 |
-
if not files:
|
477 |
-
return gr.Gallery.update(visible=False)
|
478 |
-
return gr.Gallery.update(value=[file.name for file in files], visible=True)
|
479 |
-
|
480 |
file_upload.change(fn=update_gallery, inputs=file_upload, outputs=image_input)
|
481 |
|
482 |
-
def process_recipe_request(api_key, files, num_recipes, dietary_restrictions, cuisine_preference):
|
483 |
-
if not files:
|
484 |
-
return "Please upload at least one image of ingredients."
|
485 |
-
images = [file.name for file in files]
|
486 |
-
return get_recipe_suggestions(api_key, images, num_recipes, dietary_restrictions, cuisine_preference)
|
487 |
-
|
488 |
submit_button.click(
|
489 |
fn=process_recipe_request,
|
490 |
inputs=[api_key_input, file_upload, num_recipes, dietary_restrictions, cuisine_preference],
|
|
|
14 |
with open(image_path, "rb") as image_file:
|
15 |
return base64.b64encode(image_file.read()).decode('utf-8')
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def analyze_single_image(client, img_path):
|
18 |
"""Analyze a single image to identify ingredients"""
|
19 |
system_prompt = """You are a culinary expert AI assistant that specializes in identifying ingredients in images.
|
|
|
50 |
except Exception as e:
|
51 |
return f"Error analyzing image: {str(e)}"
|
52 |
|
53 |
+
def get_recipe_suggestions(api_key, image_paths, num_recipes=3, dietary_restrictions="None", cuisine_preference="Any"):
|
54 |
"""Get recipe suggestions based on the uploaded images of ingredients"""
|
55 |
if not api_key:
|
56 |
return "Please provide your Together API key."
|
57 |
|
58 |
+
if not image_paths or len(image_paths) == 0:
|
59 |
return "Please upload at least one image of ingredients."
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
try:
|
62 |
client = Together(api_key=api_key)
|
63 |
|
|
|
103 |
temperature=0.7
|
104 |
)
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
result = "## π Ingredients Identified\n\n"
|
107 |
result += combined_ingredients
|
108 |
result += "\n\n---\n\n"
|
|
|
110 |
result += response.choices[0].message.content
|
111 |
|
112 |
return result
|
|
|
113 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
114 |
return f"Error: {str(e)}"
|
115 |
|
116 |
+
def update_gallery(files):
|
117 |
+
"""Update the gallery with uploaded image paths"""
|
118 |
+
if not files or len(files) == 0:
|
119 |
+
return gr.update(visible=False)
|
120 |
+
return gr.update(value=files, visible=True)
|
121 |
+
|
122 |
+
def process_recipe_request(api_key, files, num_recipes, dietary_restrictions, cuisine_preference):
|
123 |
+
"""Process the recipe request with uploaded files"""
|
124 |
+
if not files:
|
125 |
+
return "Please upload at least one image of ingredients."
|
126 |
+
return get_recipe_suggestions(api_key, files, num_recipes, dietary_restrictions, cuisine_preference)
|
127 |
+
|
128 |
custom_css = """
|
129 |
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');
|
130 |
:root {
|
|
|
439 |
|
440 |
gr.HTML(html_footer)
|
441 |
|
|
|
|
|
|
|
|
|
|
|
442 |
file_upload.change(fn=update_gallery, inputs=file_upload, outputs=image_input)
|
443 |
|
|
|
|
|
|
|
|
|
|
|
|
|
444 |
submit_button.click(
|
445 |
fn=process_recipe_request,
|
446 |
inputs=[api_key_input, file_upload, num_recipes, dietary_restrictions, cuisine_preference],
|