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, "min_length": 500, "temperature": 0.7, "do_sample": True, "repetition_penalty": 1.2, "stop_sequences": ["\n\n"] } } response = requests.post(url, headers=headers, json=payload) response_data = response.json() if "error" in response_data: return "Error generating recipe: " + response_data["error"] 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): description = image_to_text(image)[0]['generated_text'] recipe = generate_recipe(description) # Guardar la receta en un archivo temporal recipe_file_path = "/tmp/recipe.md" with open(recipe_file_path, "w") as file: file.write(recipe) return description, recipe, recipe_file_path # Paso 4: Crear la interfaz de Gradio iface = gr.Interface( fn=process_image, inputs=gr.Image(type="pil"), outputs=[ gr.Textbox(label="Description"), gr.Text(label="Recipe"), gr.File(label="Download Recipe") # Botón para descargar el archivo ], 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()