Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,15 +1,17 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
-
import json
|
| 6 |
-
import uvicorn # <-- Ajouté
|
| 7 |
|
|
|
|
| 8 |
token = os.getenv("HF_TOKEN")
|
| 9 |
client = InferenceClient(api_key=token)
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
|
|
|
| 13 |
app.add_middleware(
|
| 14 |
CORSMiddleware,
|
| 15 |
allow_origins=["*"],
|
|
@@ -17,40 +19,57 @@ app.add_middleware(
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
@app.post("/verify")
|
| 21 |
async def verify(data: dict):
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
|
|
|
| 25 |
prompt = f"""<s>[INST] <<SYS>>
|
| 26 |
-
Tu es un sismologue expert.
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
VILLE : {sujet_cours}
|
| 29 |
-
MESSAGE : {question_eleve}
|
| 30 |
|
| 31 |
-
Réponds
|
| 32 |
{{
|
| 33 |
"isCorrect": true ou false,
|
| 34 |
-
"feedback": "Ton analyse
|
| 35 |
}}
|
| 36 |
<</SYS>> [/INST]"""
|
| 37 |
|
| 38 |
try:
|
|
|
|
| 39 |
response = client.text_generation(
|
| 40 |
prompt,
|
| 41 |
model="mistralai/Mistral-7B-Instruct-v0.3",
|
| 42 |
-
max_new_tokens=
|
| 43 |
-
temperature=0.3
|
| 44 |
)
|
|
|
|
|
|
|
| 45 |
start = response.find('{')
|
| 46 |
end = response.rfind('}') + 1
|
| 47 |
return json.loads(response[start:end])
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
# Lancement du serveur
|
| 52 |
if __name__ == "__main__":
|
| 53 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 54 |
-
except Exception as e:
|
| 55 |
-
# On affiche l'erreur réelle pour comprendre le problème
|
| 56 |
-
return {"isCorrect": False, "feedback": f"Erreur technique : {str(e)}"}
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
+
import uvicorn
|
| 4 |
from fastapi import FastAPI
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# 1. Configuration du client avec votre Token (Stocké dans les Secrets du Space)
|
| 9 |
token = os.getenv("HF_TOKEN")
|
| 10 |
client = InferenceClient(api_key=token)
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
+
# 2. Configuration CORS pour autoriser votre application web à appeler ce Space
|
| 15 |
app.add_middleware(
|
| 16 |
CORSMiddleware,
|
| 17 |
allow_origins=["*"],
|
|
|
|
| 19 |
allow_headers=["*"],
|
| 20 |
)
|
| 21 |
|
| 22 |
+
@app.get("/")
|
| 23 |
+
def read_root():
|
| 24 |
+
return {"status": "Le proxy sismologue est en ligne"}
|
| 25 |
+
|
| 26 |
@app.post("/verify")
|
| 27 |
async def verify(data: dict):
|
| 28 |
+
# Récupération des données envoyées par l'élève
|
| 29 |
+
question_eleve = data.get("userLatex", "")
|
| 30 |
+
sujet_cours = data.get("expectedLatex", "Pont-Saint-Esprit")
|
| 31 |
|
| 32 |
+
# Construction du prompt pour l'IA
|
| 33 |
prompt = f"""<s>[INST] <<SYS>>
|
| 34 |
+
Tu es un sismologue expert de l'IRSN. Ton rôle est d'analyser le rapport d'un élève de 4ème.
|
| 35 |
+
|
| 36 |
+
RÈGLES :
|
| 37 |
+
1. Analyse si l'élève mentionne l'ALÉA (faille), l'ENJEU (habitants) et le RISQUE.
|
| 38 |
+
2. Sois bref (moins de 80 mots).
|
| 39 |
+
3. Utilise un ton encourageant.
|
| 40 |
+
4. Si l'élève se trompe, donne un indice sans donner la réponse.
|
| 41 |
+
|
| 42 |
VILLE : {sujet_cours}
|
| 43 |
+
MESSAGE DE L'ÉLÈVE : {question_eleve}
|
| 44 |
|
| 45 |
+
Réponds STRICTEMENT au format JSON suivant :
|
| 46 |
{{
|
| 47 |
"isCorrect": true ou false,
|
| 48 |
+
"feedback": "Ton analyse ici"
|
| 49 |
}}
|
| 50 |
<</SYS>> [/INST]"""
|
| 51 |
|
| 52 |
try:
|
| 53 |
+
# Appel au modèle de langage (Mistral)
|
| 54 |
response = client.text_generation(
|
| 55 |
prompt,
|
| 56 |
model="mistralai/Mistral-7B-Instruct-v0.3",
|
| 57 |
+
max_new_tokens=200,
|
| 58 |
+
temperature=0.3
|
| 59 |
)
|
| 60 |
+
|
| 61 |
+
# Extraction du JSON dans la réponse de l'IA
|
| 62 |
start = response.find('{')
|
| 63 |
end = response.rfind('}') + 1
|
| 64 |
return json.loads(response[start:end])
|
| 65 |
+
|
| 66 |
except Exception as e:
|
| 67 |
+
# Renvoie l'erreur réelle pour le débogage
|
| 68 |
+
return {
|
| 69 |
+
"isCorrect": False,
|
| 70 |
+
"feedback": f"Erreur technique (vérifiez votre HF_TOKEN) : {str(e)}"
|
| 71 |
+
}
|
| 72 |
|
| 73 |
+
# 3. Lancement du serveur sur le port 7860 (obligatoire sur HF Spaces)
|
| 74 |
if __name__ == "__main__":
|
| 75 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|