daniescamilla commited on
Commit
363a5a2
verified
1 Parent(s): 54f9a3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -44
app.py CHANGED
@@ -1,44 +1,49 @@
1
- # Obt茅n el token desde las variables de entorno
2
- hf_token = os.getenv("HF_TOKEN")
3
-
4
- # Paso 1: Cargar el modelo de image-to-text
5
- image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
6
-
7
- # Paso 2: Funci贸n para llamar a la API de Qwen con el token seguro
8
- def generate_recipe(description):
9
- url = "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct"
10
- headers = {"Authorization": f"Bearer {hf_token}"}
11
- payload = {
12
- "inputs": f"Give me a list of ingredients and a step-by-step recipe for a dish described as: {description}",
13
- "parameters": {"max_length": 500}
14
- }
15
-
16
- response = requests.post(url, headers=headers, json=payload)
17
- response_data = response.json()
18
-
19
- # Manejo de posibles errores
20
- if "error" in response_data:
21
- return "Error generating recipe: " + response_data["error"]
22
- return response_data[0]["generated_text"] if response_data else "No recipe generated."
23
-
24
- # Paso 3: Define la funci贸n principal para procesar la imagen y generar la receta
25
- def process_image(image):
26
- # Paso 3.1: Generar descripci贸n del plato
27
- description = image_to_text(image)[0]['generated_text']
28
-
29
- # Paso 3.2: Generar receta a partir de la descripci贸n
30
- recipe = generate_recipe(description)
31
-
32
- return description, recipe
33
-
34
- # Paso 4: Crear la interfaz de Gradio
35
- iface = gr.Interface(
36
- fn=process_image,
37
- inputs=gr.Image(type="pil"),
38
- outputs=["text", "text"],
39
- title="Recipe Generator from Dish Image",
40
- description="Upload an image of a dish to get a description, ingredient list, and step-by-step recipe."
41
- )
42
-
43
- # Lanzar la aplicaci贸n de Gradio
44
- iface.launch()
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import requests
4
+ from transformers import pipeline
5
+
6
+ # Obt茅n el token desde las variables de entorno
7
+ hf_token = os.getenv("HF_TOKEN") # Esta l铆nea solo debe aparecer una vez
8
+
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: Funci贸n para llamar a la API de Qwen con el token seguro
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}"}
16
+ payload = {
17
+ "inputs": f"Give me a list of ingredients and a step-by-step recipe for a dish described as: {description}",
18
+ "parameters": {"max_length": 500}
19
+ }
20
+
21
+ response = requests.post(url, headers=headers, json=payload)
22
+ response_data = response.json()
23
+
24
+ # Manejo de posibles errores
25
+ if "error" in response_data:
26
+ return "Error generating recipe: " + response_data["error"]
27
+ return response_data[0]["generated_text"] if response_data else "No recipe generated."
28
+
29
+ # Paso 3: Define la funci贸n principal para procesar la imagen y generar la receta
30
+ def process_image(image):
31
+ # Paso 3.1: Generar descripci贸n del plato
32
+ description = image_to_text(image)[0]['generated_text']
33
+
34
+ # Paso 3.2: Generar receta a partir de la descripci贸n
35
+ recipe = generate_recipe(description)
36
+
37
+ return description, recipe
38
+
39
+ # Paso 4: Crear la interfaz de Gradio
40
+ iface = gr.Interface(
41
+ fn=process_image,
42
+ inputs=gr.Image(type="pil"),
43
+ outputs=["text", "text"],
44
+ title="Recipe Generator from Dish Image",
45
+ description="Upload an image of a dish to get a description, ingredient list, and step-by-step recipe."
46
+ )
47
+
48
+ # Lanzar la aplicaci贸n de Gradio
49
+ iface.launch()