Spaces:
Sleeping
Sleeping
File size: 2,367 Bytes
363a5a2 021e335 363a5a2 bf4b0df e2663ed bf4b0df e2663ed bf4b0df 363a5a2 e2663ed bf4b0df 363a5a2 021e335 e2663ed 363a5a2 |
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 |
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_length": 5000, # Maximum length for the response
"min_length": 2000 # Minimum length for the response
}
}
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()
|