snap-to-chef / app.py
daniescamilla's picture
Update app.py
3fee28d verified
raw
history blame
2.78 kB
import os
import gradio as gr
import requests
from transformers import pipeline
# Obt茅n el token desde las variables de entorno
hf_token = os.getenv("HF_TOKEN")
# Paso 1: Cargar el modelo de image-to-text
image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
# Paso 2: Funci贸n para llamar a la API de Qwen con el token seguro
def generate_recipe(description):
url = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct"
headers = {"Authorization": f"Bearer {hf_token}"}
# A帽ade el system input para definir el rol del modelo
system_input = (
"Act as a professional chef. Your task is to explain to a regular person "
"how to cook a given dish, providing a step-by-step guide and a list of ingredients with exact quantities."
)
prompt = (
f"{system_input} The dish is described as: {description}. "
"Provide a list of ingredients followed by detailed cooking instructions."
)
payload = {
"inputs": prompt,
"parameters": {
"max_new_tokens": 1500, # Limita solo la cantidad de tokens generados en la respuesta
"min_length": 500, # Establece un m铆nimo de longitud para evitar truncamiento prematuro
"temperature": 0.7, # Ajusta la variabilidad
"do_sample": True, # Sampling para respuestas m谩s variadas
"repetition_penalty": 1.2, # Evita la repetici贸n excesiva
"stop_sequences": ["\n\n"] # Para detenerse en el salto de p谩rrafos y no truncar antes de tiempo
}
}
response = requests.post(url, headers=headers, json=payload)
response_data = response.json()
# Manejo de posibles errores
if "error" in response_data:
return "Error generating recipe: " + response_data["error"]
# Return only the generated text, without the input prompt
return response_data[0]["generated_text"].replace(prompt, "").strip() if response_data else "No recipe generated."
# Paso 3: Define la funci贸n principal para procesar la imagen y generar la receta
def process_image(image):
# Paso 3.1: Generar descripci贸n del plato
description = image_to_text(image)[0]['generated_text']
# Paso 3.2: Generar receta a partir de la descripci贸n
recipe = generate_recipe(description)
return description, recipe
# Paso 4: Crear la interfaz de Gradio
iface = gr.Interface(
fn=process_image,
inputs=gr.Image(type="pil"),
outputs=["text", "text"],
title="Recipe Generator from Dish Image",
description="Upload an image of a dish to get a description, ingredient list, and step-by-step recipe."
)
# Lanzar la aplicaci贸n de Gradio
iface.launch()