Spaces:
Sleeping
Sleeping
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) | |
text_to_text = pipeline("text-generation", model="ai-forever/mGPT") | |
def get_reciepe(ingridients): | |
question = 'what can i cook of ' + ingridients + '?' | |
answers = text_to_text(question) | |
return answers[0]['generated_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="Classification Result"), # Output type: Markdown | |
title="Food Ingredient Classifier", | |
description="Upload an image of a food ingredient to classify it." | |
) | |
# Launch the Gradio app | |
iface.launch() | |