Syrgak33 commited on
Commit
ac15a8a
·
verified ·
1 Parent(s): 4bd1a99

Update app.py

Browse files

Changed output type

Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -10,35 +10,42 @@ from Gradio_UI import GradioUI
10
  # A tool that fetches data from an API endpoint
11
  import qrcode
12
  import os
 
 
 
13
 
14
  @tool
15
  def generate_qr_code(data: str) -> str:
16
- """Generates a QR code from the given data and saves it as an image.
17
  Args:
18
  data: The text or URL to encode in the QR code.
19
  Returns:
20
- Filepath to the generated QR code image.
21
  """
22
  img = qrcode.make(data)
23
- img_path = "qrcode.png"
24
- img.save(img_path)
25
- return img_path
 
26
 
27
  @tool
28
  def generate_image_and_qr(prompt: str) -> dict:
29
- """Generates an image from a text prompt and creates a QR code linking to the saved image.
30
  Args:
31
  prompt: The text prompt for image generation.
32
  Returns:
33
- A dictionary containing the image and QR code file paths.
34
  """
35
  try:
36
- image_path = "generated_image.png"
37
  image = image_generation_tool(prompt=prompt)
38
- image.save(image_path)
39
 
40
- qr_code_path = generate_qr_code(image_path)
41
- return {"image": image_path, "qr_code": qr_code_path}
 
 
 
 
 
42
  except Exception as e:
43
  return {"error": str(e)}
44
 
@@ -59,9 +66,6 @@ def get_current_time_in_timezone(timezone: str) -> str:
59
 
60
  final_answer = FinalAnswerTool()
61
 
62
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
63
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
64
-
65
  model = HfApiModel(
66
  max_tokens=2096,
67
  temperature=0.5,
@@ -87,4 +91,4 @@ agent = CodeAgent(
87
  prompt_templates=prompt_templates
88
  )
89
 
90
- GradioUI(agent).launch()
 
10
  # A tool that fetches data from an API endpoint
11
  import qrcode
12
  import os
13
+ from PIL import Image
14
+ import io
15
+ import base64
16
 
17
  @tool
18
  def generate_qr_code(data: str) -> str:
19
+ """Generates a QR code from the given data and returns it as a base64 string.
20
  Args:
21
  data: The text or URL to encode in the QR code.
22
  Returns:
23
+ Base64 encoded QR code image.
24
  """
25
  img = qrcode.make(data)
26
+ buffered = io.BytesIO()
27
+ img.save(buffered, format="PNG")
28
+ img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
29
+ return img_str
30
 
31
  @tool
32
  def generate_image_and_qr(prompt: str) -> dict:
33
+ """Generates an image from a text prompt and returns both the image and QR code as base64 strings.
34
  Args:
35
  prompt: The text prompt for image generation.
36
  Returns:
37
+ A dictionary containing base64 encoded image and QR code.
38
  """
39
  try:
 
40
  image = image_generation_tool(prompt=prompt)
 
41
 
42
+ buffered = io.BytesIO()
43
+ image.save(buffered, format="PNG")
44
+ image_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
45
+
46
+ qr_code_str = generate_qr_code(image_str)
47
+
48
+ return {"image": image_str, "qr_code": qr_code_str}
49
  except Exception as e:
50
  return {"error": str(e)}
51
 
 
66
 
67
  final_answer = FinalAnswerTool()
68
 
 
 
 
69
  model = HfApiModel(
70
  max_tokens=2096,
71
  temperature=0.5,
 
91
  prompt_templates=prompt_templates
92
  )
93
 
94
+ GradioUI(agent).launch()