Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,162 +1,72 @@
|
|
1 |
-
import
|
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 |
-
Mot-clé principal : {mot_cle_principal}
|
31 |
-
Ton et/ou public cible : {ton_cible}
|
32 |
-
Mots-clés lexicaux à inclure : {mots_cles_lexicaux}"""
|
33 |
-
+p+"""
|
34 |
-
|
35 |
-
INSTRUCTIONS:
|
36 |
-
NOMBRE DE MOTS : 1000 à 1500
|
37 |
-
### Règles de rédaction :
|
38 |
-
N'hésitez pas à prendre votre temps pour réfléchir à votre réponse j'ai besoin d'un long article.
|
39 |
-
1. Structure de l'article avec des titres hiérarchisés.
|
40 |
-
2. Optimisation du mot-clé principal (densité d'environ 1,5%, utilisation dans le titre SEO, méta description, et répartition uniforme).
|
41 |
-
3. Inclusion de sous-titres et de mots-clés LSI pour enrichir le contenu sans bourrage de mots-clés.
|
42 |
-
4. Titres et méta descriptions attractifs avec appels à l'action.
|
43 |
-
5. Ton informatif et engageant, avec une variété de vocabulaire.
|
44 |
-
6. Introduction et conclusion engageantes avec le mot-clé principal.
|
45 |
-
7. Section FAQ avec trois questions/réponses courtes.
|
46 |
-
8. Au moins un lien externe vers une source crédible.
|
47 |
-
N'hésitez pas à prendre votre temps pour réfléchir à votre réponse j'ai besoin d'un long article.
|
48 |
-
Assurez que l'article soit lisible, bien structuré et visuellement attrayant.
|
49 |
"""
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
p=""""""
|
57 |
-
prompt = f"""
|
58 |
-
CONTEXT:
|
59 |
-
Please write an article of 1500 - 2000 words. Feel free to take your time to think about your response, I need a long article.
|
60 |
-
You are an SEO writer tasked with composing an SEO-optimized blog article on the following topic.
|
61 |
-
Here are the information and guidelines to follow:
|
62 |
-
|
63 |
-
Article title: {article_title}
|
64 |
-
Main keyword: {main_keyword}
|
65 |
-
Target tone and/or audience: {target_tone}
|
66 |
-
Lexical keywords to include: {lexical_keywords}"""+p+"""
|
67 |
-
|
68 |
-
INSTRUCTIONS:
|
69 |
-
WORD COUNT: 1000 to 1500
|
70 |
-
### Writing rules:
|
71 |
-
Feel free to take your time to think about your response, I need a long article.
|
72 |
-
1. Structure the article with hierarchical headings.
|
73 |
-
2. Optimize the main keyword (density of about 1.5%, use in the SEO title, meta description, and evenly distributed).
|
74 |
-
3. Include subheadings and LSI keywords to enrich the content without keyword stuffing.
|
75 |
-
4. Attractive titles and meta descriptions with calls to action.
|
76 |
-
5. Informative and engaging tone, with a variety of vocabulary.
|
77 |
-
6. Engaging introduction and conclusion with the main keyword.
|
78 |
-
7. FAQ section with three short questions/answers.
|
79 |
-
8. At least one external link to a credible source.
|
80 |
-
Feel free to take your time to think about your response, I need a long article.
|
81 |
-
Ensure that the article is readable, well-structured, and visually appealing.
|
82 |
"""
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
rep5 = call_ai_api(prompt5)
|
112 |
-
|
113 |
-
# Sixth AI API call to ensure the text length
|
114 |
-
prompt6 = ("Trop court, seulement j'aurai besoin de 1500 mots minimum attendu. "
|
115 |
-
"Laisse la même structure de texte, ajoute juste du contenu. " + rep5)
|
116 |
-
rep6 = call_ai_api(prompt6)
|
117 |
-
|
118 |
-
return rep6
|
119 |
-
|
120 |
-
def create_pipeline(article_title, main_keyword, target_tone,optional_text):
|
121 |
-
# Initial AI API call
|
122 |
-
lexical_field_keywords=generate_words(article_title, main_keyword)
|
123 |
-
pp = generate_seo_prompt(article_title, main_keyword, target_tone, lexical_field_keywords,optional_text)
|
124 |
-
|
125 |
-
rep = call_ai_api(pp)
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
"The text is still way too short. Also, there is no title management, the first title is an H2, "
|
134 |
-
"and the subtitles are marked as paragraphs. Regarding the link at the end, it should be integrated into the text. "
|
135 |
-
"Lastly, the meta description should not appear when requesting an article.")
|
136 |
-
rep3 = call_ai_api(prompt3)
|
137 |
-
|
138 |
-
# Fourth AI API call to fix title issues and meta description
|
139 |
-
prompt4 = ("Fix this: Poor title management, the first title must be an H1. The main keyword is not used enough, it should have a density of 1.5%. "
|
140 |
-
"The title should only have a capital letter on the first word of the sentence, not on the others. "
|
141 |
-
"The meta description should not be written. " + rep3)
|
142 |
-
rep4 = call_ai_api(prompt4)
|
143 |
-
|
144 |
-
# Fifth AI API call to fix keyword density and internal link
|
145 |
-
prompt5 = (f"Fix this - The main keyword: {main_keyword} is not used enough, it should have a density of about 1.5%. "
|
146 |
-
"Add a link inside the text, and place the FAQ after the conclusion. " + rep4)
|
147 |
-
rep5 = call_ai_api(prompt5)
|
148 |
-
|
149 |
-
# Sixth AI API call to ensure the text length
|
150 |
-
prompt6 = ("Too short, I need a minimum of 1500 words expected. "
|
151 |
-
"Keep the same text structure, just add more content. " + rep5)
|
152 |
-
rep6 = call_ai_api(prompt6)
|
153 |
-
|
154 |
-
return rep6
|
155 |
-
|
156 |
-
def generer_mots(titre_article,mot_cle_principal):
|
157 |
-
words=call_ai_api("Generes moi des mots-clés similaires a partir de ca ( SEULEMENT DES MOTS OU GROUPE DE MOTS séparés par virgules ) :"+titre_article+" et "+mot_cle_principal)
|
158 |
-
return words
|
159 |
-
|
160 |
-
def generate_words(article_title, main_keyword):
|
161 |
-
words = call_ai_api("Generate similar keywords from these (ONLY WORDS OR GROUPS OF WORDS separated by commas): " + article_title + " and " + main_keyword)
|
162 |
-
return words
|
|
|
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 = None
|
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 = None
|
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 |
+
- optional_text: str - Optional text to include in the article.
|
53 |
+
"""
|
54 |
+
try:
|
55 |
+
# Basic validation of the input
|
56 |
+
if not request.article_title or not request.main_keyword:
|
57 |
+
raise HTTPException(status_code=400, detail="Title and main keyword are required")
|
58 |
+
|
59 |
+
article = create_pipeline(request.article_title, request.main_keyword, request.target_tone,request.optional_text)
|
60 |
+
|
61 |
+
# Ensure the response is not empty
|
62 |
+
if not article:
|
63 |
+
raise HTTPException(status_code=204, detail="Generated article is empty")
|
64 |
+
|
65 |
+
return ArticleResponse(article=article)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
except HTTPException as http_exc:
|
68 |
+
logger.error(f"HTTP Exception: {http_exc.detail}")
|
69 |
+
raise http_exc
|
70 |
+
except Exception as e:
|
71 |
+
logger.error(f"Unhandled Exception: {str(e)}")
|
72 |
+
raise HTTPException(status_code=500, detail="An internal server error occurred")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|