mgbam commited on
Commit
36b8a4a
·
verified ·
1 Parent(s): b657573

Update interm.py

Browse files
Files changed (1) hide show
  1. interm.py +72 -42
interm.py CHANGED
@@ -1,42 +1,72 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
2
- from fpdf import FPDF
3
- import tempfile
4
-
5
- # Initialize Clinical AI Agent
6
- clinical_agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())
7
-
8
- def generate_clinical_content(topic, audience):
9
- """Generates medical content based on topic and audience."""
10
- prompt = f"Write a detailed medical article on: {topic}.\nTarget Audience: {audience}.\nInclude latest medical research insights."
11
- return clinical_agent.run(prompt)
12
-
13
- def generate_summary(content):
14
- """Summarizes a given clinical document or research paper."""
15
- summary_prompt = f"Summarize the following medical research:\n{content}"
16
- return clinical_agent.run(summary_prompt)
17
-
18
- def generate_soap_note(symptoms, history):
19
- """Generates a SOAP Note for clinicians based on symptoms and patient history."""
20
- soap_prompt = (
21
- f"Create a structured SOAP Note for a patient with:\n"
22
- f"Symptoms: {symptoms}\n"
23
- f"History: {history}\n"
24
- f"Include Assessment & Plan."
25
- )
26
- return clinical_agent.run(soap_prompt)
27
-
28
- def save_as_text(content, filename):
29
- """Saves AI-generated content as a TXT file."""
30
- with tempfile.NamedTemporaryFile(delete=False, suffix=".txt") as tmp_file:
31
- tmp_file.write(content.encode())
32
- return tmp_file.name, filename
33
-
34
- def save_as_pdf(content, filename):
35
- """Saves AI-generated content as a PDF file."""
36
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
37
- pdf = FPDF()
38
- pdf.add_page()
39
- pdf.set_font("Arial", size=12)
40
- pdf.multi_cell(0, 10, content)
41
- pdf.output(tmp_file.name)
42
- return tmp_file.name, filename
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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