Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +56 -0
- crew.py +87 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from crew import NewsletterGenCrew
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
def load_html_template():
|
| 7 |
+
with open('config/newsletter_template.html', 'r') as file:
|
| 8 |
+
html_template = file.read()
|
| 9 |
+
return html_template
|
| 10 |
+
|
| 11 |
+
def generate_newsletter(topic: str, personal_message: str):
|
| 12 |
+
yield "🛠️ Generating newsletter... please wait.", None
|
| 13 |
+
|
| 14 |
+
inputs = {
|
| 15 |
+
'topic': topic,
|
| 16 |
+
'personal_message': personal_message,
|
| 17 |
+
'html_template': load_html_template()
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
crew_instance = NewsletterGenCrew()
|
| 21 |
+
crew = crew_instance.crew()
|
| 22 |
+
crew.kickoff(inputs=inputs)
|
| 23 |
+
|
| 24 |
+
newsletter_task = crew_instance.newsletter_task()
|
| 25 |
+
output_html = newsletter_task.output.raw
|
| 26 |
+
|
| 27 |
+
yield "✅ Newsletter ready!", output_html
|
| 28 |
+
|
| 29 |
+
with gr.Blocks() as demo:
|
| 30 |
+
gr.Markdown("## ✨ AI-Powered Newsletter Generator")
|
| 31 |
+
gr.Markdown("Enter your topic and personal message below, then click **Submit** to generate your newsletter.")
|
| 32 |
+
|
| 33 |
+
with gr.Row():
|
| 34 |
+
topic_input = gr.Textbox(label="📰 Newsletter Topic", placeholder="e.g., AI in Healthcare")
|
| 35 |
+
message_input = gr.Textbox(label="✉️ Personal Message", placeholder="e.g., Here's a weekly digest...", lines=2)
|
| 36 |
+
|
| 37 |
+
submit_btn = gr.Button("🚀 Submit")
|
| 38 |
+
html_output = gr.HTML(label="🧾 Your Newsletter")
|
| 39 |
+
status_box = gr.Textbox(label="🧠 Status", interactive=False)
|
| 40 |
+
|
| 41 |
+
submit_btn.click(
|
| 42 |
+
fn=generate_newsletter,
|
| 43 |
+
inputs=[topic_input, message_input],
|
| 44 |
+
outputs=[status_box, html_output]
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
demo.launch()
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
crew.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from crewai import Agent, Crew, Process, Task
|
| 2 |
+
from crewai.project import CrewBase, agent, crew, task
|
| 3 |
+
from tools.custom_tool import SearchAndContents, FindSimilar, GetContents
|
| 4 |
+
from langchain_groq import ChatGroq
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from langchain_core.agents import AgentFinish
|
| 7 |
+
import json
|
| 8 |
+
import os
|
| 9 |
+
import time
|
| 10 |
+
#os.environ['GROQ_API_KEY'] = os.getenv('GROQ_API')
|
| 11 |
+
|
| 12 |
+
@CrewBase
|
| 13 |
+
class NewsletterGenCrew:
|
| 14 |
+
"""NewsletterGen crew"""
|
| 15 |
+
|
| 16 |
+
agents_config = "config/agents.yaml"
|
| 17 |
+
tasks_config = "config/tasks.yaml"
|
| 18 |
+
|
| 19 |
+
def llm(self):
|
| 20 |
+
llm = ChatGroq(api_key=os.getenv("GROQ_API_KEY"), model="groq/llama-3.3-70b-versatile")
|
| 21 |
+
#llm = LLM(api_key=os.getenv('GROQ_API_KEY'), model='groq/meta-llama/llama-4-scout-17b-16e-instruct')
|
| 22 |
+
return llm
|
| 23 |
+
|
| 24 |
+
@agent
|
| 25 |
+
def researcher(self) -> Agent:
|
| 26 |
+
return Agent(
|
| 27 |
+
config=self.agents_config["researcher"],
|
| 28 |
+
tools=[SearchAndContents()],
|
| 29 |
+
verbose=False,
|
| 30 |
+
allow_delegation=False,
|
| 31 |
+
llm=self.llm()
|
| 32 |
+
#step_callback=lambda step: self.step_callback(step, "Research Agent")
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
@agent
|
| 36 |
+
def editor(self) -> Agent:
|
| 37 |
+
return Agent(
|
| 38 |
+
config=self.agents_config["editor"],
|
| 39 |
+
verbose=False,
|
| 40 |
+
allow_delegation=False,
|
| 41 |
+
llm=self.llm()
|
| 42 |
+
#step_callback=lambda step: self.step_callback(step, "Chief Editor")
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
@agent
|
| 46 |
+
def designer(self) -> Agent:
|
| 47 |
+
return Agent(
|
| 48 |
+
config=self.agents_config["designer"],
|
| 49 |
+
verbose=False,
|
| 50 |
+
allow_delegation=False,
|
| 51 |
+
llm=self.llm()
|
| 52 |
+
#step_callback=lambda step: self.step_callback(step, "HTML Writer")
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
@task
|
| 56 |
+
def research_task(self) -> Task:
|
| 57 |
+
return Task(
|
| 58 |
+
config=self.tasks_config["research_task"],
|
| 59 |
+
agent=self.researcher()
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
@task
|
| 63 |
+
def edit_task(self) -> Task:
|
| 64 |
+
return Task(
|
| 65 |
+
config=self.tasks_config["edit_task"],
|
| 66 |
+
agent=self.editor()
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
@task
|
| 70 |
+
def newsletter_task(self) -> Task:
|
| 71 |
+
# Define the task
|
| 72 |
+
return Task(
|
| 73 |
+
config=self.tasks_config["newsletter_task"],
|
| 74 |
+
agent=self.designer(),
|
| 75 |
+
#output_file=output_file
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
@crew
|
| 79 |
+
def crew(self) -> Crew:
|
| 80 |
+
"""Creates the NewsletterGen crew"""
|
| 81 |
+
return Crew(
|
| 82 |
+
agents=self.agents, # Automatically created by the @agent decorator
|
| 83 |
+
tasks=self.tasks, # Automatically created by the @task decorator
|
| 84 |
+
process=Process.sequential,
|
| 85 |
+
verbose=False
|
| 86 |
+
# process=Process.hierarchical, # In case you wanna use that instead https://docs.crewai.com/how-to/Hierarchical/
|
| 87 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
exa-py
|
| 2 |
+
crewai
|
| 3 |
+
langchain==0.3.20
|
| 4 |
+
langchain-core==0.3.51
|
| 5 |
+
langchain-groq==0.3.2
|
| 6 |
+
python-dotenv==1.0.1
|