-
Notifications
You must be signed in to change notification settings - Fork 0
/
cckp_publication_abstracts.py
77 lines (66 loc) · 2.55 KB
/
cckp_publication_abstracts.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import synapseclient
import boto3
import json
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from hallmarks_llm import extract_cancer_hallmarks
from tqdm import tqdm
# Initialize Synapse and AWS
syn = synapseclient.Synapse()
syn.login()
session = boto3.Session(profile_name="htan-dev")
bedrock = session.client(service_name="bedrock-runtime")
# Fetch the table
table = syn.tableQuery("SELECT * FROM syn21868591")
df = table.asDataFrame()
# Initialize columns for hallmarks and scores
df["hallmarks"] = None
df["scores"] = None
def extract_hallmarks_with_retries(index, row, max_retries=5, base_wait_time=2):
"""
Extract cancer hallmarks from the given abstract with retry logic for throttling errors.
"""
retries = 0
while retries < max_retries:
try:
# Extract hallmarks from the abstract
result = extract_cancer_hallmarks(row["abstract"])
hallmarks = [item["hallmark"] for item in result["extracted_hallmarks"]]
scores = [
item["confidence_score"] for item in result["extracted_hallmarks"]
]
return index, json.dumps(hallmarks), json.dumps(scores)
except Exception as e:
if "ThrottlingException" in str(e):
retries += 1
wait_time = base_wait_time * (2 ** (retries - 1)) # Exponential backoff
print(
f"Throttling error on row {index}. Retrying in {wait_time}s... "
f"(Attempt {retries}/{max_retries})"
)
time.sleep(wait_time)
else:
print(f"Unhandled error on row {index}: {e}")
return index, None, None
print(f"Max retries exceeded for row {index}")
return index, None, None
# Process rows in parallel
with ThreadPoolExecutor(max_workers=30) as executor:
futures = {
executor.submit(extract_hallmarks_with_retries, i, row): i
for i, row in df.iterrows()
}
# Display progress bar
for future in tqdm(
as_completed(futures), total=len(futures), desc="Processing abstracts"
):
index = futures[future]
try:
index, hallmarks, scores = future.result()
df.at[index, "hallmarks"] = hallmarks
df.at[index, "scores"] = scores
except Exception as e:
print(f"Error processing row {index}: {e}")
# Save to CSV
df.to_csv("cckp_publication_abstracts.csv", index=False, quoting=1)
print("Processing complete. Data saved to 'cckp_publication_abstracts.csv'.")