jseb4s commited on
Commit
a45fcc8
·
verified ·
1 Parent(s): 3a28ef1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -10
app.py CHANGED
@@ -9,16 +9,20 @@ import gradio as gr
9
  from PIL import Image
10
  import tempfile
11
  import os
 
 
12
 
13
  # Import tool from Hub
14
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
15
 
16
  ##############
17
  @tool
18
- def get_game_of_thrones_character_info(character_name: str) -> str:
19
  """A tool that fetches information about a character from Game of Thrones, including their house and a generated image.
20
  Args:
21
  character_name: The name of the character (e.g., 'Jon Snow').
 
 
22
  """
23
  base_url = "https://anapioficeandfire.com/api/characters"
24
 
@@ -30,7 +34,7 @@ def get_game_of_thrones_character_info(character_name: str) -> str:
30
  characters = response.json()
31
 
32
  if not characters:
33
- return f"No se encontró información para el personaje: {character_name}"
34
 
35
  # Tomar el primer resultado (puede haber varios con el mismo nombre)
36
  character = characters[0]
@@ -65,10 +69,10 @@ def get_game_of_thrones_character_info(character_name: str) -> str:
65
  # Generar la imagen usando la herramienta de generación de imágenes
66
  image = image_generation_tool(image_prompt)
67
 
68
- # Guardar la imagen en un archivo temporal
69
- with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
70
- image.save(temp_file, format="JPEG")
71
- temp_file_path = temp_file.name
72
 
73
  # Formatear la respuesta
74
  result = f"Información sobre {name}:\n"
@@ -82,12 +86,11 @@ def get_game_of_thrones_character_info(character_name: str) -> str:
82
  result += f"- House(s): {', '.join(houses)}\n"
83
  else:
84
  result += "- Casa(s): Ninguna o desconocida\n"
85
- result += f"- Imagen generada: {temp_file_path}\n"
86
 
87
- return result
88
 
89
  except Exception as e:
90
- return f"Error al obtener información del personaje: {str(e)}"
91
 
92
  ###############
93
 
@@ -119,4 +122,18 @@ agent = CodeAgent(
119
  )
120
 
121
  ##############
122
- GradioUI(agent).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  from PIL import Image
10
  import tempfile
11
  import os
12
+ import base64
13
+ from io import BytesIO
14
 
15
  # Import tool from Hub
16
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
17
 
18
  ##############
19
  @tool
20
+ def get_game_of_thrones_character_info(character_name: str) -> dict:
21
  """A tool that fetches information about a character from Game of Thrones, including their house and a generated image.
22
  Args:
23
  character_name: The name of the character (e.g., 'Jon Snow').
24
+ Returns:
25
+ A dictionary containing the character info and the base64-encoded image.
26
  """
27
  base_url = "https://anapioficeandfire.com/api/characters"
28
 
 
34
  characters = response.json()
35
 
36
  if not characters:
37
+ return {"error": f"No se encontró información para el personaje: {character_name}"}
38
 
39
  # Tomar el primer resultado (puede haber varios con el mismo nombre)
40
  character = characters[0]
 
69
  # Generar la imagen usando la herramienta de generación de imágenes
70
  image = image_generation_tool(image_prompt)
71
 
72
+ # Convertir la imagen a base64
73
+ buffered = BytesIO()
74
+ image.save(buffered, format="JPEG")
75
+ img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
76
 
77
  # Formatear la respuesta
78
  result = f"Información sobre {name}:\n"
 
86
  result += f"- House(s): {', '.join(houses)}\n"
87
  else:
88
  result += "- Casa(s): Ninguna o desconocida\n"
 
89
 
90
+ return {"info": result, "image": f"data:image/jpeg;base64,{img_base64}"}
91
 
92
  except Exception as e:
93
+ return {"error": f"Error al obtener información del personaje: {str(e)}"}
94
 
95
  ###############
96
 
 
122
  )
123
 
124
  ##############
125
+ def display_character_info(character_name):
126
+ result = get_game_of_thrones_character_info(character_name)
127
+ if "error" in result:
128
+ return result["error"], None
129
+ return result["info"], result["image"]
130
+
131
+ interface = gr.Interface(
132
+ fn=display_character_info,
133
+ inputs="text",
134
+ outputs=["text", "image"],
135
+ title="Información de Personajes de Juego de Tronos",
136
+ description="Ingresa el nombre de un personaje de Juego de Tronos para obtener información y una imagen generada."
137
+ )
138
+
139
+ interface.launch()