Mikasa06 commited on
Commit
5da4258
·
verified ·
1 Parent(s): d35ba49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -6,6 +6,9 @@ import yaml
6
  from tools.final_answer import FinalAnswerTool
7
  import matplotlib.pyplot as plt
8
  import io
 
 
 
9
 
10
  from Gradio_UI import GradioUI
11
 
@@ -36,7 +39,7 @@ def get_current_time_in_timezone(timezone: str) -> str:
36
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
37
 
38
  @tool
39
- def generate_bar_chart(categories: list[str], values: list[int]) -> bytes:
40
  """A tool that generates a bar chart from given categories and values, and returns it as an image.
41
 
42
  Args:
@@ -44,7 +47,7 @@ def generate_bar_chart(categories: list[str], values: list[int]) -> bytes:
44
  values: A list of integers or floats representing values (e.g., [10, 20, 15]).
45
 
46
  Returns:
47
- Image data in bytes format.
48
  """
49
  try:
50
  if len(categories) != len(values):
@@ -56,7 +59,7 @@ def generate_bar_chart(categories: list[str], values: list[int]) -> bytes:
56
  # Add labels and title
57
  plt.xlabel('Categories')
58
  plt.ylabel('Values')
59
- plt.title('Bar Graph')
60
 
61
  # Save the plot to a BytesIO buffer
62
  img_buffer = io.BytesIO()
@@ -65,10 +68,13 @@ def generate_bar_chart(categories: list[str], values: list[int]) -> bytes:
65
 
66
  # Get image bytes
67
  img_buffer.seek(0)
68
- return img_buffer.getvalue()
 
 
 
69
 
70
  except Exception as e:
71
- return f"Error generating bar chart: {str(e)}".encode()
72
 
73
 
74
 
 
6
  from tools.final_answer import FinalAnswerTool
7
  import matplotlib.pyplot as plt
8
  import io
9
+ from PIL import Image as PILImage
10
+ from IPython.display import display, Image
11
+
12
 
13
  from Gradio_UI import GradioUI
14
 
 
39
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
40
 
41
  @tool
42
+ def generate_bar_chart(categories: list[str], values: list[int]) -> PILImage:
43
  """A tool that generates a bar chart from given categories and values, and returns it as an image.
44
 
45
  Args:
 
47
  values: A list of integers or floats representing values (e.g., [10, 20, 15]).
48
 
49
  Returns:
50
+ An image object containing the bar chart.
51
  """
52
  try:
53
  if len(categories) != len(values):
 
59
  # Add labels and title
60
  plt.xlabel('Categories')
61
  plt.ylabel('Values')
62
+ plt.title('User-Generated Bar Graph')
63
 
64
  # Save the plot to a BytesIO buffer
65
  img_buffer = io.BytesIO()
 
68
 
69
  # Get image bytes
70
  img_buffer.seek(0)
71
+
72
+ # Open the image and return as PIL Image object
73
+ img = PILImage.open(img_buffer)
74
+ return img
75
 
76
  except Exception as e:
77
+ print(f"Error generating bar chart: {str(e)}")
78
 
79
 
80