Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from crewai import Agent, Task, Crew, LLM
|
4 |
+
from crewai_tools import SerperDevTool
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
# Load environment variables
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# Streamlit page config
|
11 |
+
st.set_page_config(page_title="AI News Generator", page_icon="📰", layout="wide")
|
12 |
+
|
13 |
+
# Title and description
|
14 |
+
st.title("🤖 AI News Generator, powered by CrewAI and Cohere's Command R7B")
|
15 |
+
st.markdown("Generate comprehensive blog posts about any topic using AI agents.")
|
16 |
+
|
17 |
+
# Sidebar
|
18 |
+
with st.sidebar:
|
19 |
+
st.header("Content Settings")
|
20 |
+
|
21 |
+
# Make the text input take up more space
|
22 |
+
topic = st.text_area(
|
23 |
+
"Enter your topic",
|
24 |
+
height=100,
|
25 |
+
placeholder="Enter the topic you want to generate content about..."
|
26 |
+
)
|
27 |
+
|
28 |
+
# Add more sidebar controls if needed
|
29 |
+
st.markdown("### Advanced Settings")
|
30 |
+
temperature = st.slider("Temperature", 0.0, 1.0, 0.7)
|
31 |
+
|
32 |
+
# Add some spacing
|
33 |
+
st.markdown("---")
|
34 |
+
|
35 |
+
# Make the generate button more prominent in the sidebar
|
36 |
+
generate_button = st.button("Generate Content", type="primary", use_container_width=True)
|
37 |
+
|
38 |
+
# Add some helpful information
|
39 |
+
with st.expander("ℹ️ How to use"):
|
40 |
+
st.markdown("""
|
41 |
+
1. Enter your desired topic in the text area above
|
42 |
+
2. Adjust the temperature if needed (higher = more creative)
|
43 |
+
3. Click 'Generate Content' to start
|
44 |
+
4. Wait for the AI to generate your article
|
45 |
+
5. Download the result as a markdown file
|
46 |
+
""")
|
47 |
+
|
48 |
+
def generate_content(topic):
|
49 |
+
llm = LLM(
|
50 |
+
model="command-r",
|
51 |
+
temperature=0.7
|
52 |
+
)
|
53 |
+
|
54 |
+
search_tool = SerperDevTool(n_results=10)
|
55 |
+
|
56 |
+
# First Agent: Senior Research Analyst
|
57 |
+
senior_research_analyst = Agent(
|
58 |
+
role="Senior Research Analyst",
|
59 |
+
goal=f"Research, analyze, and synthesize comprehensive information on {topic} from reliable web sources",
|
60 |
+
backstory="You're an expert research analyst with advanced web research skills. "
|
61 |
+
"You excel at finding, analyzing, and synthesizing information from "
|
62 |
+
"across the internet using search tools. You're skilled at "
|
63 |
+
"distinguishing reliable sources from unreliable ones, "
|
64 |
+
"fact-checking, cross-referencing information, and "
|
65 |
+
"identifying key patterns and insights. You provide "
|
66 |
+
"well-organized research briefs with proper citations "
|
67 |
+
"and source verification. Your analysis includes both "
|
68 |
+
"raw data and interpreted insights, making complex "
|
69 |
+
"information accessible and actionable.",
|
70 |
+
allow_delegation=False,
|
71 |
+
verbose=True,
|
72 |
+
tools=[search_tool],
|
73 |
+
llm=llm
|
74 |
+
)
|
75 |
+
|
76 |
+
# Second Agent: Content Writer
|
77 |
+
content_writer = Agent(
|
78 |
+
role="Content Writer",
|
79 |
+
goal="Transform research findings into engaging blog posts while maintaining accuracy",
|
80 |
+
backstory="You're a skilled content writer specialized in creating "
|
81 |
+
"engaging, accessible content from technical research. "
|
82 |
+
"You work closely with the Senior Research Analyst and excel at maintaining the perfect "
|
83 |
+
"balance between informative and entertaining writing, "
|
84 |
+
"while ensuring all facts and citations from the research "
|
85 |
+
"are properly incorporated. You have a talent for making "
|
86 |
+
"complex topics approachable without oversimplifying them.",
|
87 |
+
allow_delegation=False,
|
88 |
+
verbose=True,
|
89 |
+
llm=llm
|
90 |
+
)
|
91 |
+
|
92 |
+
# Research Task
|
93 |
+
research_task = Task(
|
94 |
+
description=("""
|
95 |
+
1. Conduct comprehensive research on {topic} including:
|
96 |
+
- Recent developments and news
|
97 |
+
- Key industry trends and innovations
|
98 |
+
- Expert opinions and analyses
|
99 |
+
- Statistical data and market insights
|
100 |
+
2. Evaluate source credibility and fact-check all information
|
101 |
+
3. Organize findings into a structured research brief
|
102 |
+
4. Include all relevant citations and sources
|
103 |
+
"""),
|
104 |
+
expected_output="""A detailed research report containing:
|
105 |
+
- Executive summary of key findings
|
106 |
+
- Comprehensive analysis of current trends and developments
|
107 |
+
- List of verified facts and statistics
|
108 |
+
- All citations and links to original sources
|
109 |
+
- Clear categorization of main themes and patterns
|
110 |
+
Please format with clear sections and bullet points for easy reference.""",
|
111 |
+
agent=senior_research_analyst
|
112 |
+
)
|
113 |
+
|
114 |
+
# Writing Task
|
115 |
+
writing_task = Task(
|
116 |
+
description=("""
|
117 |
+
Using the research brief provided, create an engaging blog post that:
|
118 |
+
1. Transforms technical information into accessible content
|
119 |
+
2. Maintains all factual accuracy and citations from the research
|
120 |
+
3. Includes:
|
121 |
+
- Attention-grabbing introduction
|
122 |
+
- Well-structured body sections with clear headings
|
123 |
+
- Compelling conclusion
|
124 |
+
4. Preserves all source citations in [Source: URL] format
|
125 |
+
5. Includes a References section at the end
|
126 |
+
"""),
|
127 |
+
expected_output="""A polished blog post in markdown format that:
|
128 |
+
- Engages readers while maintaining accuracy
|
129 |
+
- Contains properly structured sections
|
130 |
+
- Includes Inline citations hyperlinked to the original source url
|
131 |
+
- Presents information in an accessible yet informative way
|
132 |
+
- Follows proper markdown formatting, use H1 for the title and H3 for the sub-sections""",
|
133 |
+
agent=content_writer
|
134 |
+
)
|
135 |
+
|
136 |
+
# Create Crew
|
137 |
+
crew = Crew(
|
138 |
+
agents=[senior_research_analyst, content_writer],
|
139 |
+
tasks=[research_task, writing_task],
|
140 |
+
verbose=True
|
141 |
+
)
|
142 |
+
|
143 |
+
return crew.kickoff(inputs={"topic": topic})
|
144 |
+
|
145 |
+
# Main content area
|
146 |
+
if generate_button:
|
147 |
+
with st.spinner('Generating content... This may take a moment.'):
|
148 |
+
try:
|
149 |
+
result = generate_content(topic)
|
150 |
+
st.markdown("### Generated Content")
|
151 |
+
st.markdown(result)
|
152 |
+
|
153 |
+
# Add download button
|
154 |
+
st.download_button(
|
155 |
+
label="Download Content",
|
156 |
+
data=result.raw,
|
157 |
+
file_name=f"{topic.lower().replace(' ', '_')}_article.md",
|
158 |
+
mime="text/markdown"
|
159 |
+
)
|
160 |
+
except Exception as e:
|
161 |
+
st.error(f"An error occurred: {str(e)}")
|
162 |
+
|
163 |
+
# Footer
|
164 |
+
st.markdown("---")
|
165 |
+
st.markdown("Built with CrewAI, Streamlit and powered by Cohere's Command R7B")
|