Spaces:
Running
Running
File size: 2,498 Bytes
72addc3 |
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 68 |
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
# Define the request model
class ArticleRequest(BaseModel):
titre_article: str
mot_cle_principal: str
ton_cible: str
# 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.
"""
try:
article = create_pipeline_fr(request.titre_article, request.mot_cle_principal, request.ton_cible)
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.
"""
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)
# 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") |