Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Request
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import Optional
|
| 5 |
+
import logging
|
| 6 |
+
from utils import *
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
# Define the request model
|
| 12 |
+
class ArticleRequesteng(BaseModel):
|
| 13 |
+
article_title: str
|
| 14 |
+
main_keyword: str
|
| 15 |
+
target_tone: str
|
| 16 |
+
# Define the request model
|
| 17 |
+
class ArticleRequest(BaseModel):
|
| 18 |
+
titre_article: str
|
| 19 |
+
mot_cle_principal: str
|
| 20 |
+
ton_cible: str
|
| 21 |
+
|
| 22 |
+
# Define the response model
|
| 23 |
+
class ArticleResponse(BaseModel):
|
| 24 |
+
article: str
|
| 25 |
+
|
| 26 |
+
@app.post("/generate_article_fr", response_model=ArticleResponse)
|
| 27 |
+
async def generate_article(request: ArticleRequest):
|
| 28 |
+
"""
|
| 29 |
+
Endpoint to generate a French SEO article.
|
| 30 |
+
Parameters:
|
| 31 |
+
- titre_article: str - The title of the article.
|
| 32 |
+
- mot_cle_principal: str - The main keyword for the article.
|
| 33 |
+
- ton_cible: str - The target tone of the article.
|
| 34 |
+
"""
|
| 35 |
+
try:
|
| 36 |
+
article = create_pipeline_fr(request.titre_article, request.mot_cle_principal, request.ton_cible)
|
| 37 |
+
return ArticleResponse(article=article)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 40 |
+
|
| 41 |
+
@app.post("/generate_article_eng", response_model=ArticleResponse)
|
| 42 |
+
async def generate_article_eng(request: ArticleRequesteng):
|
| 43 |
+
"""
|
| 44 |
+
Endpoint to generate an SEO article.
|
| 45 |
+
Parameters:
|
| 46 |
+
- article_title: str - The title of the article.
|
| 47 |
+
- main_keyword: str - The main keyword for the article.
|
| 48 |
+
- target_tone: str - The target tone of the article.
|
| 49 |
+
"""
|
| 50 |
+
try:
|
| 51 |
+
# Basic validation of the input
|
| 52 |
+
if not request.article_title or not request.main_keyword:
|
| 53 |
+
raise HTTPException(status_code=400, detail="Title and main keyword are required")
|
| 54 |
+
|
| 55 |
+
article = create_pipeline(request.article_title, request.main_keyword, request.target_tone)
|
| 56 |
+
|
| 57 |
+
# Ensure the response is not empty
|
| 58 |
+
if not article:
|
| 59 |
+
raise HTTPException(status_code=204, detail="Generated article is empty")
|
| 60 |
+
|
| 61 |
+
return ArticleResponse(article=article)
|
| 62 |
+
|
| 63 |
+
except HTTPException as http_exc:
|
| 64 |
+
logger.error(f"HTTP Exception: {http_exc.detail}")
|
| 65 |
+
raise http_exc
|
| 66 |
+
except Exception as e:
|
| 67 |
+
logger.error(f"Unhandled Exception: {str(e)}")
|
| 68 |
+
raise HTTPException(status_code=500, detail="An internal server error occurred")
|