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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import re
|
| 4 |
+
import time
|
| 5 |
+
from crewai import Agent, Task, Crew, LLM
|
| 6 |
+
|
| 7 |
+
# Healthcare-focused blog generator using CrewAI
|
| 8 |
+
class HealthcareBlogGenerator:
|
| 9 |
+
def __init__(self):
|
| 10 |
+
self.llm = None
|
| 11 |
+
self.crew = None
|
| 12 |
+
|
| 13 |
+
def setup_llm(self):
|
| 14 |
+
"""Setup the LLM (API key assumed already set via env variable)"""
|
| 15 |
+
self.llm = LLM("cohere/command-a-03-2025")
|
| 16 |
+
|
| 17 |
+
def create_agents(self):
|
| 18 |
+
"""Create specialized healthcare content agents"""
|
| 19 |
+
planner = Agent(
|
| 20 |
+
role="Healthcare Content Planner",
|
| 21 |
+
goal="Plan engaging, medically accurate, and educational content on {topic}",
|
| 22 |
+
backstory="You're a healthcare content strategist with expertise in medical writing. "
|
| 23 |
+
"You specialize in creating content plans for healthcare topics that are "
|
| 24 |
+
"accurate, accessible to patients and healthcare professionals, and "
|
| 25 |
+
"comply with medical content guidelines.",
|
| 26 |
+
allow_delegation=False,
|
| 27 |
+
verbose=True,
|
| 28 |
+
llm=self.llm
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
writer = Agent(
|
| 32 |
+
role="Medical Content Writer",
|
| 33 |
+
goal="Write accurate, evidence-based, and accessible healthcare content on {topic}",
|
| 34 |
+
backstory="You're a medical writer with extensive experience in healthcare communication. "
|
| 35 |
+
"You excel at translating complex medical information into clear, understandable "
|
| 36 |
+
"content for patients, caregivers, and professionals.",
|
| 37 |
+
allow_delegation=False,
|
| 38 |
+
verbose=True,
|
| 39 |
+
llm=self.llm
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
editor = Agent(
|
| 43 |
+
role="Healthcare Content Editor",
|
| 44 |
+
goal="Edit and review healthcare content for accuracy, clarity, and compliance",
|
| 45 |
+
backstory="You're a healthcare content editor with a background in medical publishing. "
|
| 46 |
+
"You ensure all healthcare content meets the highest standards of medical "
|
| 47 |
+
"accuracy, includes proper disclaimers, and is accessible.",
|
| 48 |
+
allow_delegation=False,
|
| 49 |
+
verbose=True,
|
| 50 |
+
llm=self.llm
|
| 51 |
+
)
|
| 52 |
+
return planner, writer, editor
|
| 53 |
+
|
| 54 |
+
def create_tasks(self, planner, writer, editor):
|
| 55 |
+
"""Create healthcare-focused tasks"""
|
| 56 |
+
plan = Task(
|
| 57 |
+
description=("Research the latest medical evidence and guidelines on {topic}, "
|
| 58 |
+
"analyze the target audience, and create a structured outline."),
|
| 59 |
+
expected_output="A comprehensive healthcare content plan with outline and audience analysis.",
|
| 60 |
+
agent=planner,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
write = Task(
|
| 64 |
+
description=("Write a well-structured, medically accurate blog post on {topic}, "
|
| 65 |
+
"using the content plan as a guide. Include disclaimers and practical advice."),
|
| 66 |
+
expected_output="A markdown-formatted healthcare blog post ready for publication.",
|
| 67 |
+
agent=writer,
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
edit = Task(
|
| 71 |
+
description=("Review and polish the blog post for accuracy, clarity, readability, "
|
| 72 |
+
"and compliance with healthcare content standards."),
|
| 73 |
+
expected_output="A final, polished markdown blog post with disclaimers.",
|
| 74 |
+
agent=editor
|
| 75 |
+
)
|
| 76 |
+
return plan, write, edit
|
| 77 |
+
|
| 78 |
+
def generate_blog(self, topic, progress=gr.Progress()):
|
| 79 |
+
"""Generate a healthcare blog post"""
|
| 80 |
+
try:
|
| 81 |
+
if not topic.strip():
|
| 82 |
+
return "β Please provide a healthcare topic to write about."
|
| 83 |
+
|
| 84 |
+
progress(0.1, desc="Setting up AI agents...")
|
| 85 |
+
self.setup_llm()
|
| 86 |
+
planner, writer, editor = self.create_agents()
|
| 87 |
+
plan, write, edit = self.create_tasks(planner, writer, editor)
|
| 88 |
+
|
| 89 |
+
self.crew = Crew(
|
| 90 |
+
agents=[planner, writer, editor],
|
| 91 |
+
tasks=[plan, write, edit],
|
| 92 |
+
verbose=True
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
progress(0.5, desc="Generating blog post...")
|
| 96 |
+
result = self.crew.kickoff(inputs={"topic": topic})
|
| 97 |
+
progress(1.0, desc="Done!")
|
| 98 |
+
|
| 99 |
+
final_content = f"""# Healthcare Blog: {topic}
|
| 100 |
+
|
| 101 |
+
*Generated by AI Healthcare Content Crew*
|
| 102 |
+
|
| 103 |
+
---
|
| 104 |
+
|
| 105 |
+
{result}
|
| 106 |
+
|
| 107 |
+
---
|
| 108 |
+
|
| 109 |
+
## Important Medical Disclaimer
|
| 110 |
+
|
| 111 |
+
**This content is for educational purposes only and is not medical advice. Always seek professional medical guidance. In case of an emergency, call your doctor or emergency services immediately.**
|
| 112 |
+
|
| 113 |
+
---
|
| 114 |
+
|
| 115 |
+
*Content generated on {time.strftime("%Y-%m-%d %H:%M:%S")} using AI technology*
|
| 116 |
+
"""
|
| 117 |
+
return final_content
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
return f"β Error generating content: {str(e)}"
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# Initialize generator
|
| 124 |
+
blog_generator = HealthcareBlogGenerator()
|
| 125 |
+
|
| 126 |
+
def _slugify(text: str) -> str:
|
| 127 |
+
text = text.strip().lower()
|
| 128 |
+
text = re.sub(r"[^\w\s-]", "", text)
|
| 129 |
+
text = re.sub(r"[\s_-]+", "-", text)
|
| 130 |
+
return text[:80] or "blog"
|
| 131 |
+
|
| 132 |
+
# Gradio interface
|
| 133 |
+
def create_interface():
|
| 134 |
+
with gr.Blocks(
|
| 135 |
+
title="Healthcare Blog Generator",
|
| 136 |
+
theme=gr.themes.Soft(),
|
| 137 |
+
css="""
|
| 138 |
+
.healthcare-header {
|
| 139 |
+
text-align: center;
|
| 140 |
+
padding: 20px;
|
| 141 |
+
border-radius: 10px;
|
| 142 |
+
margin-bottom: 20px;
|
| 143 |
+
}
|
| 144 |
+
.disclaimer-box {
|
| 145 |
+
padding: 15px;
|
| 146 |
+
margin: 15px 0;
|
| 147 |
+
border-radius: 5px;
|
| 148 |
+
border: 1px solid #666;
|
| 149 |
+
}
|
| 150 |
+
"""
|
| 151 |
+
) as demo:
|
| 152 |
+
|
| 153 |
+
gr.HTML("""
|
| 154 |
+
<div class="healthcare-header">
|
| 155 |
+
<h1>π₯ Healthcare Blog Generator</h1>
|
| 156 |
+
<p>AI-Powered Medical Content Creation with CrewAI & Cohere</p>
|
| 157 |
+
</div>
|
| 158 |
+
""")
|
| 159 |
+
|
| 160 |
+
gr.HTML("""
|
| 161 |
+
<div class="disclaimer-box">
|
| 162 |
+
<h3>βοΈ Medical Content Notice</h3>
|
| 163 |
+
<p>This tool generates educational healthcare content using AI. All content should be reviewed by qualified medical professionals before publication.</p>
|
| 164 |
+
</div>
|
| 165 |
+
""")
|
| 166 |
+
|
| 167 |
+
with gr.Row():
|
| 168 |
+
with gr.Column(scale=1):
|
| 169 |
+
gr.HTML("<h3>π Content Configuration</h3>")
|
| 170 |
+
|
| 171 |
+
topic_input = gr.Textbox(
|
| 172 |
+
label="π₯ Healthcare Topic",
|
| 173 |
+
placeholder="e.g., Diabetes Management, Heart Disease Prevention...",
|
| 174 |
+
lines=2
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
example_topics = [
|
| 178 |
+
"Type 2 Diabetes Management and Prevention",
|
| 179 |
+
"Mental Health Awareness in the Workplace",
|
| 180 |
+
"Heart Disease Prevention Strategies",
|
| 181 |
+
"Understanding Hypertension and Blood Pressure",
|
| 182 |
+
"Women's Health and Preventive Care",
|
| 183 |
+
"Pediatric Vaccination Guidelines",
|
| 184 |
+
"Managing Chronic Pain Naturally",
|
| 185 |
+
"Nutrition for Healthy Aging"
|
| 186 |
+
]
|
| 187 |
+
|
| 188 |
+
topic_examples = gr.Dropdown(
|
| 189 |
+
choices=example_topics,
|
| 190 |
+
label="Select Example Topic",
|
| 191 |
+
interactive=True
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
topic_examples.change(
|
| 195 |
+
fn=lambda x: x if x else "",
|
| 196 |
+
inputs=[topic_examples],
|
| 197 |
+
outputs=[topic_input]
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
generate_btn = gr.Button("π Generate Healthcare Blog", variant="primary")
|
| 201 |
+
|
| 202 |
+
gr.HTML("""
|
| 203 |
+
<div style="margin-top: 20px;">
|
| 204 |
+
<h4>π How it works:</h4>
|
| 205 |
+
<ol>
|
| 206 |
+
<li><b>Planner Agent:</b> Researches and plans content</li>
|
| 207 |
+
<li><b>Writer Agent:</b> Creates evidence-based medical content</li>
|
| 208 |
+
<li><b>Editor Agent:</b> Reviews for accuracy and compliance</li>
|
| 209 |
+
</ol>
|
| 210 |
+
</div>
|
| 211 |
+
""")
|
| 212 |
+
|
| 213 |
+
with gr.Column(scale=2):
|
| 214 |
+
gr.HTML("<h3>π Generated Healthcare Content</h3>")
|
| 215 |
+
|
| 216 |
+
output_markdown = gr.Markdown(
|
| 217 |
+
value="π Enter a topic and click **Generate Healthcare Blog** to create content!",
|
| 218 |
+
label="Blog Content"
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
download_btn = gr.DownloadButton("πΎ Download Blog Post", visible=False)
|
| 222 |
+
|
| 223 |
+
# --- FIXED HANDLER: write file and return its path to the DownloadButton ---
|
| 224 |
+
def generate_and_update(topic):
|
| 225 |
+
if not topic.strip():
|
| 226 |
+
return "β Please provide a healthcare topic.", gr.update(visible=False)
|
| 227 |
+
|
| 228 |
+
content = blog_generator.generate_blog(topic)
|
| 229 |
+
# create a safe file path
|
| 230 |
+
fname = f"healthcare_blog_{_slugify(topic)}.md"
|
| 231 |
+
fpath = os.path.join(os.getcwd(), fname)
|
| 232 |
+
with open(fpath, "w", encoding="utf-8") as f:
|
| 233 |
+
f.write(content)
|
| 234 |
+
# Show the download button with a valid file path
|
| 235 |
+
return content, gr.update(visible=True, value=fpath)
|
| 236 |
+
|
| 237 |
+
generate_btn.click(
|
| 238 |
+
fn=generate_and_update,
|
| 239 |
+
inputs=[topic_input],
|
| 240 |
+
outputs=[output_markdown, download_btn]
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
return demo
|
| 246 |
+
|
| 247 |
+
# Run app
|
| 248 |
+
if __name__ == "__main__":
|
| 249 |
+
demo = create_interface()
|
| 250 |
+
demo.launch()
|