Update app.py
Browse files
app.py
CHANGED
|
@@ -4,44 +4,41 @@ import requests
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
-
# Autorise tout le monde à interroger ce serveur (nécessaire pour votre HTML externe)
|
| 8 |
CORS(app)
|
| 9 |
|
| 10 |
-
# Récupération de la clé
|
| 11 |
API_KEY = os.environ.get("ALBERT_KEY")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
ALBERT_URL = "https://albert.api.etalab.gouv.fr/v1/chat/completions"
|
| 15 |
|
| 16 |
@app.route('/')
|
| 17 |
def home():
|
| 18 |
-
return "
|
| 19 |
|
| 20 |
@app.route('/verify', methods=['POST'])
|
| 21 |
def verify_math():
|
| 22 |
-
# 1.
|
| 23 |
data = request.json
|
| 24 |
user_latex = data.get('userLatex', '')
|
| 25 |
expected_latex = data.get('expectedLatex', '')
|
| 26 |
|
|
|
|
| 27 |
if not API_KEY:
|
| 28 |
-
return jsonify({"error": "Clé API
|
| 29 |
|
| 30 |
-
# 2.
|
| 31 |
-
# On demande un JSON strict pour faciliter la lecture
|
| 32 |
system_prompt = (
|
| 33 |
-
"Tu es un professeur de mathématiques
|
| 34 |
-
"
|
| 35 |
-
"
|
| 36 |
-
"Réponds
|
| 37 |
-
"{\"isCorrect\": boolean, \"feedback\": \"texte court en français\"}."
|
| 38 |
)
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
payload = {
|
| 43 |
-
# C'est ici qu'on met l'ID officiel du modèle Albert
|
| 44 |
-
"model": "openai/gpt-oss-120b",
|
| 45 |
"temperature": 0,
|
| 46 |
"messages": [
|
| 47 |
{"role": "system", "content": system_prompt},
|
|
@@ -54,21 +51,17 @@ payload = {
|
|
| 54 |
"Content-Type": "application/json"
|
| 55 |
}
|
| 56 |
|
| 57 |
-
# 3.
|
| 58 |
try:
|
| 59 |
response = requests.post(ALBERT_URL, json=payload, headers=headers)
|
| 60 |
-
response.raise_for_status()
|
| 61 |
|
| 62 |
-
# 4. Renvoyer la réponse d'Albert à l'élève
|
| 63 |
albert_data = response.json()
|
| 64 |
-
|
| 65 |
-
# On extrait le contenu du message (souvent dans choices[0].message.content)
|
| 66 |
content = albert_data['choices'][0]['message']['content']
|
| 67 |
|
| 68 |
return jsonify({"raw_content": content})
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
-
print(f"Erreur: {e}")
|
| 72 |
return jsonify({"error": str(e)}), 500
|
| 73 |
|
| 74 |
if __name__ == '__main__':
|
|
|
|
| 4 |
import os
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
|
|
|
| 7 |
CORS(app)
|
| 8 |
|
| 9 |
+
# Récupération de la clé depuis les secrets Hugging Face
|
| 10 |
API_KEY = os.environ.get("ALBERT_KEY")
|
| 11 |
|
| 12 |
+
# URL API Albert
|
| 13 |
ALBERT_URL = "https://albert.api.etalab.gouv.fr/v1/chat/completions"
|
| 14 |
|
| 15 |
@app.route('/')
|
| 16 |
def home():
|
| 17 |
+
return "Serveur Albert Proxy en ligne !"
|
| 18 |
|
| 19 |
@app.route('/verify', methods=['POST'])
|
| 20 |
def verify_math():
|
| 21 |
+
# 1. Récupération des données
|
| 22 |
data = request.json
|
| 23 |
user_latex = data.get('userLatex', '')
|
| 24 |
expected_latex = data.get('expectedLatex', '')
|
| 25 |
|
| 26 |
+
# Vérification présence clé
|
| 27 |
if not API_KEY:
|
| 28 |
+
return jsonify({"error": "Clé API ALBERT_KEY manquante dans les Settings"}), 500
|
| 29 |
|
| 30 |
+
# 2. Préparation du prompt
|
|
|
|
| 31 |
system_prompt = (
|
| 32 |
+
"Tu es un professeur de mathématiques. "
|
| 33 |
+
"Vérifie si la réponse de l'élève est mathématiquement équivalente à l'attendu "
|
| 34 |
+
"ET si elle est sous forme développée et réduite. "
|
| 35 |
+
"Réponds uniquement en JSON : {\"isCorrect\": boolean, \"feedback\": \"string\"}."
|
|
|
|
| 36 |
)
|
| 37 |
+
|
| 38 |
+
user_prompt = f"Attendu: {expected_latex}. Élève: {user_latex}."
|
| 39 |
|
| 40 |
+
payload = {
|
| 41 |
+
"model": "openai/gpt-oss-120b",
|
|
|
|
|
|
|
|
|
|
| 42 |
"temperature": 0,
|
| 43 |
"messages": [
|
| 44 |
{"role": "system", "content": system_prompt},
|
|
|
|
| 51 |
"Content-Type": "application/json"
|
| 52 |
}
|
| 53 |
|
| 54 |
+
# 3. Appel API
|
| 55 |
try:
|
| 56 |
response = requests.post(ALBERT_URL, json=payload, headers=headers)
|
| 57 |
+
response.raise_for_status()
|
| 58 |
|
|
|
|
| 59 |
albert_data = response.json()
|
|
|
|
|
|
|
| 60 |
content = albert_data['choices'][0]['message']['content']
|
| 61 |
|
| 62 |
return jsonify({"raw_content": content})
|
| 63 |
|
| 64 |
except Exception as e:
|
|
|
|
| 65 |
return jsonify({"error": str(e)}), 500
|
| 66 |
|
| 67 |
if __name__ == '__main__':
|