Spaces:
Sleeping
Sleeping
import streamlit as st | |
from crewai import Crew, Agent, Task, Process | |
from langchain_core.tools import Tool | |
from langchain_community.tools import DuckDuckGoSearchRun | |
from langchain_community.llms import HuggingFaceHub # Change back to HuggingFaceHub | |
import datetime | |
import os | |
# --- Environment Setup --- | |
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_instance = DuckDuckGoSearchRun() | |
# Wrap the DuckDuckGoSearchRun instance with the Tool class. | |
search_tool = Tool( | |
name="DuckDuckGo Search", | |
func=search_tool_instance.run, | |
description="Useful for searching the web for recent AI news articles.", | |
) | |
# 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( # Use HuggingFaceHub | |
repo_id="deepseek-ai/DeepSeek-Coder-33B-Instruct", #Specify the model | |
model_kwargs={"temperature": 0.5, "max_new_tokens": 1024, "repetition_penalty": 1.2}, # Add model parameters here | |
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( # Use HuggingFaceHub | |
repo_id="deepseek-ai/DeepSeek-Coder-33B-Instruct", #Specify model | |
model_kwargs={"temperature": 0.2, "max_new_tokens": 1024, "repetition_penalty": 1.2}, #Add model parameters here | |
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, | |
expected_output="A list of titles and URLs of the most important AI news articles from yesterday." | |
) | |
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, | |
expected_output="A concise daily AI news briefing in markdown format, no more than 500 words." | |
) | |
# Create Crew | |
crew = Crew( | |
agents=[researcher, summarizer], | |
tasks=[research_task, summarize_task], | |
verbose=True, | |
process=Process.sequential | |
) | |
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() | |
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() |