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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template, jsonify
|
| 2 |
+
import PIL.Image
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
import os
|
| 5 |
+
from tempfile import NamedTemporaryFile
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Configuration de Gemini
|
| 10 |
+
generation_config = {
|
| 11 |
+
"temperature": 1,
|
| 12 |
+
"max_output_tokens": 8192,
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
safety_settings = [
|
| 16 |
+
{
|
| 17 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
| 18 |
+
"threshold": "BLOCK_NONE"
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
| 22 |
+
"threshold": "BLOCK_NONE"
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 26 |
+
"threshold": "BLOCK_NONE"
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 30 |
+
"threshold": "BLOCK_NONE"
|
| 31 |
+
},
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
GOOGLE_API_KEY = os.environ.get("TOKEN")
|
| 35 |
+
|
| 36 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 37 |
+
|
| 38 |
+
@app.route('/')
|
| 39 |
+
def index():
|
| 40 |
+
return render_template('histoire.html')
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def rediger_section_histoire(theme, point, contexte_precedent=""):
|
| 44 |
+
"""Génère une section de dissertation d'histoire."""
|
| 45 |
+
prompt = f"""
|
| 46 |
+
Rédige une section détaillée pour une dissertation d'histoire sur le thème "{theme}".
|
| 47 |
+
|
| 48 |
+
Point à traiter: {point}
|
| 49 |
+
|
| 50 |
+
Structure à suivre:
|
| 51 |
+
1. Phrase chapeau introduisant l'idée principale
|
| 52 |
+
2. Trois arguments développés:
|
| 53 |
+
- Argument + Explication détaillée + Exemple historique précis
|
| 54 |
+
- Utiliser des dates et des personnages historiques spécifiques
|
| 55 |
+
3. Phrase de transition vers le point suivant
|
| 56 |
+
|
| 57 |
+
Format:
|
| 58 |
+
- Utilise "## " pour le titre principal
|
| 59 |
+
- Utilise "### " pour les sous-parties
|
| 60 |
+
- Mets en **gras** les dates et personnages importants
|
| 61 |
+
- Utilise des listes numérotées pour les arguments
|
| 62 |
+
- Ajoute des citations historiques pertinentes avec ">"
|
| 63 |
+
|
| 64 |
+
Contexte précédent de la dissertation: {contexte_precedent}
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 68 |
+
chat = model.start_chat()
|
| 69 |
+
response = chat.send_message(prompt)
|
| 70 |
+
return response.text, chat.history
|
| 71 |
+
|
| 72 |
+
def rediger_section_geographie(theme, point, contexte_precedent=""):
|
| 73 |
+
"""Génère une section de dissertation de géographie."""
|
| 74 |
+
prompt = f"""
|
| 75 |
+
Rédige une section détaillée pour une dissertation de géographie sur le thème "{theme}".
|
| 76 |
+
|
| 77 |
+
Point à traiter: {point}
|
| 78 |
+
|
| 79 |
+
Structure à suivre:
|
| 80 |
+
1. Phrase chapeau présentant l'enjeu géographique
|
| 81 |
+
2. Trois aspects à développer:
|
| 82 |
+
- Analyse spatiale + Exemple territorial concret + Données chiffrées
|
| 83 |
+
- Utiliser des échelles d'analyse variées (locale, régionale, mondiale)
|
| 84 |
+
3. Phrase de transition
|
| 85 |
+
|
| 86 |
+
Format:
|
| 87 |
+
- Utilise "## " pour le titre principal
|
| 88 |
+
- Utilise "### " pour les sous-parties
|
| 89 |
+
- Mets en **gras** les concepts géographiques clés
|
| 90 |
+
- Utilise des listes à puces pour les exemples territoriaux
|
| 91 |
+
- Ajoute des données statistiques importantes dans des blocs "`"
|
| 92 |
+
|
| 93 |
+
Contexte précédent de la dissertation: {contexte_precedent}
|
| 94 |
+
"""
|
| 95 |
+
|
| 96 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 97 |
+
chat = model.start_chat()
|
| 98 |
+
response = chat.send_message(prompt)
|
| 99 |
+
return response.text, chat.history
|
| 100 |
+
|
| 101 |
+
def generer_introduction(sujet, type_sujet="histoire"):
|
| 102 |
+
"""Génère l'introduction de la dissertation."""
|
| 103 |
+
prompt = f"""
|
| 104 |
+
Rédige une introduction structurée pour une dissertation de {type_sujet} sur le sujet:
|
| 105 |
+
"{sujet}"
|
| 106 |
+
|
| 107 |
+
Structure:
|
| 108 |
+
1. Accroche (définition ou contextualisation)
|
| 109 |
+
2. Problématique claire
|
| 110 |
+
3. Annonce du plan
|
| 111 |
+
|
| 112 |
+
Format en Markdown avec paragraphes distincts.
|
| 113 |
+
"""
|
| 114 |
+
|
| 115 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 116 |
+
response = model.generate_content(prompt)
|
| 117 |
+
return response.text
|
| 118 |
+
|
| 119 |
+
def generer_conclusion(sujet, contenu, type_sujet="histoire"):
|
| 120 |
+
"""Génère la conclusion de la dissertation."""
|
| 121 |
+
prompt = f"""
|
| 122 |
+
À partir de cette dissertation de {type_sujet} sur "{sujet}",
|
| 123 |
+
rédige une conclusion qui:
|
| 124 |
+
1. Synthétise les points principaux
|
| 125 |
+
2. Répond à la problématique
|
| 126 |
+
3. Propose une ouverture pertinente
|
| 127 |
+
|
| 128 |
+
Contenu de la dissertation:
|
| 129 |
+
{contenu}
|
| 130 |
+
"""
|
| 131 |
+
|
| 132 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 133 |
+
response = model.generate_content(prompt)
|
| 134 |
+
return response.text
|
| 135 |
+
|
| 136 |
+
@app.route('/api/histoire', methods=['POST'])
|
| 137 |
+
def submit_histoire():
|
| 138 |
+
# Récupération des données
|
| 139 |
+
sujet = request.form.get('sujet-histoire', '').strip()
|
| 140 |
+
points = [
|
| 141 |
+
request.form.get('point1-histoire', '').strip(),
|
| 142 |
+
request.form.get('point2-histoire', '').strip(),
|
| 143 |
+
request.form.get('point3-histoire', '').strip()
|
| 144 |
+
]
|
| 145 |
+
|
| 146 |
+
if not sujet or not all(points):
|
| 147 |
+
return jsonify({"error": "Tous les champs sont obligatoires"}), 400
|
| 148 |
+
|
| 149 |
+
try:
|
| 150 |
+
# Génération de l'introduction
|
| 151 |
+
dissertation = generer_introduction(sujet, "histoire")
|
| 152 |
+
dissertation += "\n\n"
|
| 153 |
+
|
| 154 |
+
# Génération du développement
|
| 155 |
+
contexte = dissertation
|
| 156 |
+
for point in points:
|
| 157 |
+
section_texte, _ = rediger_section_histoire(sujet, point, contexte)
|
| 158 |
+
dissertation += section_texte + "\n\n"
|
| 159 |
+
contexte = dissertation
|
| 160 |
+
|
| 161 |
+
# Génération de la conclusion
|
| 162 |
+
dissertation += generer_conclusion(sujet, dissertation, "histoire")
|
| 163 |
+
|
| 164 |
+
return jsonify({"output": dissertation}), 200
|
| 165 |
+
|
| 166 |
+
except Exception as e:
|
| 167 |
+
return jsonify({"error": str(e)}), 500
|
| 168 |
+
|
| 169 |
+
@app.route('/api/geographie', methods=['POST'])
|
| 170 |
+
def submit_geographie():
|
| 171 |
+
# Récupération des données
|
| 172 |
+
sujet = request.form.get('sujet-geographie', '').strip()
|
| 173 |
+
points = [
|
| 174 |
+
request.form.get('point1-geographie', '').strip(),
|
| 175 |
+
request.form.get('point2-geographie', '').strip(),
|
| 176 |
+
request.form.get('point3-geographie', '').strip()
|
| 177 |
+
]
|
| 178 |
+
|
| 179 |
+
if not sujet or not all(points):
|
| 180 |
+
return jsonify({"error": "Tous les champs sont obligatoires"}), 400
|
| 181 |
+
|
| 182 |
+
try:
|
| 183 |
+
# Génération de l'introduction
|
| 184 |
+
dissertation = generer_introduction(sujet, "géographie")
|
| 185 |
+
dissertation += "\n\n"
|
| 186 |
+
|
| 187 |
+
# Génération du développement
|
| 188 |
+
contexte = dissertation
|
| 189 |
+
for point in points:
|
| 190 |
+
section_texte, _ = rediger_section_geographie(sujet, point, contexte)
|
| 191 |
+
dissertation += section_texte + "\n\n"
|
| 192 |
+
contexte = dissertation
|
| 193 |
+
|
| 194 |
+
# Génération de la conclusion
|
| 195 |
+
dissertation += generer_conclusion(sujet, dissertation, "géographie")
|
| 196 |
+
|
| 197 |
+
return jsonify({"output": dissertation}), 200
|
| 198 |
+
|
| 199 |
+
except Exception as e:
|
| 200 |
+
return jsonify({"error": str(e)}), 500
|