DeepSeek-AINews / app.py
skjaini's picture
Create app.py
87b46ad verified
raw
history blame
4.85 kB
import streamlit as st
from crewai import Crew, Agent, Task, Process
from langchain_community.tools import DuckDuckGoSearchRun
#from langchain_openai import ChatOpenAI # Remove OpenAI
from langchain_community.llms import HuggingFaceHub # Import Hugging Face Hub
import datetime
import os
# --- Environment Setup ---
# Make sure to set your HUGGINGFACEHUB_API_TOKEN in your environment variables.
huggingfacehub_api_token = os.environ.get("HUGGINGFACEHUB_API_TOKEN")
# --- Helper Functions ---
def get_date_range():
"""Calculates yesterday's date for the search query."""
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
return yesterday.strftime("%Y-%m-%d")
# --- Agent and Task Definitions ---
def create_ai_news_crew():
"""Creates the CrewAI crew, agents, and tasks."""
search_tool = DuckDuckGoSearchRun()
# Define Agents
researcher = Agent(
role='AI News Researcher',
goal='Find the most recent and relevant AI news articles from yesterday',
backstory="""You are a specialized AI research agent
focused on finding the most relevant and impactful news articles
related to Artificial Intelligence. You excel at using search
tools effectively to find information.""",
verbose=True,
allow_delegation=False,
tools=[search_tool],
llm=HuggingFaceHub(
repo_id="deepseek-ai/DeepSeek-Coder-33B-Instruct", # Use the DeepSeek-Coder model (Instruct version is better for this task)
model_kwargs={"temperature": 0.5, "max_new_tokens": 1024, "repetition_penalty": 1.2}, # Added repetition penalty
huggingfacehub_api_token=huggingfacehub_api_token,
)
)
summarizer = Agent(
role='AI News Summarizer',
goal='Summarize the key news articles and create a concise daily briefing',
backstory="""You are an expert at taking multiple pieces of information
and condensing them into clear, concise, and informative summaries.
You are writing for a busy executive who needs to stay up-to-date
on AI developments quickly.""",
verbose=True,
allow_delegation=False,
llm=HuggingFaceHub(
repo_id="deepseek-ai/DeepSeek-Coder-33B-Instruct", #Use DeepSeek-Coder model
model_kwargs={"temperature": 0.2, "max_new_tokens": 1024, "repetition_penalty": 1.2}, # Lower temp, high rep penalty for concise output
huggingfacehub_api_token=huggingfacehub_api_token,
)
)
# Define Tasks
yesterday_str = get_date_range()
research_task = Task(
description=f"""Find at least 5 relevant news articles about Artificial Intelligence
published on {yesterday_str}. Focus on major breakthroughs,
industry news, ethical considerations, and new applications of AI.
Return the titles and URLs of the most important articles.
""",
agent=researcher
)
summarize_task = Task(
description="""Using the news articles identified, create a daily AI news
briefing. The briefing should be no more than 500 words and should
cover the 3-5 most important AI news items from yesterday. Include
a very brief (1-2 sentence) summary of each item and, if possible, link to the source.
Format the output using markdown for readability.
""",
agent=summarizer
)
# Create Crew
crew = Crew(
agents=[researcher, summarizer],
tasks=[research_task, summarize_task],
verbose=True, # You can set it to 1 or 2 to different level of logs
process=Process.sequential # Tasks are executed sequentially
)
return crew
# --- Streamlit App ---
def main():
"""Main function to run the Streamlit app."""
st.set_page_config(
page_title="AI Daily News Briefing",
page_icon="πŸ€–",
layout="wide"
)
st.title("AI Daily News Briefing πŸ€–")
st.write("Get a concise summary of the most important AI news from yesterday.")
if st.button("Generate Briefing"):
with st.spinner("Generating your daily AI news briefing..."):
try:
crew = create_ai_news_crew()
result = crew.kickoff() # Start the crew's work
st.subheader("Your AI News Briefing:")
st.markdown(result)
except Exception as e:
st.error(f"An error occurred: {e}")
st.error("Please check your API key and ensure you have set up the environment correctly.")
if __name__ == "__main__":
if not huggingfacehub_api_token:
st.error("HUGGINGFACEHUB_API_TOKEN is not set. Please set it as an environment variable.")
else:
main()