Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -215,23 +215,101 @@ def new_chat(): return [], "", "New chat started"
|
|
| 215 |
|
| 216 |
def get_pubmed(query, n=5):
|
| 217 |
try:
|
|
|
|
| 218 |
r = requests.get("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi",
|
| 219 |
-
params={"db":"pubmed","term":
|
| 220 |
ids = r.json()["esearchresult"]["idlist"]
|
| 221 |
if not ids: return ""
|
| 222 |
return chr(10).join(["https://pubmed.ncbi.nlm.nih.gov/"+i for i in ids])
|
| 223 |
except: return ""
|
| 224 |
|
| 225 |
-
def
|
| 226 |
-
if not
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
r = requests.get("https://api.semanticscholar.org/graph/v1/paper/search",
|
| 230 |
-
params={"query":
|
|
|
|
| 231 |
papers = r.json().get("data",[])
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
def research_chat(message, history):
|
| 237 |
if not GROQ_KEY:
|
|
|
|
| 215 |
|
| 216 |
def get_pubmed(query, n=5):
|
| 217 |
try:
|
| 218 |
+
forced = query + " AND (heart valve OR hemodynamics OR microfluidic OR thrombogen OR creatinine OR PIV OR CKD)"
|
| 219 |
r = requests.get("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi",
|
| 220 |
+
params={"db":"pubmed","term":forced,"retmax":n,"retmode":"json","sort":"date","field":"tiab"},timeout=10)
|
| 221 |
ids = r.json()["esearchresult"]["idlist"]
|
| 222 |
if not ids: return ""
|
| 223 |
return chr(10).join(["https://pubmed.ncbi.nlm.nih.gov/"+i for i in ids])
|
| 224 |
except: return ""
|
| 225 |
|
| 226 |
+
def expand_query(query):
|
| 227 |
+
if not GROQ_KEY: return query
|
| 228 |
+
try:
|
| 229 |
+
client = Groq(api_key=GROQ_KEY)
|
| 230 |
+
system_msg = ("You are a biomedical engineering PubMed search expert for SJSU CardioLab. "
|
| 231 |
+
"Convert the user query into optimized PubMed keywords. "
|
| 232 |
+
"Focus on: mechanical heart valves, hemodynamics, blood flow, PIV, thrombogenicity, FSI, CFD, microfluidics, CKD, creatinine. "
|
| 233 |
+
"Return ONLY the search terms, no explanation, no dashes, no punctuation.")
|
| 234 |
+
resp = client.chat.completions.create(
|
| 235 |
+
model="llama-3.3-70b-versatile",
|
| 236 |
+
messages=[
|
| 237 |
+
{"role":"system","content":system_msg},
|
| 238 |
+
{"role":"user","content":"Optimize for PubMed: "+query}
|
| 239 |
+
],
|
| 240 |
+
max_tokens=60
|
| 241 |
+
)
|
| 242 |
+
expanded = resp.choices[0].message.content.strip()
|
| 243 |
+
return expanded if expanded else query
|
| 244 |
+
except: return query
|
| 245 |
+
|
| 246 |
+
def search_pubmed_smart(query, n=8):
|
| 247 |
try:
|
| 248 |
+
expanded = expand_query(query)
|
| 249 |
+
biomedical_filter = " AND (heart valve OR hemodynamics OR microfluidic OR thrombogen OR creatinine OR PIV OR CFD OR fluid structure OR CKD)"
|
| 250 |
+
forced = expanded + biomedical_filter
|
| 251 |
+
r = requests.get("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi",
|
| 252 |
+
params={"db":"pubmed","term":forced,"retmax":n,"retmode":"json","sort":"date","field":"tiab"},timeout=10)
|
| 253 |
+
ids = r.json()["esearchresult"]["idlist"]
|
| 254 |
+
if not ids:
|
| 255 |
+
return "No PubMed results found for this topic.", expanded
|
| 256 |
+
r2 = requests.get("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi",
|
| 257 |
+
params={"db":"pubmed","id":",".join(ids),"retmode":"xml","rettype":"abstract"},timeout=10)
|
| 258 |
+
lines = []
|
| 259 |
+
try:
|
| 260 |
+
import xml.etree.ElementTree as ET
|
| 261 |
+
root = ET.fromstring(r2.content)
|
| 262 |
+
for article in root.findall(".//PubmedArticle"):
|
| 263 |
+
try:
|
| 264 |
+
title_el = article.find(".//ArticleTitle")
|
| 265 |
+
title = title_el.text if title_el is not None else "No title"
|
| 266 |
+
pmid_el = article.find(".//PMID")
|
| 267 |
+
pmid = pmid_el.text if pmid_el is not None else ""
|
| 268 |
+
year_el = article.find(".//PubDate/Year")
|
| 269 |
+
year = year_el.text if year_el is not None else ""
|
| 270 |
+
url = "https://pubmed.ncbi.nlm.nih.gov/"+pmid
|
| 271 |
+
lines.append(str(title)[:90]+" ("+year+")"+chr(10)+" "+url)
|
| 272 |
+
except: continue
|
| 273 |
+
except:
|
| 274 |
+
lines = ["https://pubmed.ncbi.nlm.nih.gov/"+i for i in ids]
|
| 275 |
+
return chr(10)+chr(10).join(lines), expanded
|
| 276 |
+
except Exception as e:
|
| 277 |
+
return "PubMed error: "+str(e), query
|
| 278 |
+
|
| 279 |
+
def search_scholar_smart(query, n=8):
|
| 280 |
+
try:
|
| 281 |
+
expanded = expand_query(query)
|
| 282 |
r = requests.get("https://api.semanticscholar.org/graph/v1/paper/search",
|
| 283 |
+
params={"query":expanded,"limit":n,"fields":"title,year,url,citationCount"},
|
| 284 |
+
timeout=15)
|
| 285 |
papers = r.json().get("data",[])
|
| 286 |
+
if not papers:
|
| 287 |
+
return "No Semantic Scholar results found."
|
| 288 |
+
out = []
|
| 289 |
+
for p in papers:
|
| 290 |
+
title = p.get("title","")
|
| 291 |
+
year = str(p.get("year",""))
|
| 292 |
+
url = p.get("url","")
|
| 293 |
+
citations = str(p.get("citationCount",0))
|
| 294 |
+
if url:
|
| 295 |
+
out.append(str(title)[:90]+" ("+year+") | "+citations+" citations"+chr(10)+" "+url)
|
| 296 |
+
return chr(10)+chr(10).join(out) if out else "No results."
|
| 297 |
+
except Exception as e:
|
| 298 |
+
return "Scholar error: "+str(e)
|
| 299 |
+
|
| 300 |
+
def quick_search(query):
|
| 301 |
+
if not query.strip(): return "Please enter a research topic."
|
| 302 |
+
pubmed_results, expanded = search_pubmed_smart(query, n=8)
|
| 303 |
+
scholar_results = search_scholar_smart(query, n=6)
|
| 304 |
+
result = "YOUR QUERY: " + query + chr(10)
|
| 305 |
+
result += "AI EXPANDED: " + expanded + chr(10)
|
| 306 |
+
result += "="*45 + chr(10) + chr(10)
|
| 307 |
+
result += "PUBMED RESULTS (titles + verified links):" + chr(10)
|
| 308 |
+
result += pubmed_results + chr(10) + chr(10)
|
| 309 |
+
result += "="*45 + chr(10)
|
| 310 |
+
result += "SEMANTIC SCHOLAR RESULTS:" + chr(10)
|
| 311 |
+
result += scholar_results
|
| 312 |
+
return result
|
| 313 |
|
| 314 |
def research_chat(message, history):
|
| 315 |
if not GROQ_KEY:
|