cookai / app.py
ajdarshaydullin's picture
fix form
d081832
import gradio as gr
# Use a pipeline as a high-level helper
from transformers import pipeline
detector = pipeline("object-detection", model="hustvl/yolos-tiny")
# Use a pipeline as a high-level helper
from transformers import pipeline
food_classifier = pipeline("image-classification", model="facebook/deit-base-distilled-patch16-384")
def get_ingridients_list(image, score_threshold=.85):
objects = detector(image)
ingridients = []
for obj in objects:
cropped_image = image.crop((obj['box']['xmin'], obj['box']['ymin'], obj['box']['xmax'], obj['box']['ymax']))
classes = food_classifier(cropped_image)
best_match = max(classes, key=lambda x: x['score'])
if best_match['score'] > score_threshold:
ingridients.append(best_match['label'])
return list(set(ingridients))
def get_ingridients(image):
ingridients = get_ingridients_list(image)
return ', '.join(ingridients)
from yandex_cloud_ml_sdk import YCloudML
import os
def get_reciepe(ingridients):
messages = [
{
"role": "system",
"text": "suggest a dish that can be prepared from the suggested ingredients",
},
{
"role": "user",
"text": ingridients,
},
]
sdk = YCloudML(
folder_id="b1ghdrfjtfkvir55hc0m",
auth=os.getenv('YC_APIKEY'),
)
result = (
sdk.models.completions("yandexgpt-lite").configure(temperature=0.5).run(messages)
)
return str(result[0].text)
def get_answer(image):
ingridients = get_ingridients(image)
return get_reciepe(ingridients)
# Create a Gradio interface
iface = gr.Interface(
fn=get_answer, # Function to call
inputs=gr.Image(label="Upload an image", type="pil"), # Input type: Image
outputs=gr.Markdown(label="Result"), # Output type: Markdown
title="cook.ai",
description="Upload an image of a food ingredients"
)
# Launch the Gradio app
iface.launch()