File size: 1,343 Bytes
bde8a90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm

def save_notebook(notebook):
    from pathlib import Path
    import os,shutil

    folder_path = Path("/tmp/Notebook")

    if folder_path.exists() and folder_path.is_dir():
        shutil.rmtree(folder_path)

    folder_path.mkdir(parents=True, exist_ok=True)

    if notebook:
        file_path = notebook.name
        with open(os.path.join(folder_path,file_path), "wb") as file:
                file.write(notebook.getbuffer())

def safe_concurrent_batch(chain, inputs, max_workers=2):
    results = []
    with ThreadPoolExecutor(max_workers=max_workers) as executor:
        future_to_input = {executor.submit(chain.invoke, inp): inp for inp in inputs}
        
        for future in tqdm(as_completed(future_to_input), total=len(inputs), desc="Processing"):
            inp = future_to_input[future]
            try:
                output = future.result()
                results.append({
                    "input": inp,
                    "output": output,
                    "status": "success"
                })
            except Exception as e:
                results.append({
                    "input": inp,
                    "output": None,
                    "status": f"failed: {str(e)}"
                })
    return results