Spaces:
Sleeping
Sleeping
File size: 1,914 Bytes
6f2e64e c725fc9 6f2e64e c725fc9 6f2e64e c725fc9 76d5a8f c725fc9 76d5a8f d081832 76d5a8f c725fc9 d081832 c725fc9 |
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 |
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()
|