Spaces:
Running
Running
| import os | |
| import csv | |
| from dotenv import load_dotenv | |
| from social_media_crew import SocialMediaCrew | |
| import mlflow | |
| mlflow.crewai.autolog() | |
| # Optional: Set a tracking URI and an experiment name if you have a tracking server | |
| mlflow.set_tracking_uri("http://localhost:5000") | |
| mlflow.set_experiment("CrewAI") | |
| # Load environment variables from .env file | |
| load_dotenv() | |
| # Now you can safely access the API key | |
| # Make sure your .env file has OPENAI_API_KEY="your_key_here" | |
| api_key = os.getenv("OPENAI_API_KEY") | |
| natura_api_token = os.getenv("NATURA_API_TOKEN") | |
| if not api_key: | |
| raise ValueError("OPENAI_API_KEY not found in .env file or environment variables.") | |
| if not natura_api_token: | |
| raise ValueError("NATURA_API_TOKEN not found in .env file or environment variables. Please set it for the URL shortener tool.") | |
| CSV_FILE = 'urls.csv' | |
| MARKDOWN_FILE = 'social_media_ads.md' | |
| all_results = [] | |
| # Initialize the SocialMediaCrew | |
| social_media_crew = SocialMediaCrew() | |
| try: | |
| with open(CSV_FILE, mode='r', encoding='utf-8') as file: | |
| reader = csv.DictReader(file) | |
| for row in reader: | |
| current_url = row['url'] | |
| # Run the crew for the current URL | |
| result = social_media_crew.run_crew(current_url) | |
| print("######################") | |
| print("Crew work finished for URL:", current_url) | |
| print("Final result:") | |
| print(result) | |
| print("######################\n") | |
| all_results.append({'url': current_url, 'ad': result}) | |
| # Write all results to a Markdown file | |
| with open(MARKDOWN_FILE, mode='w', encoding='utf-8') as md_file: | |
| md_file.write("# Social Media Ads for Perfumes\n\n") | |
| for item in all_results: | |
| md_file.write(f"## URL: {item['url']}\n\n") | |
| md_file.write(f"{item['ad']}\n\n---\n\n") | |
| print(f"All social media ads have been written to '{MARKDOWN_FILE}'") | |
| except FileNotFoundError: | |
| print(f"Error: The CSV file '{CSV_FILE}' was not found. Please create it with a 'url' column.") | |
| except KeyError: | |
| print(f"Error: The CSV file '{CSV_FILE}' must contain a 'url' column.") | |
| except Exception as e: | |
| print(f"An unexpected error occurred: {e}") | |