File size: 2,209 Bytes
94a7d52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67


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}")