sunholo 0.100.1__py3-none-any.whl → 0.100.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sunholo/auth/refresh.py CHANGED
@@ -14,7 +14,7 @@ def get_default_email():
14
14
 
15
15
  if gcs_credentials is None:
16
16
  log.error("Could not refresh the credentials properly.")
17
- return None
17
+ return None, None
18
18
 
19
19
  service_account_email = getattr(gcs_credentials, 'service_account_email', None)
20
20
  # If you use a service account credential, you can use the embedded email
@@ -23,7 +23,7 @@ def get_default_email():
23
23
  if not service_account_email:
24
24
  log.error("Could not create the credentials for signed requests - no credentials.service_account_email or GCS_MAIL_USER with roles/iam.serviceAccountTokenCreator")
25
25
 
26
- return None
26
+ return None, None
27
27
 
28
28
  log.info(f"Found default email: {service_account_email=} for {project_id=}")
29
29
  return service_account_email, gcs_credentials
sunholo/gcs/add_file.py CHANGED
@@ -182,8 +182,6 @@ def add_file_to_gcs(filename: str,
182
182
 
183
183
  blob.metadata = the_metadata
184
184
 
185
-
186
-
187
185
  max_retries = 5
188
186
  base_delay = 1 # 1 second
189
187
  for attempt in range(max_retries):
@@ -106,7 +106,14 @@ def sign_gcs_url(bucket_name:str, object_name:str, expiry_secs:int = 86400) -> O
106
106
  Returns:
107
107
  str: The signed URL or None if not avialable
108
108
  """
109
- service_account_email, gcs_credentials = get_default_email()
109
+ result = get_default_email()
110
+
111
+ # Check if the result is None
112
+ if result is None or any(item is None for item in result):
113
+ log.error("Failed to retrieve the service account email and credentials.")
114
+ return None
115
+
116
+ service_account_email, gcs_credentials = result
110
117
 
111
118
  expires = datetime.now() + timedelta(seconds=expiry_secs)
112
119
 
@@ -262,8 +262,7 @@ class GenAIFunctionProcessor:
262
262
  # Execute the function with the provided parameters
263
263
  result = fn_exec(**params_obj)
264
264
  log.info(f"Got result from {function_name}: {result} of type: {type(result)}")
265
- if not isinstance(result, str):
266
- log.warning(f"Tool functions should return strings: {function_name} returned type: {type(result)}")
265
+ #TODO: return images
267
266
  else:
268
267
  fn_result = type(fn).to_dict(fn)
269
268
  result = fn_result.get("result")
@@ -1,9 +1,12 @@
1
1
  import asyncio
2
- from ..custom_logging import log
3
- import traceback
4
2
  from typing import Callable, Any, AsyncGenerator, Dict
3
+ import time
4
+ import traceback
5
+ from ..custom_logging import setup_logging
5
6
  from tenacity import AsyncRetrying, retry_if_exception_type, wait_random_exponential, stop_after_attempt
6
7
 
8
+ log = setup_logging("sunholo_AsyncTaskRunner")
9
+
7
10
  class AsyncTaskRunner:
8
11
  def __init__(self, retry_enabled=False, retry_kwargs=None):
9
12
  self.tasks = []
@@ -11,67 +14,94 @@ class AsyncTaskRunner:
11
14
  self.retry_kwargs = retry_kwargs or {}
12
15
 
13
16
  def add_task(self, func: Callable[..., Any], *args: Any):
14
- """Adds a task to the list of tasks to be executed."""
17
+ """
18
+ Adds a task to the list of tasks to be executed.
19
+ """
15
20
  log.info(f"Adding task: {func.__name__} with args: {args}")
16
21
  self.tasks.append((func.__name__, func, args))
17
22
 
18
23
  async def run_async_as_completed(self) -> AsyncGenerator[Dict[str, Any], None]:
19
24
  """
20
- Runs all tasks concurrently and yields results and heartbeats as they are produced.
25
+ Runs all tasks concurrently and yields results as they complete,
26
+ while periodically sending heartbeat messages.
21
27
  """
22
- log.info("Running tasks asynchronously and yielding results and heartbeats as they occur")
28
+ log.info("Running tasks asynchronously and yielding results as they complete")
29
+
30
+ # Create a queue for inter-coroutine communication
23
31
  queue = asyncio.Queue()
24
- tasks = {}
25
- completed_tasks = set()
26
32
 
33
+ # List to keep track of all running tasks and their heartbeats
34
+ task_infos = []
35
+
36
+ # Start all tasks and their corresponding heartbeats
27
37
  for name, func, args in self.tasks:
28
- coro = self._task_wrapper(name, func, args, queue)
29
- task = asyncio.create_task(coro)
30
- tasks[task] = name
31
-
32
- while tasks or not queue.empty():
33
- if not queue.empty():
34
- message = await queue.get()
35
- log.info(f"Found queue message: {message}")
36
- # Ignore heartbeats from completed tasks
37
- if message['type'] == 'heartbeat' and message['func_name'] in completed_tasks:
38
- continue
39
- yield message
40
- else:
41
- done, _ = await asyncio.wait(
42
- list(tasks.keys()),
43
- timeout=0.1,
44
- return_when=asyncio.FIRST_COMPLETED
45
- )
46
- for task in done:
47
- name = tasks.pop(task)
48
- completed_tasks.add(name)
49
- try:
50
- result = await task
51
- await queue.put({'type': 'task_complete', 'func_name': name, 'result': result})
52
- except Exception as e:
53
- log.error(f"Task {name} resulted in an error: {e}\n{traceback.format_exc()}")
54
- await queue.put({'type': 'task_error', 'func_name': name, 'error': e})
38
+ # Create an event to signal task completion to the heartbeat
39
+ completion_event = asyncio.Event()
40
+
41
+ # Start the main task with retries
42
+ task_coro = self._run_with_retries(name, func, *args, queue=queue, completion_event=completion_event)
43
+ task = asyncio.create_task(task_coro)
55
44
 
56
- # Process any remaining messages in the queue
57
- while not queue.empty():
45
+ # Start the heartbeat coroutine
46
+ heartbeat_coro = self._send_heartbeat(name, completion_event, queue)
47
+ heartbeat_task = asyncio.create_task(heartbeat_coro)
48
+
49
+ # Store task information for management
50
+ task_infos.append({
51
+ 'name': name,
52
+ 'task': task,
53
+ 'heartbeat_task': heartbeat_task,
54
+ 'completion_event': completion_event
55
+ })
56
+
57
+ log.info(f"Started task '{name}' and its heartbeat")
58
+
59
+ log.info(f"Started async run with {len(self.tasks)} tasks and heartbeats")
60
+
61
+ # Create a monitor task to detect when all tasks and heartbeats are done
62
+ monitor = asyncio.create_task(self._monitor_tasks(task_infos, queue))
63
+
64
+ # Continuously yield messages from the queue until sentinel is received
65
+ while True:
58
66
  message = await queue.get()
59
- log.info(f"Found queue message: {message}")
60
- if message['type'] == 'heartbeat' and message['func_name'] in completed_tasks:
61
- continue
67
+ if message is None:
68
+ log.info("Received sentinel. Exiting message loop.")
69
+ break # Sentinel received, all tasks and heartbeats are done
70
+ log.info(f"Received message from queue: {message}")
62
71
  yield message
63
72
 
64
- async def _task_wrapper(self, name: str, func: Callable[..., Any], args: Any, queue: asyncio.Queue) -> Any:
65
- """Wraps the task function to process its output and handle retries, while managing heartbeat updates."""
66
- async def run_func():
67
- if asyncio.iscoroutinefunction(func):
68
- return await func(*args)
69
- else:
70
- return await asyncio.to_thread(func, *args)
73
+ # Wait for the monitor to finish
74
+ await monitor
75
+
76
+ log.info("All tasks and heartbeats have completed")
71
77
 
72
- # Start the heartbeat task
73
- heartbeat_task = asyncio.create_task(self._send_heartbeat(queue, name))
78
+ async def _monitor_tasks(self, task_infos, queue):
79
+ """
80
+ Monitors the tasks and heartbeats, and sends a sentinel to the queue when done.
81
+ """
82
+ # Wait for all main tasks to complete
83
+ main_tasks = [info['task'] for info in task_infos]
84
+ log.info("Monitor: Waiting for all main tasks to complete")
85
+ await asyncio.gather(*main_tasks, return_exceptions=True)
86
+ log.info("Monitor: All main tasks have completed")
74
87
 
88
+ # Cancel all heartbeat tasks
89
+ for info in task_infos:
90
+ info['heartbeat_task'].cancel()
91
+ try:
92
+ await info['heartbeat_task']
93
+ except asyncio.CancelledError:
94
+ pass
95
+ log.info(f"Monitor: Heartbeat for task '{info['name']}' has been canceled")
96
+
97
+ # Send a sentinel to indicate completion
98
+ await queue.put(None)
99
+ log.info("Monitor: Sent sentinel to queue")
100
+
101
+ async def _run_with_retries(self, name: str, func: Callable[..., Any], *args: Any, queue: asyncio.Queue, completion_event: asyncio.Event) -> None:
102
+ """
103
+ Executes a task with optional retries and sends completion or error messages to the queue.
104
+ """
75
105
  try:
76
106
  if self.retry_enabled:
77
107
  retry_kwargs = {
@@ -82,57 +112,67 @@ class AsyncTaskRunner:
82
112
  }
83
113
  async for attempt in AsyncRetrying(**retry_kwargs):
84
114
  with attempt:
85
- return await run_func()
115
+ log.info(f"Starting task '{name}' with retry")
116
+ result = await self._execute_task(func, *args)
117
+ await queue.put({'type': 'task_complete', 'func_name': name, 'result': result})
118
+ log.info(f"Sent 'task_complete' message for task '{name}'")
119
+ return
86
120
  else:
87
- try:
88
- return await run_func()
89
- except Exception as e:
90
- log.error(f"Error in task {name}: {e}\n{traceback.format_exc()}")
91
- raise
121
+ log.info(f"Starting task '{name}' with no retry")
122
+ result = await self._execute_task(func, *args)
123
+ await queue.put({'type': 'task_complete', 'func_name': name, 'result': result})
124
+ log.info(f"Sent 'task_complete' message for task '{name}'")
125
+ except Exception as e:
126
+ log.error(f"Error in task '{name}': {e}\n{traceback.format_exc()}")
127
+ await queue.put({'type': 'task_error', 'func_name': name, 'error': f'{e}\n{traceback.format_exc()}'})
128
+ log.info(f"Sent 'task_error' message for task '{name}'")
92
129
  finally:
93
- # Stop the heartbeat task
94
- heartbeat_task.cancel()
95
- # Wait for the heartbeat task to finish
96
- try:
97
- await heartbeat_task
98
- except asyncio.CancelledError:
99
- pass
130
+ log.info(f"Task '{name}' completed.")
131
+ # Set the completion event after sending the message
132
+ completion_event.set()
100
133
 
101
- async def _send_heartbeat(self, queue: asyncio.Queue, func_name: str, interval=2):
102
- """
103
- Sends a periodic heartbeat to keep the task alive and update the spinner with elapsed time.
134
+ async def _execute_task(self, func: Callable[..., Any], *args: Any) -> Any:
104
135
  """
105
- # Send the initial spinner HTML
106
- spinner_html = (
107
- f'<div id="{func_name}-spinner" class="spinner-container">'
108
- f' <div class="spinner"></div>'
109
- f' <span class="elapsed-time">Task {func_name} is still running... 0s elapsed</span>'
110
- f'</div>'
111
- )
112
- log.info(f"Heartbeat started for task {func_name}")
136
+ Executes the given task function and returns its result.
113
137
 
114
- await queue.put({'type': 'heartbeat', 'func_name': func_name, 'token': spinner_html})
138
+ Args:
139
+ func (Callable): The callable to execute.
140
+ *args: Arguments to pass to the callable.
115
141
 
116
- # Keep track of elapsed time
117
- elapsed_time = 0
142
+ Returns:
143
+ Any: The result of the task.
144
+ """
145
+ if asyncio.iscoroutinefunction(func):
146
+ return await func(*args)
147
+ else:
148
+ return await asyncio.to_thread(func, *args)
118
149
 
150
+ async def _send_heartbeat(self, func_name: str, completion_event: asyncio.Event, queue: asyncio.Queue, interval: int = 2):
151
+ """
152
+ Sends periodic heartbeat updates to indicate the task is still in progress.
153
+
154
+ Args:
155
+ func_name (str): The name of the task function.
156
+ completion_event (asyncio.Event): Event to signal when the task is completed.
157
+ queue (asyncio.Queue): The queue to send heartbeat messages to.
158
+ interval (int): How frequently to send heartbeat messages (in seconds).
159
+ """
160
+ start_time = time.time()
161
+ log.info(f"Starting heartbeat for task '{func_name}' with interval {interval} seconds")
119
162
  try:
120
- while True:
121
- await asyncio.sleep(interval) # Sleep for the interval
122
- elapsed_time += interval # Increment elapsed time
123
- log.info(f"Sending heartbeat for {func_name}: {elapsed_time}s elapsed")
124
- # Update spinner with the elapsed time
125
- update_html = (
126
- f'<div style="display: none;" data-update-id="{func_name}-spinner">'
127
- f'<span class="elapsed-time">Task {func_name} is still running... {elapsed_time}s elapsed</span>'
128
- f'</div>'
129
- )
130
- await queue.put({'type': 'heartbeat', 'func_name': func_name, 'token': update_html})
163
+ while not completion_event.is_set():
164
+ await asyncio.sleep(interval)
165
+ elapsed_time = int(time.time() - start_time)
166
+ heartbeat_message = {
167
+ 'type': 'heartbeat',
168
+ 'name': func_name,
169
+ 'interval': interval,
170
+ 'elapsed_time': elapsed_time
171
+ }
172
+ log.info(f"Sending heartbeat for task '{func_name}', running for {elapsed_time} seconds")
173
+ await queue.put(heartbeat_message)
131
174
  except asyncio.CancelledError:
132
- log.info(f"Heartbeat task for {func_name} has been cancelled.")
175
+ log.info(f"Heartbeat for task '{func_name}' has been canceled")
133
176
  finally:
134
- # Send a message indicating task completion to update the spinner's state
135
- completion_html = (
136
- f'<div style="display: none;" data-complete-id="{func_name}-spinner"></div>'
137
- )
138
- await queue.put({'type': 'heartbeat', 'func_name': func_name, 'token': completion_html})
177
+ log.info(f"Heartbeat for task '{func_name}' stopped")
178
+
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sunholo
3
- Version: 0.100.1
3
+ Version: 0.100.3
4
4
  Summary: Large Language Model DevOps - a package to help deploy LLMs to the Cloud.
5
5
  Home-page: https://github.com/sunholo-data/sunholo-py
6
- Download-URL: https://github.com/sunholo-data/sunholo-py/archive/refs/tags/v0.100.1.tar.gz
6
+ Download-URL: https://github.com/sunholo-data/sunholo-py/archive/refs/tags/v0.100.3.tar.gz
7
7
  Author: Holosun ApS
8
8
  Author-email: multivac@sunholo.com
9
9
  License: Apache License, Version 2.0
@@ -19,7 +19,7 @@ sunholo/archive/__init__.py,sha256=qNHWm5rGPVOlxZBZCpA1wTYPbalizRT7f8X4rs2t290,3
19
19
  sunholo/archive/archive.py,sha256=PxVfDtO2_2ZEEbnhXSCbXLdeoHoQVImo4y3Jr2XkCFY,1204
20
20
  sunholo/auth/__init__.py,sha256=TeP-OY0XGxYV_8AQcVGoh35bvyWhNUcMRfhuD5l44Sk,91
21
21
  sunholo/auth/gcloud.py,sha256=PdbwkuTdRi4RKBmgG9uwsReegqC4VG15_tw5uzmA7Fs,298
22
- sunholo/auth/refresh.py,sha256=6AEWX87G3I9BCqrgGJjHGrrWABBXHuaGDKU9ZEcVeXM,2017
22
+ sunholo/auth/refresh.py,sha256=WSlKa8TQ70GlZ2e0u83nYknhUsgTeiyyTVi-EFOa8Uc,2029
23
23
  sunholo/auth/run.py,sha256=pMSp2lzL6e6ZqlltVUH92bkeUt341yMue027qrE0jQU,2821
24
24
  sunholo/azure/__init__.py,sha256=S1WQ5jndzNgzhSBh9UpX_yw7hRVm3hCzkAWNxUdK4dA,48
25
25
  sunholo/azure/auth.py,sha256=Y3fDqFLYwbsIyi5hS5L-3hYnwrLWVL96yPng5Sj5c2c,2236
@@ -80,16 +80,16 @@ sunholo/embedder/embed_chunk.py,sha256=_FdO4-9frUJlDPqXv2Ai49ejUrrMTMGti3D7bfJGf
80
80
  sunholo/excel/__init__.py,sha256=AqTMN9K4qJYi4maEgoORc5oxDVGO_eqmwzDaVP37JgY,56
81
81
  sunholo/excel/plugin.py,sha256=rl3FoECZ6Ts8KKExPrbPwr3u3CegZfsevmcjgUXAlhE,4033
82
82
  sunholo/gcs/__init__.py,sha256=SZvbsMFDko40sIRHTHppA37IijvJTae54vrhooEF5-4,90
83
- sunholo/gcs/add_file.py,sha256=XuntzdFz9b0flMArgUe-4DSaPI7zdBw8hlsG1rsRQmc,8371
83
+ sunholo/gcs/add_file.py,sha256=NxUaWTP-h9l5fbjnrEYpipo5rjS3uPkjMWeDhpK113g,8369
84
84
  sunholo/gcs/download_folder.py,sha256=ijJTnS595JqZhBH8iHFErQilMbkuKgL-bnTCMLGuvlA,1614
85
- sunholo/gcs/download_url.py,sha256=q1NiJSvEhdNrmU5ZJ-sBCMC_J5CxzrajY8LRgdPOV_M,6130
85
+ sunholo/gcs/download_url.py,sha256=oPC8phesRcpXB4zB_p0nWPKPBMs7JBhhVJ7tnVYhzD8,6356
86
86
  sunholo/gcs/metadata.py,sha256=oQLcXi4brsZ74aegWyC1JZmhlaEV270HS5_UWtAYYWE,898
87
87
  sunholo/genai/__init__.py,sha256=dBl6IA3-Fx6-Vx81r0XqxHlUq6WeW1iDX188dpChu8s,115
88
88
  sunholo/genai/init.py,sha256=yG8E67TduFCTQPELo83OJuWfjwTnGZsyACospahyEaY,687
89
- sunholo/genai/process_funcs_cls.py,sha256=MF3wm-N-zoYvme4I8ffXM9I7cog8OFyBnLu1e3A6eVc,26695
89
+ sunholo/genai/process_funcs_cls.py,sha256=DPe70E71pofInLMFBFdudFZr0ZCFHN1LFCQjpxtG8xU,26552
90
90
  sunholo/genai/safety.py,sha256=mkFDO_BeEgiKjQd9o2I4UxB6XI7a9U-oOFjZ8LGRUC4,1238
91
91
  sunholo/invoke/__init__.py,sha256=o1RhwBGOtVK0MIdD55fAIMCkJsxTksi8GD5uoqVKI-8,184
92
- sunholo/invoke/async_class.py,sha256=TbYzdS2RDu1WhbYcq6wt7GTJzrtk-7Y3I-2AbJAD1Ik,6134
92
+ sunholo/invoke/async_class.py,sha256=Wkrv3exjN3aDQdI1Q4tv8I8xSloKgdz_LzGomayfLhw,7734
93
93
  sunholo/invoke/direct_vac_func.py,sha256=GXSCMkC6vOWGUtQjxy-ZpTrMvJa3CgcW-y9mDpJwWC8,9533
94
94
  sunholo/invoke/invoke_vac_utils.py,sha256=sJc1edHTHMzMGXjji1N67c3iUaP7BmAL5nj82Qof63M,2053
95
95
  sunholo/langfuse/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -144,9 +144,9 @@ sunholo/vertex/init.py,sha256=1OQwcPBKZYBTDPdyU7IM4X4OmiXLdsNV30C-fee2scQ,2875
144
144
  sunholo/vertex/memory_tools.py,sha256=tBZxqVZ4InTmdBvLlOYwoSEWu4-kGquc-gxDwZCC4FA,7667
145
145
  sunholo/vertex/safety.py,sha256=S9PgQT1O_BQAkcqauWncRJaydiP8Q_Jzmu9gxYfy1VA,2482
146
146
  sunholo/vertex/type_dict_to_json.py,sha256=uTzL4o9tJRao4u-gJOFcACgWGkBOtqACmb6ihvCErL8,4694
147
- sunholo-0.100.1.dist-info/LICENSE.txt,sha256=SdE3QjnD3GEmqqg9EX3TM9f7WmtOzqS1KJve8rhbYmU,11345
148
- sunholo-0.100.1.dist-info/METADATA,sha256=dFU8BBZlnOGZpfnkvOiQ-_yO3Hs12mNi0BFWnVQh9QQ,8312
149
- sunholo-0.100.1.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
150
- sunholo-0.100.1.dist-info/entry_points.txt,sha256=bZuN5AIHingMPt4Ro1b_T-FnQvZ3teBes-3OyO0asl4,49
151
- sunholo-0.100.1.dist-info/top_level.txt,sha256=wt5tadn5--5JrZsjJz2LceoUvcrIvxjHJe-RxuudxAk,8
152
- sunholo-0.100.1.dist-info/RECORD,,
147
+ sunholo-0.100.3.dist-info/LICENSE.txt,sha256=SdE3QjnD3GEmqqg9EX3TM9f7WmtOzqS1KJve8rhbYmU,11345
148
+ sunholo-0.100.3.dist-info/METADATA,sha256=c5y87oJoR39hmBJfM3dGVuHKI1e2A5ZJhTyrFaTOSbQ,8312
149
+ sunholo-0.100.3.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
150
+ sunholo-0.100.3.dist-info/entry_points.txt,sha256=bZuN5AIHingMPt4Ro1b_T-FnQvZ3teBes-3OyO0asl4,49
151
+ sunholo-0.100.3.dist-info/top_level.txt,sha256=wt5tadn5--5JrZsjJz2LceoUvcrIvxjHJe-RxuudxAk,8
152
+ sunholo-0.100.3.dist-info/RECORD,,