diff --git a/backend/brand_style.py b/backend/brand_style.py index d337e93..58ba16a 100644 --- a/backend/brand_style.py +++ b/backend/brand_style.py @@ -8,10 +8,10 @@ from vector_store import VectorStore from config import settings class BrandStyleManager: - def __init__(self): + def __init__(self, embeddings: CohereEmbeddings, vector_store: VectorStore): self.settings = settings - self.embeddings = CohereEmbeddings() - self.vector_store = VectorStore() + self.embeddings = embeddings + self.vector_store = vector_store self.brand_voice = self._load_brand_voice() self.sample_campaigns = self._load_sample_campaigns() @@ -91,11 +91,11 @@ class BrandStyleManager: results = self.vector_store.search(prompt_embedding, k=k) # Optionally rerank results - if results: - texts = [result["text"] for result in results] - reranked = self.embeddings.rerank_results(prompt, texts, top_n=k) - # Convert reranked results to the expected format - return [{"text": text} for text in reranked] + # if results: + # texts = [result["text"] for result in results] + # reranked = self.embeddings.rerank_results(prompt, texts, top_n=k) + # # Convert reranked results to the expected format + # return [{"text": text} for text in reranked] # If no results, return empty list return [] @@ -139,16 +139,3 @@ class BrandStyleManager: else: print(f"No content extracted from {pdf_path}") -# # Example usage -# if __name__ == "__main__": -# brand_style_manager = BrandStyleManager() - -# # Example: Get relevant context for a marketing prompt -# prompt = "Generate a marketing campaign for an Umbrella company" -# context = brand_style_manager.get_relevant_context(prompt) - -# # Print the context in a readable format -# print(f"Relevant context for prompt: '{prompt}'") -# for i, item in enumerate(context): -# print(f"\nReference {i+1}:") -# print(item["text"]) diff --git a/backend/copywriter.py b/backend/copywriter.py index fa03874..6bf59fa 100644 --- a/backend/copywriter.py +++ b/backend/copywriter.py @@ -4,16 +4,14 @@ import json from config import settings from brand_style import BrandStyleManager -# Initialize brand style manager -brand_style_manager = BrandStyleManager() - class MarketingCopywriter: - def __init__(self): + def __init__(self, brand_style_manager: BrandStyleManager): self.settings = settings self.api_key = self.settings.DEEPSEEK_API_KEY self.api_url = "https://api.deepseek.com/v1/chat/completions" + self.brand_style_manager = brand_style_manager - def _build_prompt(self, prompt: str, context: List[Dict], content_type: str, tone: str, + def _build_prompt(self, prompt: str, context: List[Dict], tone: str, brand_voice: Dict[str, Any], sample_campaigns: List[Dict[str, Any]]) -> str: """Build a prompt for the LLM using context and parameters.""" # Format context from book excerpts @@ -31,7 +29,7 @@ class MarketingCopywriter: sample_campaigns_text += f"Content:\n{campaign.get('content', '')}\n" return f"""You are a professional marketing copywriter for {self.settings.BRAND_VOICE}. - Your task is to create {content_type} content that matches the following request: {prompt} + Your task is to create content that matches the following request: {prompt} BRAND VOICE GUIDELINES: {brand_voice_text} @@ -81,10 +79,10 @@ class MarketingCopywriter: def generate_marketing_copy(prompt: str) -> str: """Helper function to generate marketing copy.""" - copywriter = MarketingCopywriter() + brand_style_manager = BrandStyleManager() + copywriter = MarketingCopywriter(brand_style_manager) context = brand_style_manager.get_relevant_context(prompt) - content_type = "email" tone = "professional and empathetic" brand_voice = brand_style_manager.get_brand_voice() sample_campaigns = brand_style_manager.get_sample_campaigns() - return copywriter.generate_copy(prompt, context, content_type, tone, brand_voice, sample_campaigns) \ No newline at end of file + return copywriter.generate_copy(prompt, context, tone, brand_voice, sample_campaigns) \ No newline at end of file diff --git a/backend/main.py b/backend/main.py index 0a44cc7..e935bda 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,22 +1,90 @@ -from flask import Flask, request, jsonify, render_template +from flask import Flask, request, jsonify, render_template, redirect, url_for from pydantic import BaseModel from typing import Optional, List, Dict, Any from copywriter import generate_marketing_copy from brand_style import BrandStyleManager from config import settings +import os +import json +import datetime app = Flask(__name__) # Initialize brand style manager +brand_style_manager = BrandStyleManager() +data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "data")) +campaign_prompt = [] + +def load_campaigns(): + campaigns_file = os.path.join(data_dir, "past_campaigns", "campaigns.json") + if os.path.exists(campaigns_file): + with open(campaigns_file, 'r', encoding='utf-8') as f: + data = json.load(f) + campaigns = data.get("campaigns", []) + return campaigns + return [] + +def save_campaigns(campaigns): + campaigns_file = os.path.join(data_dir, "past_campaigns", "campaigns.json") + os.makedirs(os.path.dirname(campaigns_file), exist_ok=True) + with open(campaigns_file, 'w', encoding='utf-8') as f: + json.dump({"campaigns": campaigns}, f, indent=4) @app.route('/', methods=['GET', 'POST']) def root(): + global prompt if request.method == 'POST': prompt = request.form.get('prompt') + campaign_prompt.pop() + campaign_prompt.append(prompt) marketing_copy = generate_marketing_copy(prompt) return render_template('index.html', generated_copy=marketing_copy) - # generated_copy = generate_marketing_copy("Generate a marketing campaign for our new comers") return render_template('index.html') +@app.route('/campaigns') +def view_campaigns(): + campaigns = load_campaigns() + return render_template('campaigns.html', campaigns=campaigns) + +@app.route('/save-edit', methods=['POST']) +def save_edit(): + edited_copy = request.form.get('editedCopy') + global campaign_prompt + prompt = campaign_prompt[-1] + campaigns = load_campaigns() + new_campaign = { + "prompt": prompt, + "content": edited_copy, + "timestamp": datetime.datetime.now().isoformat() + } + campaigns.append(new_campaign) + save_campaigns(campaigns) + + return render_template('index.html', generated_copy="Campaign saved successfully") + +@app.route('/update-campaign', methods=['POST']) +def update_campaign(): + index = int(request.form.get('index')) + edited_copy = request.form.get('editedCopy') + + campaigns = load_campaigns() + if 0 <= index < len(campaigns): + campaigns[index]['content'] = edited_copy + campaigns[index]['timestamp'] = datetime.datetime.now().isoformat() + save_campaigns(campaigns) + + return redirect(url_for('view_campaigns')) + +@app.route('/delete-campaign', methods=['POST']) +def delete_campaign(): + index = int(request.form.get('index')) + + campaigns = load_campaigns() + if 0 <= index < len(campaigns): + campaigns.pop(index) + save_campaigns(campaigns) + + return redirect(url_for('view_campaigns')) + if __name__ == "__main__": app.run(host='localhost', port=8000, debug=True) \ No newline at end of file diff --git a/backend/templates/campaigns.html b/backend/templates/campaigns.html new file mode 100644 index 0000000..bcbcbe0 --- /dev/null +++ b/backend/templates/campaigns.html @@ -0,0 +1,151 @@ + + +
+ + +{{ campaign.content }}
+No campaigns found.
+ {% endif %} +