Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,71 @@
|
|
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 |
-
|
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 |
-
except
|
67 |
-
logger.error(f"
|
|
|
|
|
|
|
68 |
raise HTTPException(status_code=500, detail="An internal server error occurred")
|
|
|
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 |
+
optional_text: str
|
17 |
+
# Define the request model
|
18 |
+
class ArticleRequest(BaseModel):
|
19 |
+
titre_article: str
|
20 |
+
mot_cle_principal: str
|
21 |
+
ton_cible: str
|
22 |
+
optional_text : str
|
23 |
+
|
24 |
+
# Define the response model
|
25 |
+
class ArticleResponse(BaseModel):
|
26 |
+
article: str
|
27 |
+
|
28 |
+
@app.post("/generate_article_fr", response_model=ArticleResponse)
|
29 |
+
async def generate_article(request: ArticleRequest):
|
30 |
+
"""
|
31 |
+
Endpoint to generate a French SEO article.
|
32 |
+
Parameters:
|
33 |
+
- titre_article: str - The title of the article.
|
34 |
+
- mot_cle_principal: str - The main keyword for the article.
|
35 |
+
- ton_cible: str - The target tone of the article.
|
36 |
+
- optional_text: str - Optional text to include in the article.
|
37 |
+
"""
|
38 |
+
try:
|
39 |
+
article = create_pipeline_fr(request.titre_article, request.mot_cle_principal, request.ton_cible,request.optional_text)
|
40 |
+
return ArticleResponse(article=article)
|
41 |
+
except Exception as e:
|
42 |
+
raise HTTPException(status_code=500, detail=str(e))
|
43 |
+
|
44 |
+
@app.post("/generate_article_eng", response_model=ArticleResponse)
|
45 |
+
async def generate_article_eng(request: ArticleRequesteng):
|
46 |
+
"""
|
47 |
+
Endpoint to generate an SEO article.
|
48 |
+
Parameters:
|
49 |
+
- article_title: str - The title of the article.
|
50 |
+
- main_keyword: str - The main keyword for the article.
|
51 |
+
- target_tone: str - The target tone of the article.
|
52 |
+
"""
|
53 |
+
try:
|
54 |
+
# Basic validation of the input
|
55 |
+
if not request.article_title or not request.main_keyword:
|
56 |
+
raise HTTPException(status_code=400, detail="Title and main keyword are required")
|
57 |
+
|
58 |
+
article = create_pipeline(request.article_title, request.main_keyword, request.target_tone,request.optional_text)
|
59 |
+
|
60 |
+
# Ensure the response is not empty
|
61 |
+
if not article:
|
62 |
+
raise HTTPException(status_code=204, detail="Generated article is empty")
|
63 |
+
|
64 |
+
return ArticleResponse(article=article)
|
65 |
+
|
66 |
+
except HTTPException as http_exc:
|
67 |
+
logger.error(f"HTTP Exception: {http_exc.detail}")
|
68 |
+
raise http_exc
|
69 |
+
except Exception as e:
|
70 |
+
logger.error(f"Unhandled Exception: {str(e)}")
|
71 |
raise HTTPException(status_code=500, detail="An internal server error occurred")
|