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.

This commit is contained in:
boladeE
2025-04-18 18:00:55 +01:00
parent e80ba5c0d7
commit a6147419e5
4 changed files with 32 additions and 67 deletions
+11 -49
View File
@@ -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)
app.run(host='localhost', port=8000, debug=True)