staedi commited on
Commit
dcac90b
·
verified ·
1 Parent(s): b6d04d1

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +138 -0
utils.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import os
2
+ # import glob
3
+ import asyncio
4
+ from ast import literal_eval
5
+ # import streamlit as st
6
+ import re
7
+
8
+ # def get_filename():
9
+ # # Return the current filename
10
+ # return os.path.realpath('__').split('/')[-1]
11
+
12
+ # def set_page_meta(path):
13
+ # # Set title
14
+ # title = path[path.rfind('/')+1:path.rfind('.py')].title().replace('_','')
15
+
16
+ # # Remove emoji from the title
17
+ # title = re.sub(r'[^a-zA-Z]','',title)
18
+
19
+ # # Generate the Page properties
20
+ # return st.Page(path,title=title)
21
+
22
+ # # Filter out unavailable framework (i.e., smolagents or llamaindex)
23
+ # def filter_framework():
24
+ # framework_filter = []
25
+
26
+ # try:
27
+ # import smolagents
28
+
29
+ # except:
30
+ # framework_filter.append('smolagents')
31
+ # try:
32
+ # import llama_index
33
+ # except:
34
+ # framework_filter.append('llamaindex')
35
+
36
+ # finally:
37
+ # try:
38
+ # import llama_index
39
+ # except:
40
+ # framework_filter.append('llamaindex')
41
+
42
+ # return framework_filter
43
+
44
+ # def get_paths(root:str = 'pages'):
45
+ # # Get the path structure (dir/file)
46
+ # paths = glob.glob(f'{root}/**/*.py',recursive=True)
47
+
48
+ # # Available framework only
49
+ # framework_filter = filter_framework()
50
+
51
+ # paths = [path for path in paths for framework in framework_filter if path.find(framework)==-1]
52
+
53
+ # # Prepare key for each navigation
54
+ # keys = [path[path.rfind('/')+1:path.rfind('.py')] if '/' not in path[path.find('/')+1:] else path[path.find('/')+1:path.rfind('/')] for path in paths]
55
+ # # Replace emoji in the Category
56
+ # keys = [re.sub(r'[^a-zA-Z]','',key) for key in keys]
57
+
58
+ # # Initialize dictionary
59
+ # page_dict = {key:[] for key in keys}
60
+
61
+ # # Populate the dictionary
62
+ # for key, path in zip(keys, paths):
63
+ # page_dict.get(key).append(set_page_meta(path))
64
+
65
+ # # Convert key to title()
66
+ # pages = {key.title():value for key, value in page_dict.items()}
67
+
68
+ # return pages
69
+
70
+ # # Validate if the tool/agent (namely, work) has been created
71
+ # def validate_agent(framework:str,work_type=None,task_type=None) -> bool:
72
+ # status = False
73
+
74
+ # if work_type and task_type:
75
+ # if 'agent' in st.session_state and \
76
+ # st.session_state.agent['framework'] == framework and \
77
+ # st.session_state.agent['work_type'] == work_type and \
78
+ # st.session_state.agent['task_type'] == task_type and \
79
+ # st.session_state.agent['agent']:
80
+ # status = True
81
+
82
+ # # task_type (tools) not specified
83
+ # elif work_type:
84
+ # if 'agent' in st.session_state and \
85
+ # st.session_state.agent['framework'] == framework and \
86
+ # st.session_state.agent['work_type'] == work_type and \
87
+ # st.session_state.agent['agent']:
88
+ # status = True
89
+
90
+ # else:
91
+ # if 'agent' in st.session_state and \
92
+ # st.session_state.agent['framework'] == framework and \
93
+ # st.session_state.agent['agent']:
94
+ # status = True
95
+
96
+ # return status
97
+
98
+
99
+ # Convert the type-safe conversion
100
+ def check_value(input):
101
+ return literal_eval(input)
102
+
103
+
104
+ # Silence the torch error
105
+ def init_async():
106
+ import torch
107
+ torch.classes.__path__ = [] # add this line to manually set it to empty.
108
+
109
+
110
+ def run_async_task(async_func, *args):
111
+ """
112
+ Run an asynchronous function in a new event loop.
113
+
114
+ Args:
115
+ async_func (coroutine): The asynchronous function to execute.
116
+ *args: Arguments to pass to the asynchronous function.
117
+
118
+ Returns:
119
+ None
120
+ """
121
+
122
+ loop = None
123
+
124
+ try:
125
+ loop = asyncio.new_event_loop()
126
+ loop.run_until_complete(async_func(*args))
127
+ except:
128
+ # Close the existing loop if open
129
+ if loop is not None:
130
+ loop.close()
131
+
132
+ # Create a new loop for retry
133
+ loop = asyncio.new_event_loop()
134
+
135
+ loop.run_until_complete(async_func(*args))
136
+ finally:
137
+ if loop is not None:
138
+ loop.close()