Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,10 @@ hf_token = os.getenv("HF_TOKEN")
|
|
9 |
# Paso 1: Cargar el modelo de image-to-text
|
10 |
image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
11 |
|
12 |
-
# Paso 2:
|
|
|
|
|
|
|
13 |
def generate_recipe(description):
|
14 |
url = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct"
|
15 |
headers = {"Authorization": f"Bearer {hf_token}"}
|
@@ -28,12 +31,12 @@ def generate_recipe(description):
|
|
28 |
payload = {
|
29 |
"inputs": prompt,
|
30 |
"parameters": {
|
31 |
-
"max_new_tokens": 1500,
|
32 |
-
"min_length": 500,
|
33 |
-
"temperature": 0.7,
|
34 |
-
"do_sample": True,
|
35 |
-
"repetition_penalty": 1.2,
|
36 |
-
"stop_sequences": ["\n\n"]
|
37 |
}
|
38 |
}
|
39 |
|
@@ -42,29 +45,41 @@ def generate_recipe(description):
|
|
42 |
|
43 |
# Manejo de posibles errores
|
44 |
if "error" in response_data:
|
45 |
-
return "Error
|
46 |
|
47 |
# Return only the generated text, without the input prompt
|
48 |
-
return response_data[0]["generated_text"].replace(prompt, "").strip() if response_data else "No
|
49 |
|
50 |
-
# Paso
|
51 |
def process_image(image):
|
52 |
-
# Paso
|
53 |
description = image_to_text(image)[0]['generated_text']
|
54 |
|
55 |
-
# Paso
|
|
|
|
|
|
|
56 |
recipe = generate_recipe(description)
|
57 |
|
58 |
-
|
|
|
|
|
|
|
59 |
|
60 |
-
# Paso
|
61 |
iface = gr.Interface(
|
62 |
fn=process_image,
|
63 |
inputs=gr.Image(type="pil"),
|
64 |
-
outputs=[
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
)
|
68 |
|
69 |
# Lanzar la aplicaci贸n de Gradio
|
70 |
-
iface.launch()
|
|
|
9 |
# Paso 1: Cargar el modelo de image-to-text
|
10 |
image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
11 |
|
12 |
+
# Paso 2: Cargar el modelo de traducci贸n a espa帽ol
|
13 |
+
translator = pipeline("translation_en_to_es", model="Helsinki-NLP/opus-mt-en-es")
|
14 |
+
|
15 |
+
# Paso 3: Funci贸n para llamar a la API de Qwen con el token seguro
|
16 |
def generate_recipe(description):
|
17 |
url = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct"
|
18 |
headers = {"Authorization": f"Bearer {hf_token}"}
|
|
|
31 |
payload = {
|
32 |
"inputs": prompt,
|
33 |
"parameters": {
|
34 |
+
"max_new_tokens": 1500,
|
35 |
+
"min_length": 500,
|
36 |
+
"temperature": 0.7,
|
37 |
+
"do_sample": True,
|
38 |
+
"repetition_penalty": 1.2,
|
39 |
+
"stop_sequences": ["\n\n"]
|
40 |
}
|
41 |
}
|
42 |
|
|
|
45 |
|
46 |
# Manejo de posibles errores
|
47 |
if "error" in response_data:
|
48 |
+
return "Error generando receta: " + response_data["error"]
|
49 |
|
50 |
# Return only the generated text, without the input prompt
|
51 |
+
return response_data[0]["generated_text"].replace(prompt, "").strip() if response_data else "No se pudo generar la receta."
|
52 |
|
53 |
+
# Paso 4: Define la funci贸n principal para procesar la imagen y generar la receta
|
54 |
def process_image(image):
|
55 |
+
# Paso 4.1: Generar descripci贸n del plato
|
56 |
description = image_to_text(image)[0]['generated_text']
|
57 |
|
58 |
+
# Paso 4.2: Traducir la descripci贸n al espa帽ol
|
59 |
+
description_es = translator(description)[0]['translation_text']
|
60 |
+
|
61 |
+
# Paso 4.3: Generar receta a partir de la descripci贸n
|
62 |
recipe = generate_recipe(description)
|
63 |
|
64 |
+
# Paso 4.4: Traducir la receta al espa帽ol
|
65 |
+
recipe_es = translator(recipe)[0]['translation_text']
|
66 |
+
|
67 |
+
return description_es, recipe_es
|
68 |
|
69 |
+
# Paso 5: Crear la interfaz de Gradio
|
70 |
iface = gr.Interface(
|
71 |
fn=process_image,
|
72 |
inputs=gr.Image(type="pil"),
|
73 |
+
outputs=[
|
74 |
+
gr.Textbox(label="Plato", lines=2),
|
75 |
+
gr.Textbox(label="Receta", lines=20)
|
76 |
+
],
|
77 |
+
title="Generador de Recetas a partir de Im谩genes",
|
78 |
+
description="Sube una imagen de un plato para obtener una descripci贸n, lista de ingredientes y pasos detallados para prepararlo.",
|
79 |
+
theme="default",
|
80 |
+
live=False,
|
81 |
+
allow_flagging="never"
|
82 |
)
|
83 |
|
84 |
# Lanzar la aplicaci贸n de Gradio
|
85 |
+
iface.launch()
|