Update interm.py
Browse files
interm.py
CHANGED
@@ -1,42 +1,72 @@
|
|
1 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
2 |
-
from
|
3 |
-
import
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
2 |
+
from Bio import Entrez
|
3 |
+
from fpdf import FPDF
|
4 |
+
import tempfile
|
5 |
+
|
6 |
+
# Initialize Clinical AI Agent
|
7 |
+
clinical_agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())
|
8 |
+
|
9 |
+
# Your PubMed Email (IMPORTANT: Use your registered PubMed email)
|
10 |
+
Entrez.email = "Pub_Email" # <-- Your registered PubMed email
|
11 |
+
|
12 |
+
def generate_clinical_content(topic, audience):
|
13 |
+
"""Generates medical content based on topic and audience."""
|
14 |
+
prompt = f"Write a detailed medical article on: {topic}.\nTarget Audience: {audience}.\nInclude latest medical research insights."
|
15 |
+
return clinical_agent.run(prompt)
|
16 |
+
|
17 |
+
def generate_summary(content):
|
18 |
+
"""Summarizes a given clinical document or research paper."""
|
19 |
+
summary_prompt = f"Summarize the following medical research:\n{content}"
|
20 |
+
return clinical_agent.run(summary_prompt)
|
21 |
+
|
22 |
+
def generate_soap_note(symptoms, history):
|
23 |
+
"""Generates a SOAP Note for clinicians based on symptoms and patient history."""
|
24 |
+
soap_prompt = (
|
25 |
+
f"Create a structured SOAP Note for a patient with:\n"
|
26 |
+
f"Symptoms: {symptoms}\n"
|
27 |
+
f"History: {history}\n"
|
28 |
+
f"Include Assessment & Plan."
|
29 |
+
)
|
30 |
+
return clinical_agent.run(soap_prompt)
|
31 |
+
|
32 |
+
def fetch_pubmed_articles(query):
|
33 |
+
"""Fetches latest medical research from PubMed based on a query."""
|
34 |
+
try:
|
35 |
+
handle = Entrez.esearch(db="pubmed", term=query, retmax=5)
|
36 |
+
record = Entrez.read(handle)
|
37 |
+
handle.close()
|
38 |
+
article_ids = record["IdList"]
|
39 |
+
|
40 |
+
articles = []
|
41 |
+
for article_id in article_ids:
|
42 |
+
handle = Entrez.efetch(db="pubmed", id=article_id, retmode="xml")
|
43 |
+
article_record = Entrez.read(handle)
|
44 |
+
handle.close()
|
45 |
+
|
46 |
+
article_data = article_record["PubmedArticle"][0]["MedlineCitation"]["Article"]
|
47 |
+
title = article_data.get("ArticleTitle", "No Title Available")
|
48 |
+
abstract = article_data.get("Abstract", {}).get("AbstractText", ["No Abstract Available"])[0]
|
49 |
+
authors = ", ".join([author["LastName"] for author in article_data.get("AuthorList", []) if "LastName" in author])
|
50 |
+
url = f"https://pubmed.ncbi.nlm.nih.gov/{article_id}/"
|
51 |
+
|
52 |
+
articles.append({"title": title, "abstract": abstract, "authors": authors, "url": url})
|
53 |
+
|
54 |
+
return articles
|
55 |
+
except Exception as e:
|
56 |
+
return [{"title": "Error Fetching Articles", "abstract": str(e), "authors": "N/A", "url": "#"}]
|
57 |
+
|
58 |
+
def save_as_text(content, filename):
|
59 |
+
"""Saves AI-generated content as a TXT file."""
|
60 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as tmp_file:
|
61 |
+
tmp_file.write(content.encode())
|
62 |
+
return tmp_file.name, filename
|
63 |
+
|
64 |
+
def save_as_pdf(content, filename):
|
65 |
+
"""Saves AI-generated content as a PDF file."""
|
66 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
67 |
+
pdf = FPDF()
|
68 |
+
pdf.add_page()
|
69 |
+
pdf.set_font("Arial", size=12)
|
70 |
+
pdf.multi_cell(0, 10, content)
|
71 |
+
pdf.output(tmp_file.name)
|
72 |
+
return tmp_file.name, filename
|