Spaces:
Running
Running
Upload 3 files
Browse files- appnew.py +214 -0
- requirements.txt +0 -0
- utils_groc.py +39 -0
appnew.py
ADDED
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import warnings
|
2 |
+
warnings.filterwarnings('ignore')
|
3 |
+
from crewai import Agent, Task, Crew
|
4 |
+
import os
|
5 |
+
from utils_groc import get_groc_api_key
|
6 |
+
from crewai_tools import SerperDevTool, ScrapeWebsiteTool, WebsiteSearchTool
|
7 |
+
from IPython.display import Markdown
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
|
11 |
+
os.environ["OPENAI_API_BASE"] = "https://api.groc.com/openai/v1"
|
12 |
+
os.environ["OPENAI_MODLE_NAME"] = "deepseek-r1-distill-llama-70b"
|
13 |
+
os.environ["OPENAI_API_KEY"] = os.getenv("GROQ_API_KEY")
|
14 |
+
|
15 |
+
def generate_blog_post(topic):
|
16 |
+
groc_api_key = get_groc_api_key()
|
17 |
+
os.environ["GROQ_API_KEY"] = 'deepseek-r1-distill-llama-70b'
|
18 |
+
|
19 |
+
search_tool = SerperDevTool()
|
20 |
+
scrape_tool = ScrapeWebsiteTool()
|
21 |
+
|
22 |
+
researcher = Agent(
|
23 |
+
role="Digital Marketing Researcher",
|
24 |
+
goal="Gather accurate, relevant, and engaging information on {topic} to support the creation of a high-quality blog post.",
|
25 |
+
backstory="You are an expert in digital marketing research, specializing in identifying trends, data, and insights that resonate with the target audience.",
|
26 |
+
instructions=[
|
27 |
+
"1. Identify the target audience and their preferences for the topic: {topic}.",
|
28 |
+
"2. Conduct thorough research using credible sources such as industry reports, case studies, and reputable websites.",
|
29 |
+
"3. Gather data, statistics, and trends that support the topic and provide value to the audience.",
|
30 |
+
"4. Organize the research into clear sections (e.g., introduction, key points, examples, and conclusions).",
|
31 |
+
"5. Ensure all information is accurate, up-to-date, and properly cited.",
|
32 |
+
"6. Provide a summary of the research findings to the Content Writer for further development."],
|
33 |
+
allow_delegation=False,
|
34 |
+
verbose=True,
|
35 |
+
tools=[search_tool, scrape_tool]
|
36 |
+
)
|
37 |
+
|
38 |
+
writer = Agent(
|
39 |
+
role="Content Writer",
|
40 |
+
goal="Write a compelling, engaging, and well-structured blog post on {topic} based on the research provided.",
|
41 |
+
backstory="You are a skilled content writer with expertise in crafting blog posts that resonate with the target audience.",
|
42 |
+
instructions=[
|
43 |
+
"1. Review the research provided by the Researcher and identify the key points to include in the blog post.",
|
44 |
+
"2. Write a captivating headline and introduction to grab the reader's attention.",
|
45 |
+
"3. Structure the blog post into sections (e.g., introduction, main body, conclusion) for easy readability.",
|
46 |
+
"4. Use clear, concise, and engaging language to communicate the information effectively.",
|
47 |
+
"5. Incorporate relevant examples, statistics, and quotes from the research to support your points.",
|
48 |
+
"6. Ensure the tone and style align with the company's brand guidelines.",
|
49 |
+
"7. Optimize the content for SEO by including relevant keywords and meta descriptions.",
|
50 |
+
"8. Submit the draft to the Editor for review and refinement."
|
51 |
+
],
|
52 |
+
allow_delegation=False,
|
53 |
+
verbose=True,
|
54 |
+
tools=[search_tool, scrape_tool]
|
55 |
+
)
|
56 |
+
|
57 |
+
editor = Agent(
|
58 |
+
role="Content Editor",
|
59 |
+
goal="Review and refine the blog post to ensure it is polished, error-free, appropriate for the company's policy and ready for publication.",
|
60 |
+
backstory="You are an experienced content editor with a keen eye for detail and a deep understanding of the company's brand voice.",
|
61 |
+
instructions=[
|
62 |
+
"1. Review the blog post draft for grammar, spelling, and punctuation errors.",
|
63 |
+
"2. Ensure the content is clear, concise, and easy to understand.",
|
64 |
+
"3. Verify that the tone and style align with the company's brand guidelines.",
|
65 |
+
"4. Check that the blog post is well-structured and flows logically.",
|
66 |
+
"5. Confirm that all facts, statistics, and quotes are accurate and properly cited.",
|
67 |
+
"6. Provide constructive feedback to the Content Writer if revisions are needed.",
|
68 |
+
"7. Finalize the blog post and prepare it for publication."
|
69 |
+
],
|
70 |
+
allow_delegation=False,
|
71 |
+
verbose=True,
|
72 |
+
tools=[search_tool, scrape_tool]
|
73 |
+
)
|
74 |
+
|
75 |
+
'''search_tool = SerperDevTool()
|
76 |
+
scrape_tool = ScrapeWebsiteTool()'''
|
77 |
+
|
78 |
+
plan = Task(
|
79 |
+
description=(
|
80 |
+
"1. Analyze the topic: {topic} and identify its relevance to the target audience.\n"
|
81 |
+
"2. Research the latest trends, statistics, and news related to {topic}.\n"
|
82 |
+
"3. Identify key players, influencers, or brands in the industry related to {topic}.\n"
|
83 |
+
"4. Gather credible sources such as industry reports, case studies, and reputable websites.\n"
|
84 |
+
"5. Highlight pain points, challenges, and opportunities related to {topic}.\n"
|
85 |
+
"6. Organize the research into clear sections (e.g., trends, data, examples, and sources)."
|
86 |
+
),
|
87 |
+
expected_output=(
|
88 |
+
"A detailed research document containing:\n"
|
89 |
+
"- Key trends and statistics related to {topic}.\n"
|
90 |
+
"- Credible sources and references.\n"
|
91 |
+
"- Insights into the target audience's pain points and interests.\n"
|
92 |
+
"- A structured outline of the research findings."
|
93 |
+
),
|
94 |
+
tools=[search_tool, scrape_tool],
|
95 |
+
agent=researcher,
|
96 |
+
)
|
97 |
+
|
98 |
+
audience_analysis = Task(
|
99 |
+
description=(
|
100 |
+
"1. Define the target audience for the blog post on {topic}.\n"
|
101 |
+
"2. Identify the audience's demographics, interests, and preferences.\n"
|
102 |
+
"3. Analyze the audience's pain points and how {topic} can address them.\n"
|
103 |
+
"4. Determine the tone and style that will resonate with the audience.\n"
|
104 |
+
"5. Provide recommendations for engaging the audience effectively."
|
105 |
+
),
|
106 |
+
expected_output=(
|
107 |
+
"An audience analysis report containing:\n"
|
108 |
+
"- Demographics and psychographics of the target audience.\n"
|
109 |
+
"- Pain points and interests related to {topic}.\n"
|
110 |
+
"- Recommendations for tone, style, and engagement strategies."
|
111 |
+
),
|
112 |
+
tools=[search_tool, scrape_tool],
|
113 |
+
agent=researcher,
|
114 |
+
)
|
115 |
+
|
116 |
+
write = Task(
|
117 |
+
description=(
|
118 |
+
"1. Use the research document and audience analysis to create a blog draft.\n"
|
119 |
+
"2. Write a compelling headline and introduction to grab the reader's attention.\n"
|
120 |
+
"3. Structure the blog post into sections (e.g., introduction, main body, conclusion).\n"
|
121 |
+
"4. Incorporate key points, examples, and statistics from the research.\n"
|
122 |
+
"5. Use a tone and style that aligns with the target audience and brand guidelines.\n"
|
123 |
+
"6. Include a clear call-to-action (CTA) to engage the audience.\n"
|
124 |
+
"7. Optimize the content for SEO by including relevant keywords and meta descriptions."
|
125 |
+
),
|
126 |
+
expected_output=(
|
127 |
+
"A complete blog draft containing:\n"
|
128 |
+
"- A captivating headline and introduction.\n"
|
129 |
+
"- Well-structured sections with clear key points.\n"
|
130 |
+
"- Examples, statistics, and quotes from the research.\n"
|
131 |
+
"- A strong call-to-action (CTA).\n"
|
132 |
+
"- SEO-optimized content with keywords and meta descriptions."
|
133 |
+
),
|
134 |
+
tools=[search_tool, scrape_tool],
|
135 |
+
agent=writer,
|
136 |
+
)
|
137 |
+
|
138 |
+
revise_blog = Task(
|
139 |
+
description=(
|
140 |
+
"1. Review the Editor's feedback on the blog draft.\n"
|
141 |
+
"2. Make necessary revisions to improve clarity, tone, and structure.\n"
|
142 |
+
"3. Ensure all facts, statistics, and quotes are accurate and properly cited.\n"
|
143 |
+
"4. Double-check SEO optimization, including keywords and meta descriptions.\n"
|
144 |
+
"5. Submit the revised draft to the Editor for final approval."
|
145 |
+
),
|
146 |
+
expected_output=(
|
147 |
+
"A revised blog draft that incorporates all feedback and is ready for final review."
|
148 |
+
),
|
149 |
+
tools=[search_tool, scrape_tool],
|
150 |
+
agent=writer,
|
151 |
+
)
|
152 |
+
|
153 |
+
edit = Task(
|
154 |
+
description=(
|
155 |
+
"1. Review the blog draft for grammar, spelling, and punctuation errors.\n"
|
156 |
+
"2. Ensure the content is clear, concise, and easy to understand.\n"
|
157 |
+
"3. Verify that the tone and style align with the company's brand guidelines.\n"
|
158 |
+
"4. Check that the blog post is well-structured and flows logically.\n"
|
159 |
+
"5. Confirm that all facts, statistics, and quotes are accurate and properly cited.\n"
|
160 |
+
"6. Provide constructive feedback to the Writer for revisions if needed."
|
161 |
+
),
|
162 |
+
expected_output=(
|
163 |
+
"A reviewed blog draft with:\n"
|
164 |
+
"- Corrections for grammar, spelling, and punctuation.\n"
|
165 |
+
"- Suggestions for improving clarity, tone, and structure.\n"
|
166 |
+
"- Confirmation of factual accuracy and proper citations.\n"
|
167 |
+
"- Feedback for the Writer if revisions are required."
|
168 |
+
),
|
169 |
+
tools=[search_tool, scrape_tool],
|
170 |
+
agent=editor,
|
171 |
+
)
|
172 |
+
|
173 |
+
finalize_blog = Task(
|
174 |
+
description=(
|
175 |
+
"1. Review the revised blog draft from the Writer.\n"
|
176 |
+
"2. Ensure all feedback has been incorporated and the content is error-free.\n"
|
177 |
+
"3. Confirm that the blog post is optimized for SEO and aligns with brand guidelines.\n"
|
178 |
+
"4. Prepare the blog post for publication by formatting it appropriately.\n"
|
179 |
+
"5. Submit the finalized blog post for publishing."
|
180 |
+
),
|
181 |
+
expected_output=(
|
182 |
+
"A finalized blog post that is:\n"
|
183 |
+
"- Error-free and polished.\n"
|
184 |
+
"- SEO-optimized and aligned with brand guidelines.\n"
|
185 |
+
"- Ready for publication."
|
186 |
+
),
|
187 |
+
tools=[search_tool, scrape_tool],
|
188 |
+
agent=editor,
|
189 |
+
)
|
190 |
+
|
191 |
+
crew = Crew(
|
192 |
+
agents=[researcher, writer, editor],
|
193 |
+
tasks=[plan, audience_analysis, write, revise_blog, edit, finalize_blog],
|
194 |
+
verbose=True,
|
195 |
+
memory=True
|
196 |
+
)
|
197 |
+
|
198 |
+
result = crew.kickoff(inputs={"topic": topic})
|
199 |
+
return result.raw
|
200 |
+
|
201 |
+
|
202 |
+
|
203 |
+
def run_crewai(topic):
|
204 |
+
return generate_blog_post(topic)
|
205 |
+
|
206 |
+
iface = gr.Interface(
|
207 |
+
fn=run_crewai,
|
208 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter a topic for the blog post..."),
|
209 |
+
outputs=gr.Textbox(lines=40, placeholder="Generated blog post will appear here..."),
|
210 |
+
title="Blog Post Generator",
|
211 |
+
description="Enter a topic to generate a SEO approved blog post."
|
212 |
+
)
|
213 |
+
|
214 |
+
iface.launch()
|
requirements.txt
ADDED
Binary file (21.4 kB). View file
|
|
utils_groc.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv, find_dotenv
|
3 |
+
|
4 |
+
# tload env diel
|
5 |
+
def load_env():
|
6 |
+
_ = load_dotenv(find_dotenv())
|
7 |
+
|
8 |
+
def get_groc_api_key():
|
9 |
+
load_env()
|
10 |
+
groc_api_key = os.getenv("GROQ_API_KEY")
|
11 |
+
return groc_api_key
|
12 |
+
|
13 |
+
def get_serper_api_key():
|
14 |
+
load_env()
|
15 |
+
groc_api_key = os.getenv("SERPER_API_KEY")
|
16 |
+
return groc_api_key
|
17 |
+
|
18 |
+
|
19 |
+
# break to 50
|
20 |
+
# don't break in the middle
|
21 |
+
def pretty_print_result(result):
|
22 |
+
parsed_result = []
|
23 |
+
for line in result.split('\n'):
|
24 |
+
if len(line) > 50:
|
25 |
+
words = line.split(' ')
|
26 |
+
new_line = ''
|
27 |
+
for word in words:
|
28 |
+
if len(new_line) + len(word) + 1 > 50:
|
29 |
+
parsed_result.append(new_line)
|
30 |
+
new_line = word
|
31 |
+
else:
|
32 |
+
if new_line == '':
|
33 |
+
new_line = word
|
34 |
+
else:
|
35 |
+
new_line += ' ' + word
|
36 |
+
parsed_result.append(new_line)
|
37 |
+
else:
|
38 |
+
parsed_result.append(line)
|
39 |
+
return "\n".join(parsed_result)
|