from fastapi import FastAPI, HTTPException, Request from fastapi.responses import JSONResponse from pydantic import BaseModel from typing import Optional import logging from utils import * app = FastAPI() logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Define the request model class ArticleRequesteng(BaseModel): article_title: str main_keyword: str target_tone: str optional_text: str = None # Define the request model class ArticleRequest(BaseModel): titre_article: str mot_cle_principal: str ton_cible: str optional_text : str = None # Define the response model class ArticleResponse(BaseModel): article: str @app.post("/generate_article_fr", response_model=ArticleResponse) async def generate_article(request: ArticleRequest): """ Endpoint to generate a French SEO article. Parameters: - titre_article: str - The title of the article. - mot_cle_principal: str - The main keyword for the article. - ton_cible: str - The target tone of the article. - optional_text: str - Optional text to include in the article. """ try: article = create_pipeline_fr(request.titre_article, request.mot_cle_principal, request.ton_cible,request.optional_text) return ArticleResponse(article=article) except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.post("/generate_article_eng", response_model=ArticleResponse) async def generate_article_eng(request: ArticleRequesteng): """ Endpoint to generate an SEO article. Parameters: - article_title: str - The title of the article. - main_keyword: str - The main keyword for the article. - target_tone: str - The target tone of the article. - optional_text: str - Optional text to include in the article. """ try: # Basic validation of the input if not request.article_title or not request.main_keyword: raise HTTPException(status_code=400, detail="Title and main keyword are required") article = create_pipeline(request.article_title, request.main_keyword, request.target_tone,request.optional_text) # Ensure the response is not empty if not article: raise HTTPException(status_code=204, detail="Generated article is empty") return ArticleResponse(article=article) except HTTPException as http_exc: logger.error(f"HTTP Exception: {http_exc.detail}") raise http_exc except Exception as e: logger.error(f"Unhandled Exception: {str(e)}") raise HTTPException(status_code=500, detail="An internal server error occurred")