AiNewsV2 / app.py
Phoenix21's picture
Rename main.py to app.py
c0b3c38 verified
raw
history blame
2.49 kB
import argparse
from datetime import datetime
import os
import getpass
import sys
from graph import create_blog_generator_workflow
from ui import display_workflow, display_blog, launch_ui
from core import generate_ai_news_blog
def main():
"""Main entry point for the application"""
parser = argparse.ArgumentParser(description="AI News Blog Generator")
parser.add_argument("--ui", action="store_true", help="Launch the web UI")
parser.add_argument("--date", type=str, help="Date to search for news (YYYY-MM-DD format)")
parser.add_argument("--groq-key", type=str, help="Groq API key")
parser.add_argument("--tavily-key", type=str, help="Tavily API key")
parser.add_argument("--output", type=str, help="Output file path")
args = parser.parse_args()
# Launch the web UI if requested
if args.ui:
print("Launching web UI...")
launch_ui()
return
# Otherwise, run the CLI version
try:
# Get API keys if not provided
groq_key = args.groq_key
if not groq_key and not os.environ.get("GROQ_API_KEY"):
groq_key = getpass.getpass("Enter the Groq API key: ")
tavily_key = args.tavily_key
if not tavily_key and not os.environ.get("TAVILY_API_KEY"):
tavily_key = getpass.getpass("Enter the Tavily API key: ")
# Display the workflow graph
print("Generating AI News Blog...")
# Generate the blog
blog_content = generate_ai_news_blog(groq_key, tavily_key, args.date)
# Save to file if output path provided
if args.output:
with open(args.output, "w") as f:
f.write(blog_content)
print(f"Blog saved to {args.output}")
else:
# Otherwise, save to default file
output_file = f"ai_news_blog_{datetime.now().strftime('%Y-%m-%d')}.md"
with open(output_file, "w") as f:
f.write(blog_content)
print(f"Blog saved to {output_file}")
# Try to display the blog if running in a notebook
try:
display_blog(blog_content)
except:
print("Blog generated successfully.")
except KeyboardInterrupt:
print("\nOperation cancelled by user.")
sys.exit(0)
except Exception as e:
print(f"Error running the pipeline: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
main()