Akshayram1 commited on
Commit
7df6f57
·
verified ·
1 Parent(s): a9f8d58

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +151 -0
app.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from crewai import Agent, Task, Crew, Process
3
+ from crewai_tools import WebsiteSearchTool
4
+ from dotenv import load_dotenv
5
+ import os
6
+ from fpdf import FPDF
7
+
8
+ # Load environment variables
9
+ load_dotenv()
10
+
11
+ # Streamlit App
12
+ st.title("Blog Generator with AI")
13
+ st.sidebar.header("Input")
14
+
15
+ # OpenAI API key input
16
+ user_api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="password")
17
+ news_link = st.sidebar.text_input("Enter News Link", "")
18
+ generate_blog = st.sidebar.button("Generate Blog")
19
+
20
+ if generate_blog:
21
+ if user_api_key:
22
+ # Set the OpenAI API key dynamically
23
+ os.environ["OPENAI_API_KEY"] = user_api_key
24
+
25
+ if news_link:
26
+ st.info("Fetching and processing the news...")
27
+
28
+ # Define tools
29
+ web_tool = WebsiteSearchTool()
30
+
31
+ # Create agents
32
+ blog_researcher = Agent(
33
+ role='AI Blog Researcher from News Website',
34
+ goal='Get the relevant latest AI related news from News Website',
35
+ verbose=True,
36
+ memory=True,
37
+ backstory=("Expert in understanding videos in AI, Data Science, Machine Learning, and GEN AI."),
38
+ tools=[web_tool],
39
+ allow_delegation=True
40
+ )
41
+
42
+ blog_writer = Agent(
43
+ role='Blog Writer',
44
+ goal='Narrate compelling tech stories about the News Article and add the reference links at the end of the blog.',
45
+ verbose=True,
46
+ memory=True,
47
+ backstory=(
48
+ "With a flair for simplifying complex topics, you craft engaging narratives that captivate and educate,"
49
+ "bringing new discoveries to light in an accessible manner."
50
+ ),
51
+ tools=[web_tool],
52
+ allow_delegation=False
53
+ )
54
+
55
+ # Define tasks
56
+ research_task = Task(
57
+ description=(
58
+ "Identify the News Article and get detailed information about the News from the website."
59
+ ),
60
+ expected_output='A comprehensive 3-paragraph-long report based on the {topic} of News.',
61
+ tools=[web_tool],
62
+ agent=blog_researcher,
63
+ )
64
+
65
+ write_task = Task(
66
+ description=(
67
+ "Get the info from the News Website on the topic {topic}."
68
+ ),
69
+ expected_output='Summarize the info from the News website on the topic {topic} and create the content for the blog.',
70
+ tools=[web_tool],
71
+ agent=blog_writer,
72
+ async_execution=False,
73
+ output_file="" # Provide an empty string or valid file path
74
+ )
75
+
76
+ # Create crew
77
+ crew = Crew(
78
+ agents=[blog_researcher, blog_writer],
79
+ tasks=[research_task, write_task],
80
+ process=Process.sequential,
81
+ memory=True,
82
+ cache=True,
83
+ max_rpm=100,
84
+ share_crew=True
85
+ )
86
+
87
+ # Kickoff the process and fetch the result
88
+ try:
89
+ result = crew.kickoff(inputs={'topic': news_link})
90
+
91
+ # Inspect result attributes for debugging
92
+ st.subheader("Result Attributes")
93
+ st.write(dir(result))
94
+
95
+ # Access task outputs
96
+ try:
97
+ task_outputs = result.tasks_output
98
+ except AttributeError:
99
+ st.error("The result object does not have 'tasks_output'.")
100
+ task_outputs = []
101
+
102
+ # Display task outputs
103
+ st.subheader("Task Outputs")
104
+ for idx, task_output in enumerate(task_outputs):
105
+ st.write(f"Task {idx + 1}:")
106
+ st.json(task_output)
107
+
108
+ # Extract blog content
109
+ try:
110
+ blog_content = task_outputs[1].raw
111
+ except (IndexError, AttributeError):
112
+ blog_content = "Unable to fetch blog content from the task outputs."
113
+
114
+ # Display the blog content
115
+ st.subheader("Generated Blog")
116
+ st.text_area("Blog Content", value=blog_content, height=400)
117
+
118
+ # Actions to save or reject the blog
119
+ st.subheader("Actions")
120
+ save_blog = st.button("Save Blog as PDF")
121
+ reject_blog = st.button("Reject Blog")
122
+
123
+ if save_blog:
124
+ try:
125
+ pdf = FPDF()
126
+ pdf.add_page()
127
+ pdf.set_font("Arial", size=12)
128
+ pdf.multi_cell(0, 10, blog_content)
129
+ pdf.output("generated_blog.pdf")
130
+
131
+ with open("generated_blog.pdf", "rb") as pdf_file:
132
+ st.download_button(
133
+ label="Download Blog as PDF",
134
+ data=pdf_file,
135
+ file_name="generated_blog.pdf",
136
+ mime="application/pdf"
137
+ )
138
+ st.success("Blog saved as PDF and ready for download!")
139
+ except Exception as e:
140
+ st.error(f"Failed to save blog as PDF: {e}")
141
+
142
+ if reject_blog:
143
+ st.warning("Blog rejected. No file was saved.")
144
+
145
+ except Exception as e:
146
+ st.error(f"An error occurred during the process: {e}")
147
+
148
+ else:
149
+ st.error("Please enter a valid news link.")
150
+ else:
151
+ st.error("Please provide your OpenAI API Key.")