mgbam commited on
Commit
e5581c3
·
verified ·
1 Parent(s): 0d14a0e

Update genesis/trials.py

Browse files
Files changed (1) hide show
  1. genesis/trials.py +12 -47
genesis/trials.py CHANGED
@@ -1,60 +1,25 @@
1
  # genesis/trials.py
 
 
 
 
 
2
  import requests
3
- from typing import List, Dict
4
 
5
- CLINICAL_TRIALS_API = "https://clinicaltrials.gov/api/query/study_fields"
6
 
7
- def search_clinical_trials(query: str, max_results: int = 5) -> List[Dict]:
8
- """
9
- Search ClinicalTrials.gov for trials matching the query.
10
-
11
- Returns a list of dictionaries with:
12
- - title
13
- - status
14
- - nct_id
15
- - locations
16
- - url
17
- """
18
  try:
19
  params = {
20
  "expr": query,
21
- "fields": "NCTId,BriefTitle,OverallStatus,LocationCity,LocationCountry",
22
  "min_rnk": 1,
23
  "max_rnk": max_results,
24
  "fmt": "json"
25
  }
26
- r = requests.get(CLINICAL_TRIALS_API, params=params, timeout=15)
27
  r.raise_for_status()
28
- data = r.json()
29
-
30
- studies = data.get("StudyFieldsResponse", {}).get("StudyFields", [])
31
- results = []
32
- for s in studies:
33
- nct_id = s.get("NCTId", [""])[0]
34
- title = s.get("BriefTitle", [""])[0]
35
- status = s.get("OverallStatus", [""])[0]
36
- cities = s.get("LocationCity", [])
37
- countries = s.get("LocationCountry", [])
38
- locations = [f"{c}, {countries[i]}" if i < len(countries) else c
39
- for i, c in enumerate(cities)]
40
- results.append({
41
- "title": title,
42
- "status": status,
43
- "nct_id": nct_id,
44
- "locations": locations,
45
- "url": f"https://clinicaltrials.gov/study/{nct_id}"
46
- })
47
- return results
48
  except Exception as e:
49
- print(f"[ClinicalTrials] Failed: {e}")
50
- return []
51
-
52
- def format_trials_markdown(trials: List[Dict]) -> str:
53
- """Convert trials list into markdown table."""
54
- if not trials:
55
- return "No clinical trials found."
56
- md = "| Title | Status | NCT ID | Locations |\n|-------|--------|--------|-----------|\n"
57
- for t in trials:
58
- loc_str = ", ".join(t["locations"]) if t["locations"] else "N/A"
59
- md += f"| [{t['title']}]({t['url']}) | {t['status']} | [{t['nct_id']}]({t['url']}) | {loc_str} |\n"
60
- return md
 
1
  # genesis/trials.py
2
+ """
3
+ Clinical Trials API integration for GENESIS-AI
4
+ Fetches trials from ClinicalTrials.gov.
5
+ """
6
+
7
  import requests
 
8
 
9
+ BASE_URL = "https://clinicaltrials.gov/api/query/study_fields"
10
 
11
+ def fetch_clinical_trials(query, max_results=10):
12
+ """Fetch clinical trial summaries for a given query."""
 
 
 
 
 
 
 
 
 
13
  try:
14
  params = {
15
  "expr": query,
16
+ "fields": "NCTId,BriefTitle,OverallStatus,StartDate,CompletionDate,Phase,LocationCountry",
17
  "min_rnk": 1,
18
  "max_rnk": max_results,
19
  "fmt": "json"
20
  }
21
+ r = requests.get(BASE_URL, params=params)
22
  r.raise_for_status()
23
+ return r.json().get("StudyFieldsResponse", {}).get("StudyFields", [])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  except Exception as e:
25
+ return {"error": str(e)}