skjaini commited on
Commit
87b46ad
·
verified ·
1 Parent(s): 0f65eb3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -0
app.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from crewai import Crew, Agent, Task, Process
3
+ from langchain_community.tools import DuckDuckGoSearchRun
4
+ #from langchain_openai import ChatOpenAI # Remove OpenAI
5
+ from langchain_community.llms import HuggingFaceHub # Import Hugging Face Hub
6
+ import datetime
7
+ import os
8
+
9
+ # --- Environment Setup ---
10
+ # Make sure to set your HUGGINGFACEHUB_API_TOKEN in your environment variables.
11
+
12
+ huggingfacehub_api_token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
13
+
14
+
15
+ # --- Helper Functions ---
16
+
17
+ def get_date_range():
18
+ """Calculates yesterday's date for the search query."""
19
+ today = datetime.date.today()
20
+ yesterday = today - datetime.timedelta(days=1)
21
+ return yesterday.strftime("%Y-%m-%d")
22
+
23
+
24
+
25
+ # --- Agent and Task Definitions ---
26
+
27
+ def create_ai_news_crew():
28
+ """Creates the CrewAI crew, agents, and tasks."""
29
+
30
+ search_tool = DuckDuckGoSearchRun()
31
+
32
+ # Define Agents
33
+ researcher = Agent(
34
+ role='AI News Researcher',
35
+ goal='Find the most recent and relevant AI news articles from yesterday',
36
+ backstory="""You are a specialized AI research agent
37
+ focused on finding the most relevant and impactful news articles
38
+ related to Artificial Intelligence. You excel at using search
39
+ tools effectively to find information.""",
40
+ verbose=True,
41
+ allow_delegation=False,
42
+ tools=[search_tool],
43
+ llm=HuggingFaceHub(
44
+ repo_id="deepseek-ai/DeepSeek-Coder-33B-Instruct", # Use the DeepSeek-Coder model (Instruct version is better for this task)
45
+ model_kwargs={"temperature": 0.5, "max_new_tokens": 1024, "repetition_penalty": 1.2}, # Added repetition penalty
46
+ huggingfacehub_api_token=huggingfacehub_api_token,
47
+ )
48
+ )
49
+
50
+ summarizer = Agent(
51
+ role='AI News Summarizer',
52
+ goal='Summarize the key news articles and create a concise daily briefing',
53
+ backstory="""You are an expert at taking multiple pieces of information
54
+ and condensing them into clear, concise, and informative summaries.
55
+ You are writing for a busy executive who needs to stay up-to-date
56
+ on AI developments quickly.""",
57
+ verbose=True,
58
+ allow_delegation=False,
59
+ llm=HuggingFaceHub(
60
+ repo_id="deepseek-ai/DeepSeek-Coder-33B-Instruct", #Use DeepSeek-Coder model
61
+ model_kwargs={"temperature": 0.2, "max_new_tokens": 1024, "repetition_penalty": 1.2}, # Lower temp, high rep penalty for concise output
62
+ huggingfacehub_api_token=huggingfacehub_api_token,
63
+ )
64
+ )
65
+
66
+ # Define Tasks
67
+ yesterday_str = get_date_range()
68
+ research_task = Task(
69
+ description=f"""Find at least 5 relevant news articles about Artificial Intelligence
70
+ published on {yesterday_str}. Focus on major breakthroughs,
71
+ industry news, ethical considerations, and new applications of AI.
72
+ Return the titles and URLs of the most important articles.
73
+ """,
74
+ agent=researcher
75
+ )
76
+
77
+ summarize_task = Task(
78
+ description="""Using the news articles identified, create a daily AI news
79
+ briefing. The briefing should be no more than 500 words and should
80
+ cover the 3-5 most important AI news items from yesterday. Include
81
+ a very brief (1-2 sentence) summary of each item and, if possible, link to the source.
82
+ Format the output using markdown for readability.
83
+ """,
84
+ agent=summarizer
85
+ )
86
+
87
+ # Create Crew
88
+ crew = Crew(
89
+ agents=[researcher, summarizer],
90
+ tasks=[research_task, summarize_task],
91
+ verbose=True, # You can set it to 1 or 2 to different level of logs
92
+ process=Process.sequential # Tasks are executed sequentially
93
+ )
94
+ return crew
95
+
96
+
97
+
98
+
99
+ # --- Streamlit App ---
100
+
101
+ def main():
102
+ """Main function to run the Streamlit app."""
103
+
104
+ st.set_page_config(
105
+ page_title="AI Daily News Briefing",
106
+ page_icon="🤖",
107
+ layout="wide"
108
+ )
109
+
110
+ st.title("AI Daily News Briefing 🤖")
111
+ st.write("Get a concise summary of the most important AI news from yesterday.")
112
+
113
+ if st.button("Generate Briefing"):
114
+ with st.spinner("Generating your daily AI news briefing..."):
115
+ try:
116
+ crew = create_ai_news_crew()
117
+ result = crew.kickoff() # Start the crew's work
118
+ st.subheader("Your AI News Briefing:")
119
+ st.markdown(result)
120
+
121
+ except Exception as e:
122
+ st.error(f"An error occurred: {e}")
123
+ st.error("Please check your API key and ensure you have set up the environment correctly.")
124
+
125
+
126
+
127
+ if __name__ == "__main__":
128
+ if not huggingfacehub_api_token:
129
+ st.error("HUGGINGFACEHUB_API_TOKEN is not set. Please set it as an environment variable.")
130
+ else:
131
+ main()