Spaces:
No application file
No application file
Commit ·
0ed2a5c
1
Parent(s): 4917677
Aplicativo detecção de objtetos e criação de receita com HF e OpenAI
Browse files- recipe_app.py +100 -0
recipe_app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Data Scientist.: Dr. Eddy Giusepe Chirinos Isidro
|
| 3 |
+
|
| 4 |
+
Link de estudo:
|
| 5 |
+
|
| 6 |
+
https://youtu.be/nGjHq7sXoSQ
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import tkinter as tk
|
| 10 |
+
from tkinter import filedialog
|
| 11 |
+
from PIL import Image, ImageTk
|
| 12 |
+
import requests
|
| 13 |
+
import os
|
| 14 |
+
import openai
|
| 15 |
+
import torch
|
| 16 |
+
from transformers import DetrForObjectDetection, DetrImageProcessor
|
| 17 |
+
|
| 18 |
+
# OpenAI API key
|
| 19 |
+
openai.api_key = os.getenv("openai_key")
|
| 20 |
+
|
| 21 |
+
# Initialize tkinter window
|
| 22 |
+
root = tk.Tk()
|
| 23 |
+
canvas = tk.Canvas(root, height=600, width=800)
|
| 24 |
+
canvas.pack()
|
| 25 |
+
|
| 26 |
+
# Create Detr object
|
| 27 |
+
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-101")
|
| 28 |
+
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-101")
|
| 29 |
+
|
| 30 |
+
# Create a frame for the canvas
|
| 31 |
+
frame = tk.Frame(root, bg='#80c1ff', bd=5)
|
| 32 |
+
frame.place(relx=0.15, rely=0.15, relwidth=0.7, relheight=0.7)
|
| 33 |
+
|
| 34 |
+
# Create an upload button
|
| 35 |
+
uploadButton = tk.Button(root, text='Upload Image', command=lambda: upload())
|
| 36 |
+
uploadButton.pack()
|
| 37 |
+
|
| 38 |
+
# Create a recipe button
|
| 39 |
+
recipeButton = tk.Button(root, text='Generate Recipe', command=lambda: generate_recipe())
|
| 40 |
+
recipeButton.pack()
|
| 41 |
+
|
| 42 |
+
# Create a quit button
|
| 43 |
+
quitButton = tk.Button(root, text='Quit', command=root.quit)
|
| 44 |
+
quitButton.pack()
|
| 45 |
+
|
| 46 |
+
# Create a label for the recipe, wrap the text with 500 characters
|
| 47 |
+
recipe = tk.Label(frame, text="Recipe", bg="lightblue", wraplength=500)
|
| 48 |
+
recipe.pack()
|
| 49 |
+
|
| 50 |
+
def upload():
|
| 51 |
+
# Get the filename from the dialog
|
| 52 |
+
filename = filedialog.askopenfilename(initialdir="/", title="Select an image", filetypes=(("JPG files", "*.jpg"), ("JPEG files", "*.jpeg"), ("PNG files", "*.png")))
|
| 53 |
+
|
| 54 |
+
# Load the image
|
| 55 |
+
global image
|
| 56 |
+
image = Image.open(filename)
|
| 57 |
+
|
| 58 |
+
# Resize the image
|
| 59 |
+
resized = image.resize((800, 600), Image.ANTIALIAS)
|
| 60 |
+
|
| 61 |
+
# Create a PhotoImage object
|
| 62 |
+
global photo
|
| 63 |
+
photo = ImageTk.PhotoImage(resized)
|
| 64 |
+
|
| 65 |
+
# Add the image to the canvas
|
| 66 |
+
canvas.create_image(0, 0, image=photo, anchor=tk.NW)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def generate_recipe():
|
| 70 |
+
# Run the model
|
| 71 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 72 |
+
outputs = model(**inputs)
|
| 73 |
+
|
| 74 |
+
# Convert outputs (bounding boxes and class logits) to COCO API
|
| 75 |
+
# Let's only keep detections with score > 0.9
|
| 76 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 77 |
+
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.7)[0]
|
| 78 |
+
|
| 79 |
+
# Get the list of detected objects
|
| 80 |
+
detected_objects = []
|
| 81 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 82 |
+
detected_objects.append(model.config.id2label[label.item()])
|
| 83 |
+
|
| 84 |
+
# Generate the recipe
|
| 85 |
+
prompt = f"Create a recipe from the edible items from this list. Do not use anything that is not on the list: {detected_objects}"
|
| 86 |
+
response = openai.Completion.create(
|
| 87 |
+
model="text-davinci-003",
|
| 88 |
+
prompt=prompt,
|
| 89 |
+
temperature=0.7,
|
| 90 |
+
max_tokens=256,
|
| 91 |
+
top_p=1,
|
| 92 |
+
frequency_penalty=0,
|
| 93 |
+
presence_penalty=0
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Update the recipe label
|
| 97 |
+
recipe.config(text=response["choices"][0]["text"])
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
root.mainloop()
|