From a6147419e5e86e8e306fcd4b7699d5ae7dd6f59a Mon Sep 17 00:00:00 2001 From: boladeE Date: Fri, 18 Apr 2025 18:00:55 +0100 Subject: [PATCH] Refactor backend to use Flask instead of FastAPI, updating routing and form handling in main.py. Adjusted index.html form action to match new routing. Added .env to .gitignore. Enhanced formatting in copywriter.py for improved readability and added guidelines for content generation. --- .gitignore | 2 ++ backend/copywriter.py | 35 +++++++++++---------- backend/main.py | 60 +++++++----------------------------- backend/templates/index.html | 2 +- 4 files changed, 32 insertions(+), 67 deletions(-) diff --git a/.gitignore b/.gitignore index a230a78..9fd76f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .venv/ __pycache__/ + +.env \ No newline at end of file diff --git a/backend/copywriter.py b/backend/copywriter.py index 3ae05f3..fa03874 100644 --- a/backend/copywriter.py +++ b/backend/copywriter.py @@ -31,25 +31,28 @@ 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_type} content that matches the following request: {prompt} -BRAND VOICE GUIDELINES: -{brand_voice_text} + BRAND VOICE GUIDELINES: + {brand_voice_text} -SAMPLE CAMPAIGNS: -{sample_campaigns_text} + SAMPLE CAMPAIGNS: + {sample_campaigns_text} -RELEVANT BOOK EXCERPTS: -{context_text} + RELEVANT BOOK EXCERPTS: + {context_text} -Guidelines: -1. Maintain a {tone} tone throughout -2. Follow {self.settings.BRAND_VOICE}'s brand voice guidelines -3. Be persuasive and engaging -4. Include a clear call-to-action -5. Keep the content concise and impactful + Guidelines: + 1. Maintain a {tone} tone throughout + 2. Follow {self.settings.BRAND_VOICE}'s brand voice guidelines + 3. Be persuasive and engaging + 4. Include a clear call-to-action + 5. Keep the content concise and impactful + 6. Ensure there is no special formatting in the output just plain text. + 7. Make no reference to Adriana James. -Generate the marketing copy:""" + Generate the marketing copy: + """ def generate_copy(self, prompt: str, context: List[Dict], content_type: str, tone: str, brand_voice: Dict[str, Any], sample_campaigns: List[Dict[str, Any]]) -> str: @@ -84,6 +87,4 @@ def generate_marketing_copy(prompt: str) -> str: 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) - -print(generate_marketing_copy("Generate a marketing campaign for our new comers")) \ No newline at end of file + return copywriter.generate_copy(prompt, context, content_type, tone, brand_voice, sample_campaigns) \ No newline at end of file diff --git a/backend/main.py b/backend/main.py index bea45b2..0a44cc7 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,60 +1,22 @@ -from fastapi import FastAPI, HTTPException -from fastapi.middleware.cors import CORSMiddleware +from flask import Flask, request, jsonify, render_template from pydantic import BaseModel from typing import Optional, List, Dict, Any -import uvicorn from copywriter import generate_marketing_copy from brand_style import BrandStyleManager from config import settings -from fastapi.templating import Jinja2Templates -from fastapi import Request - -class CopyRequest(BaseModel): - prompt: str - -app = FastAPI(title="Marketing Assistant AI") - -# Add CORS middleware -app.add_middleware( - CORSMiddleware, - allow_origins=["*"], # Allows all origins - allow_credentials=True, - allow_methods=["*"], # Allows all methods - allow_headers=["*"], # Allows all headers -) - -# Initialize templates -templates = Jinja2Templates(directory="backend/templates") +app = Flask(__name__) # Initialize brand style manager -@app.get("/") -def root(request: Request): - return templates.TemplateResponse("index.html", {"request": request}) - - -@app.get("/generate-copy") -def create_marketing_copy(request: Request): - print(f"Received request: {request}") - # print(f"Received prompt: {request.prompt}") - generated_copy = "Something" - # print(f"Generated copy: {generated_copy}") - - return templates.TemplateResponse("index.html", {"request": request, "generated_copy": generated_copy}) - # try: - # # Generate the marketing copy using the simplified function - # generated_copy = generate_marketing_copy(request.prompt) - # print(f"Generated copy: {generated_copy}") - # return { - # "status": "success", - # "data": { - # "generated_copy": generated_copy - # } - # } - # except Exception as e: - # print(f"Error generating copy: {str(e)}") - # raise HTTPException(status_code=500, detail=str(e)) +@app.route('/', methods=['GET', 'POST']) +def root(): + if request.method == 'POST': + prompt = request.form.get('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') if __name__ == "__main__": - uvicorn.run("main:app", host="localhost", port=8000, reload=True) \ No newline at end of file + app.run(host='localhost', port=8000, debug=True) \ No newline at end of file diff --git a/backend/templates/index.html b/backend/templates/index.html index 9bf2c9a..2bc6d28 100644 --- a/backend/templates/index.html +++ b/backend/templates/index.html @@ -69,7 +69,7 @@

Marketing Assistant AI

-
+