Akshayram1 commited on
Commit
e5b00f3
·
verified ·
1 Parent(s): c940863

Update app.py

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