Akshayram1 commited on
Commit
9f50ed7
·
verified ·
1 Parent(s): 774cd2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -38
app.py CHANGED
@@ -4,7 +4,8 @@ from crewai_tools import WebsiteSearchTool
4
  from dotenv import load_dotenv
5
  import os
6
  from fpdf import FPDF
7
- from io import BytesIO # To handle in-memory files
 
8
 
9
  # Load environment variables
10
  load_dotenv()
@@ -56,22 +57,42 @@ if generate_blog:
56
  # Define tasks
57
  research_task = Task(
58
  description=(
59
- "Identify the News Article and get detailed information about the News from the website."
 
 
 
 
 
60
  ),
61
- expected_output='A comprehensive 3-paragraph-long report based on the {topic} of News.',
62
- tools=[web_tool],
63
- agent=blog_researcher,
 
 
 
 
 
 
64
  )
65
 
66
  write_task = Task(
67
  description=(
68
- "Get the info from the News Website on the topic {topic}."
 
 
 
 
 
69
  ),
70
- expected_output='Summarize the info from the News website on the topic {topic} and create the content for the blog.',
71
- tools=[web_tool],
72
- agent=blog_writer,
73
- async_execution=False,
74
- output_file="" # Provide an empty string or valid file path
 
 
 
 
75
  )
76
 
77
  # Create crew
@@ -79,28 +100,19 @@ if generate_blog:
79
  agents=[blog_researcher, blog_writer],
80
  tasks=[research_task, write_task],
81
  process=Process.sequential,
82
- memory=True,
83
- cache=True,
84
- max_rpm=100,
85
- share_crew=True
86
  )
87
 
88
- # Kickoff the process and fetch the result
89
  try:
90
- result = crew.kickoff(inputs={'topic': news_link})
91
-
92
- # Access task outputs
93
- try:
94
- task_outputs = result.tasks_output
95
- except AttributeError:
96
- st.error("The result object does not have 'tasks_output'.")
97
- task_outputs = []
98
-
99
- # Extract blog content
100
- try:
101
- blog_content = task_outputs[1].raw
102
- except (IndexError, AttributeError):
103
- blog_content = "Unable to fetch blog content from the task outputs."
104
 
105
  # Display the blog content
106
  st.subheader("Generated Blog")
@@ -113,18 +125,42 @@ if generate_blog:
113
 
114
  if save_blog:
115
  try:
116
- # Generate PDF in-memory
117
  pdf = FPDF()
118
  pdf.add_page()
 
 
 
 
 
 
 
 
119
  pdf.set_font("Arial", size=12)
120
- pdf.multi_cell(0, 10, blog_content)
121
 
122
- # Use BytesIO for in-memory file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  pdf_buffer = BytesIO()
124
  pdf.output(pdf_buffer)
125
- pdf_buffer.seek(0) # Reset buffer pointer
126
 
127
- # Provide download button
128
  st.download_button(
129
  label="Download Blog as PDF",
130
  data=pdf_buffer,
@@ -132,16 +168,18 @@ if generate_blog:
132
  mime="application/pdf"
133
  )
134
  st.success("Blog saved as PDF and ready for download!")
 
135
  except Exception as e:
136
- st.error(f"Failed to save blog as PDF: {e}")
 
137
 
138
  if reject_blog:
139
  st.warning("Blog rejected. No file was saved.")
140
 
141
  except Exception as e:
142
- st.error(f"An error occurred during the process: {e}")
143
 
144
  else:
145
  st.error("Please enter a valid news link.")
146
  else:
147
- st.error("Please provide your OpenAI API Key.")
 
4
  from dotenv import load_dotenv
5
  import os
6
  from fpdf import FPDF
7
+ from io import BytesIO
8
+ import textwrap
9
 
10
  # Load environment variables
11
  load_dotenv()
 
57
  # Define tasks
58
  research_task = Task(
59
  description=(
60
+ "Research Task: "
61
+ "1. Visit the provided news link and analyze the content thoroughly\n"
62
+ "2. Extract key information about the AI-related news or development\n"
63
+ "3. Identify main points, technological aspects, and potential impact\n"
64
+ "4. Gather any relevant background information or context\n"
65
+ "5. Note any quotes, statistics, or specific technical details"
66
  ),
67
+ expected_output=(
68
+ "A detailed research report containing:\n"
69
+ "- Main story/development summary\n"
70
+ "- Technical aspects and innovations\n"
71
+ "- Impact and implications\n"
72
+ "- Supporting facts and context\n"
73
+ "- Notable quotes or statistics"
74
+ ),
75
+ agent=blog_researcher
76
  )
77
 
78
  write_task = Task(
79
  description=(
80
+ "Writing Task:\n"
81
+ "1. Use the research findings to craft an engaging blog post\n"
82
+ "2. Structure the content with clear introduction, body, and conclusion\n"
83
+ "3. Explain technical concepts in an accessible manner\n"
84
+ "4. Include relevant examples and real-world applications\n"
85
+ "5. Add reference links at the end"
86
  ),
87
+ expected_output=(
88
+ "A well-structured blog post that includes:\n"
89
+ "- Engaging headline and introduction\n"
90
+ "- Clear explanation of the AI development\n"
91
+ "- Technical details explained simply\n"
92
+ "- Impact and future implications\n"
93
+ "- Reference links"
94
+ ),
95
+ agent=blog_writer
96
  )
97
 
98
  # Create crew
 
100
  agents=[blog_researcher, blog_writer],
101
  tasks=[research_task, write_task],
102
  process=Process.sequential,
103
+ verbose=2
 
 
 
104
  )
105
 
 
106
  try:
107
+ # Kickoff the process
108
+ result = crew.kickoff()
109
+
110
+ # Extract blog content from the result
111
+ if hasattr(result, 'task_output'):
112
+ blog_content = result.task_output
113
+ else:
114
+ # If task_output is not available, try to get the raw result
115
+ blog_content = str(result)
 
 
 
 
 
116
 
117
  # Display the blog content
118
  st.subheader("Generated Blog")
 
125
 
126
  if save_blog:
127
  try:
128
+ # Create PDF with proper text wrapping and formatting
129
  pdf = FPDF()
130
  pdf.add_page()
131
+ pdf.set_auto_page_break(auto=True, margin=15)
132
+
133
+ # Add title
134
+ pdf.set_font("Arial", "B", 16)
135
+ pdf.cell(0, 10, "Generated Blog", ln=True, align='C')
136
+ pdf.ln(10)
137
+
138
+ # Add content with proper formatting
139
  pdf.set_font("Arial", size=12)
 
140
 
141
+ # Process and wrap text
142
+ effective_page_width = pdf.w - 2 * pdf.l_margin
143
+
144
+ # Split content into paragraphs
145
+ paragraphs = blog_content.split('\n')
146
+
147
+ for paragraph in paragraphs:
148
+ if paragraph.strip(): # Only process non-empty paragraphs
149
+ # Wrap text to fit page width
150
+ wrapped_text = textwrap.fill(paragraph, width=95)
151
+
152
+ # Split wrapped text into lines and write them
153
+ lines = wrapped_text.split('\n')
154
+ for line in lines:
155
+ pdf.multi_cell(0, 10, line)
156
+ pdf.ln(5) # Add small space between paragraphs
157
+
158
+ # Generate PDF bytes
159
  pdf_buffer = BytesIO()
160
  pdf.output(pdf_buffer)
161
+ pdf_buffer.seek(0)
162
 
163
+ # Create download button
164
  st.download_button(
165
  label="Download Blog as PDF",
166
  data=pdf_buffer,
 
168
  mime="application/pdf"
169
  )
170
  st.success("Blog saved as PDF and ready for download!")
171
+
172
  except Exception as e:
173
+ st.error(f"Failed to save blog as PDF: {str(e)}")
174
+ st.error("PDF Generation Error Details:", exc_info=True)
175
 
176
  if reject_blog:
177
  st.warning("Blog rejected. No file was saved.")
178
 
179
  except Exception as e:
180
+ st.error(f"An error occurred during the process: {str(e)}")
181
 
182
  else:
183
  st.error("Please enter a valid news link.")
184
  else:
185
+ st.error("Please provide your OpenAI API Key.")