Nechba commited on
Commit
7ca5cdc
·
verified ·
1 Parent(s): c9eca86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -67
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
- # 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")
 
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")