Spaces:
Sleeping
Sleeping
File size: 14,884 Bytes
52ee7a9 0b336c0 a6bd112 52ee7a9 0b336c0 52ee7a9 a6bd112 52ee7a9 0b336c0 52ee7a9 a6bd112 0b336c0 52ee7a9 0b336c0 52ee7a9 a6bd112 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 86f6253 4bb7c94 86f6253 4bb7c94 86f6253 0b336c0 86f6253 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 52ee7a9 1e2e3b8 0b336c0 52ee7a9 0b336c0 52ee7a9 1e2e3b8 0b336c0 52ee7a9 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 0b336c0 1e2e3b8 52ee7a9 1e2e3b8 0b336c0 4bb7c94 86f6253 0b336c0 4bb7c94 52ee7a9 e7d7b51 86f6253 e7d7b51 0b336c0 e7d7b51 0b336c0 e7d7b51 0b336c0 e7d7b51 52ee7a9 e7d7b51 52ee7a9 a6bd112 52ee7a9 0b336c0 52ee7a9 86f6253 52ee7a9 4bb7c94 d8b73be 4bb7c94 a6bd112 d8b73be 4bb7c94 d8b73be 4bb7c94 d8b73be 4bb7c94 d8b73be 4bb7c94 d8b73be a6bd112 4bb7c94 a6bd112 4bb7c94 d8b73be 86f6253 4bb7c94 86f6253 551646a 86f6253 551646a 52ee7a9 551646a 4bb7c94 0b336c0 86f6253 d8b73be 52ee7a9 d8b73be 4bb7c94 1b1c01c 4bb7c94 d8b73be 4bb7c94 1b1c01c 4bb7c94 1b1c01c 4bb7c94 1b1c01c 86f6253 4bb7c94 86f6253 4bb7c94 86f6253 0b336c0 1e2e3b8 0b336c0 d8b73be 4bb7c94 d8b73be 4bb7c94 86f6253 1b1c01c 0b336c0 4bb7c94 0b336c0 1e2e3b8 0b336c0 1e2e3b8 86f6253 1e2e3b8 0b336c0 86f6253 1e2e3b8 86f6253 52ee7a9 4bb7c94 d8b73be a6bd112 d8b73be 4bb7c94 1b1c01c 4bb7c94 0b336c0 4bb7c94 |
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
import ast
import json
import os
import statistics
from collections import Counter
from typing import Any, Dict, List
import langchain
import openai
import pandas as pd
import regex as re
import requests
from dotenv import load_dotenv
from langchain import OpenAI
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
from langchain.chains.llm import LLMChain
from langchain.chains.qa_with_sources.loading import load_qa_with_sources_chain
from langchain.document_loaders import UnstructuredURLLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain_community.document_loaders import JSONLoader
from langchain_community.document_loaders.csv_loader import CSVLoader
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI
load_dotenv()
# getting the json files
def get_clinical_record_info(clinical_record_id: str) -> Dict[str, Any]:
# Request:
# curl -X GET "https://clinicaltrials.gov/api/v2/studies/NCT00841061" \
# -H "accept: text/csv"
request_url = f"https://clinicaltrials.gov/api/v2/studies/{clinical_record_id}"
response = requests.get(request_url, headers={"accept": "application/json"})
return response.json()
def get_clinical_records_by_ids(clinical_record_ids: List[str]) -> List[Dict[str, Any]]:
clinical_records = []
for clinical_record_id in clinical_record_ids:
clinical_record_info = get_clinical_record_info(clinical_record_id)
clinical_records.append(clinical_record_info)
return clinical_records
# # def process_json_data_for_llm(data):
# # Define the fields you want to keep
# fields_to_keep = [
# "class_of_organization",
# "title",
# "overallStatus",
# "descriptionModule",
# "conditions",
# "interventions",
# "outcomesModule",
# "eligibilityModule",
# ]
# # Iterate through the dictionary and keep only the desired fields
# filtered_data = []
# for item in data:
# try:
# organization_name = item["protocolSection"]["identificationModule"][
# "organization"
# ]["fullName"]
# except:
# organization_name = ""
# try:
# project_title = item["protocolSection"]["identificationModule"][
# "officialTitle"
# ]
# except:
# project_title = ""
# try:
# status = item["protocolSection"]["statusModule"]["overallStatus"]
# except:
# status = ""
# try:
# briefDescription = item["protocolSection"]["descriptionModule"][
# "briefSummary"
# ]
# except:
# briefDescription = ""
# try:
# detailedDescription = item["protocolSection"]["descriptionModule"][
# "detailedDescription"
# ]
# except:
# detailedDescription = ""
# try:
# conditions = item["protocolSection"]["conditionsModule"]["conditions"]
# except:
# conditions = []
# try:
# keywords = item["protocolSection"]["conditionsModule"]["keywords"]
# except:
# keywords = []
# try:
# interventions = item["protocolSection"]["armsInterventionsModule"][
# "interventions"
# ]
# except:
# interventions = []
# try:
# primary_outcomes = item["protocolSection"]["outcomesModule"][
# "primaryOutcomes"
# ]
# except:
# primary_outcomes = []
# try:
# secondary_outcomes = item["protocolSection"]["outcomesModule"][
# "secondaryOutcomes"
# ]
# except:
# secondary_outcomes = []
# try:
# eligibility = item["protocolSection"]["eligibilityModule"]
# except:
# eligibility = {}
# filtered_item = {
# "organization_name": organization_name,
# "project_title": project_title,
# "status": status,
# "briefDescription": briefDescription,
# "detailedDescription": detailedDescription,
# "keywords": keywords,
# "interventions": interventions,
# "primary_outcomes": primary_outcomes,
# "secondary_outcomes": secondary_outcomes,
# "eligibility": eligibility,
# }
# filtered_data.append(filtered_item)
# return filtered_data
# # for ele in filtered_data:
# # print(ele)
def process_dictionaty_with_llm_to_generate_response(json_data):
# processed_data = process_json_data_for_llm(json_data)
# res = tagging_chain.invoke({"input": processed_data})
# return res
# Iterate through the dictionary and keep only the desired fields
filtered_data = []
for item in json_data:
try:
organization_name = item["protocolSection"]["identificationModule"][
"organization"
]["fullName"]
except:
organization_name = ""
try:
project_title = item["protocolSection"]["identificationModule"][
"officialTitle"
]
except:
project_title = ""
try:
status = item["protocolSection"]["statusModule"]["overallStatus"]
except:
status = ""
try:
briefDescription = item["protocolSection"]["descriptionModule"][
"briefSummary"
]
except:
briefDescription = ""
try:
detailedDescription = item["protocolSection"]["descriptionModule"][
"detailedDescription"
]
except:
detailedDescription = ""
try:
conditions = item["protocolSection"]["conditionsModule"]["conditions"]
except:
conditions = []
try:
keywords = item["protocolSection"]["conditionsModule"]["keywords"]
except:
keywords = []
try:
interventions = item["protocolSection"]["armsInterventionsModule"][
"interventions"
]
except:
interventions = []
try:
primary_outcomes = item["protocolSection"]["outcomesModule"][
"primaryOutcomes"
]
except:
primary_outcomes = []
try:
secondary_outcomes = item["protocolSection"]["outcomesModule"][
"secondaryOutcomes"
]
except:
secondary_outcomes = []
try:
eligibility = item["protocolSection"]["eligibilityModule"]
except:
eligibility = {}
filtered_item = {
"organization_name": organization_name,
"project_title": project_title,
"status": status,
"briefDescription": briefDescription,
"detailedDescription": detailedDescription,
"keywords": keywords,
"interventions": interventions,
"primary_outcomes": primary_outcomes,
"secondary_outcomes": secondary_outcomes,
"eligibility": eligibility,
}
filtered_data.append(filtered_item)
return filtered_data
def get_short_summary_out_of_json_files(data_json):
prompt_template = """You are an expert on clinicial trials and their analysis of their reports.
# Task
You will be given a text of descriptions of multiple clinical trials realed to similar diseases. Your job is to come up with a short and detailed summary of the descriptions of the clinical trials. Your users are clinical researchers, so you should be technical and specific, including scientific terms in the summary.
{text}"""
prompt_template = """You are an expert clinician working on the analysis of reports of clinical trials.
# Task
You will be given a set of descriptions of clinical trials. Your job is to come up with a short summary (100-200 words) of the descriptions of the clinical trials. Your users are clinical researchers who are experts in medicine, so you should be technical and specific, including scientific terms. Always be faithful to the original information written in the reports.
To write your summary, you will need to read the following examples, labeled as "Report 1", "Report 2", and so on. Your answer should be a single paragraph (100-200 words) that summarizes the general content of all the reports. Format your answer in Markdown format, **highlighting** the most important concepts, and _italicizing_ the technical concepts extracted from the reports. Be very specific about the details of the clinical trials.
{text}
General summary:"""
prompt = PromptTemplate.from_template(prompt_template)
llm = ChatOpenAI(
temperature=0.5, model_name="gpt-4-turbo", api_key=os.environ["OPENAI_API_KEY"]
)
llm_chain = LLMChain(llm=llm, prompt=prompt)
# Define StuffDocumentsChain
stuff_chain = StuffDocumentsChain(
llm_chain=llm_chain, document_variable_name="text"
)
descriptions = [
(
x["detailedDescription"]
if "detailedDescription" in x and len(x["detailedDescription"]) > 0
else x["briefSummary"]
)
for x in data_json
if "detailedDescription" in x or "briefSummary" in x
]
combined_descriptions = ""
for i, description in enumerate(descriptions):
combined_descriptions += f"Report {i+1}:\n{description}\n"
print(f"Combined descriptions: {combined_descriptions}")
result = stuff_chain.run(combined_descriptions)
print(f"Result_summarization: {result}")
return result
def analyze_data(data):
print(f"Data: {data}")
# Extract minimum and maximum ages: Turn ['18 Years', '20 Years'] into [18, 20]
min_ages = [
int(re.search(r"\d+", age).group()) for age in data["minimum_age"] if age
]
max_ages = [
int(re.search(r"\d+", age).group()) for age in data["maximum_age"] if age
]
# primary_timeframe= [int(age.split()[0]) for age in data['[primary_outcome]'] if age]
# Calculate average minimum and maximum ages
avg_min_age = statistics.mean(min_ages) if min_ages else None
avg_max_age = statistics.mean(max_ages) if max_ages else None
# Find most common gender
gender_counter = Counter(data["gender"])
most_common_gender = gender_counter.most_common(1)[0][0]
# Flatten keywords list and find common keywords
# keywords = [keyword for sublist in data["keywords"] for keyword in sublist]
# common_keywords = [word for word, count in Counter(keywords).most_common()]
return {
"avg_min_age": avg_min_age,
"avg_max_age": avg_max_age,
"most_common_gender": most_common_gender,
}
def tagging_insights_from_json(data_json):
processed_json = process_dictionaty_with_llm_to_generate_response(data_json)
tagging_prompt = ChatPromptTemplate.from_template(
"""Extract the desired information from the following JSON data.
Only extract the properties mentioned in the 'Classification' function. Output a list of the extracted properties, starting with [ and ending with ], for each of the properties.
Raw data (in JSON format):
{input}
"""
)
class Classification(BaseModel):
# description: str = Field(
# description="text description grouping all the clinical trials using briefDescription and detailedDescription keys"
# )
# project_title: list = Field(
# description="Extract the project titles of all the clinical trials"
# )
# status: list = Field(
# description="Extract the status of all the clinical trials"
# )
# keywords: list = Field(
# description="Extract the most relevant keywords for each clinical trials"
# )
# interventions: list = Field(
# description="describe the interventions for each clinical trial using title, name and description"
# )
# primary_outcomes: list = Field(
# description="get the timeframe of each clinical trial"
# )
# secondary_outcomes: list= Field(description= "get the secondary outcomes of each clinical trial")
# eligibility: list = Field(
# description="get the timeframe of each clinical trial"
# )
# healthy_volunteers: list= Field(description= "determine whether the clinical trial requires healthy volunteers")
minimum_age: list = Field(
description="get the minimum age from each experiment"
)
maximum_age: list = Field(
description="get the maximum age from each experiment"
)
gender: list = Field(description="get the gender from each experiment")
def get_dict(self):
return {
# "project_title": self.project_title,
# "status": self.status,
# "keywords": self.keywords,
# "interventions": self.interventions,
# "primary_outcomes": self.primary_outcomes,
# "secondary_outcomes": self.secondary_outcomes,
# "eligibility": self.eligibility,
# "healthy_volunteers": self.healthy_volunteers,
"minimum_age": self.minimum_age,
"maximum_age": self.maximum_age,
"gender": self.gender,
}
# LLM
llm = ChatOpenAI(
temperature=0.6,
model="gpt-4-turbo",
openai_api_key=os.environ["OPENAI_API_KEY"],
).with_structured_output(Classification)
# stuff_chain = StuffDocumentsChain(llm_chain=llm, document_variable_name="text")
tagging_chain = tagging_prompt | llm
res = tagging_chain.invoke({"input": processed_json})
unprocessed_results_dict = res.get_dict()
results_dict = analyze_data(unprocessed_results_dict)
# stats_dict= {'Average Minimum age': avg_min_age,
# 'Average Maximum age': avg_max_age,
# 'Most common gender undergoing the trials': most_common_gender,
# 'common keywords found in the trials': common_keywords}
print(f"Result_tagging: {results_dict}")
return results_dict
# clinical_record_info = get_clinical_records_by_ids(['NCT00841061', 'NCT03035123', 'NCT02272751', 'NCT03035123', 'NCT03055377'])
# print(clinical_record_info)
# with open('data.json', 'w') as f:
# json.dump(clinical_record_info, f, indent=4)
# tagging_chain = tagging_insights_from_json(json_data)
|