Spaces:
Sleeping
Sleeping
update routes
Browse files- app/api/routes/routes.py +100 -106
app/api/routes/routes.py
CHANGED
@@ -1,107 +1,101 @@
|
|
1 |
-
from fastapi import APIRouter, HTTPException
|
2 |
-
from fastapi.responses import FileResponse
|
3 |
-
from app.services.video_service import VideoService
|
4 |
-
from fastapi import APIRouter, HTTPException
|
5 |
-
from app.services.quiz_generator import QuizGenerator
|
6 |
-
from app.models.quiz import QuizRequest
|
7 |
-
from app.core.config import settings
|
8 |
-
import logging
|
9 |
-
import uuid
|
10 |
-
|
11 |
-
# Configurer le logging
|
12 |
-
logging.basicConfig(level=logging.DEBUG)
|
13 |
-
logger = logging.getLogger(__name__)
|
14 |
-
|
15 |
-
# Importons le quiz de test
|
16 |
-
TEST_QUIZ = {
|
17 |
-
"id": "test-quiz",
|
18 |
-
"theme": "Intelligence Artificielle",
|
19 |
-
"questions": [
|
20 |
-
{
|
21 |
-
"question": "Qu'est-ce que le Machine Learning ?",
|
22 |
-
"options": [
|
23 |
-
"Un type de robot",
|
24 |
-
"Une branche de l'IA permettant aux machines d'apprendre",
|
25 |
-
"Un langage de programmation",
|
26 |
-
"Un système d'exploitation"
|
27 |
-
],
|
28 |
-
"correct_answer": "Une branche de l'IA permettant aux machines d'apprendre"
|
29 |
-
},
|
30 |
-
{
|
31 |
-
"question": "Qu'est-ce qu'un réseau de neurones ?",
|
32 |
-
"options": [
|
33 |
-
"Un système inspiré du cerveau humain",
|
34 |
-
"Un réseau social",
|
35 |
-
"Un câble ethernet",
|
36 |
-
"Un type de processeur"
|
37 |
-
],
|
38 |
-
"correct_answer": "Un système inspiré du cerveau humain"
|
39 |
-
},
|
40 |
-
{
|
41 |
-
"question": "Quel est le langage le plus utilisé en IA ?",
|
42 |
-
"options": [
|
43 |
-
"Java",
|
44 |
-
"C++",
|
45 |
-
"Python",
|
46 |
-
"JavaScript"
|
47 |
-
],
|
48 |
-
"correct_answer": "Python"
|
49 |
-
}
|
50 |
-
]
|
51 |
-
}
|
52 |
-
|
53 |
-
|
54 |
-
router = APIRouter()
|
55 |
-
router = APIRouter()
|
56 |
-
quiz_generator = QuizGenerator(provider=settings.AI_PROVIDER)
|
57 |
-
|
58 |
-
@router.post("/quiz")
|
59 |
-
async def create_quiz(request: QuizRequest):
|
60 |
-
try:
|
61 |
-
questions = await quiz_generator.generate_quiz(
|
62 |
-
theme=request.theme,
|
63 |
-
num_questions=request.num_questions
|
64 |
-
)
|
65 |
-
# Créer un ID unique pour le quiz
|
66 |
-
quiz_id = f"quiz_{uuid.uuid4().hex[:8]}"
|
67 |
-
|
68 |
-
quiz_data = {
|
69 |
-
"id": quiz_id,
|
70 |
-
"theme": request.theme,
|
71 |
-
"questions": [
|
72 |
-
{
|
73 |
-
"question": q.question,
|
74 |
-
"options": q.options,
|
75 |
-
"correct_answer": q.correct_answer
|
76 |
-
} for q in questions
|
77 |
-
]
|
78 |
-
}
|
79 |
-
|
80 |
-
return quiz_data
|
81 |
-
except Exception as e:
|
82 |
-
logger.error(f"Erreur dans create_quiz: {str(e)}")
|
83 |
-
raise HTTPException(status_code=500, detail=str(e))
|
84 |
-
|
85 |
-
|
86 |
-
@router.post("/quiz/{quiz_id}/video")
|
87 |
-
async def generate_video(quiz_id: str, quiz_data: dict):
|
88 |
-
try:
|
89 |
-
logger.info(f"Début de la génération de vidéo pour le quiz {quiz_id}")
|
90 |
-
logger.debug(f"Données du quiz reçues: {quiz_data}")
|
91 |
-
|
92 |
-
# Vérifier que les données nécessaires sont présentes
|
93 |
-
if not quiz_data.get("questions") or quiz_data.get("styleConfig") is None:
|
94 |
-
raise HTTPException(status_code=400, detail="Données du quiz ou style manquants")
|
95 |
-
|
96 |
-
#
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
path=video_path,
|
102 |
-
media_type="video/mp4",
|
103 |
-
filename=f"quiz_{quiz_id}.mp4"
|
104 |
-
)
|
105 |
-
except Exception as e:
|
106 |
-
logger.error(f"Erreur lors de la génération de la vidéo: {str(e)}", exc_info=True)
|
107 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
1 |
+
from fastapi import APIRouter, HTTPException
|
2 |
+
from fastapi.responses import FileResponse
|
3 |
+
from app.services.video_service import VideoService
|
4 |
+
from fastapi import APIRouter, HTTPException
|
5 |
+
from app.services.quiz_generator import QuizGenerator
|
6 |
+
from app.models.quiz import QuizRequest
|
7 |
+
from app.core.config import settings
|
8 |
+
import logging
|
9 |
+
import uuid
|
10 |
+
|
11 |
+
# Configurer le logging
|
12 |
+
logging.basicConfig(level=logging.DEBUG)
|
13 |
+
logger = logging.getLogger(__name__)
|
14 |
+
|
15 |
+
# Importons le quiz de test
|
16 |
+
TEST_QUIZ = {
|
17 |
+
"id": "test-quiz",
|
18 |
+
"theme": "Intelligence Artificielle",
|
19 |
+
"questions": [
|
20 |
+
{
|
21 |
+
"question": "Qu'est-ce que le Machine Learning ?",
|
22 |
+
"options": [
|
23 |
+
"Un type de robot",
|
24 |
+
"Une branche de l'IA permettant aux machines d'apprendre",
|
25 |
+
"Un langage de programmation",
|
26 |
+
"Un système d'exploitation"
|
27 |
+
],
|
28 |
+
"correct_answer": "Une branche de l'IA permettant aux machines d'apprendre"
|
29 |
+
},
|
30 |
+
{
|
31 |
+
"question": "Qu'est-ce qu'un réseau de neurones ?",
|
32 |
+
"options": [
|
33 |
+
"Un système inspiré du cerveau humain",
|
34 |
+
"Un réseau social",
|
35 |
+
"Un câble ethernet",
|
36 |
+
"Un type de processeur"
|
37 |
+
],
|
38 |
+
"correct_answer": "Un système inspiré du cerveau humain"
|
39 |
+
},
|
40 |
+
{
|
41 |
+
"question": "Quel est le langage le plus utilisé en IA ?",
|
42 |
+
"options": [
|
43 |
+
"Java",
|
44 |
+
"C++",
|
45 |
+
"Python",
|
46 |
+
"JavaScript"
|
47 |
+
],
|
48 |
+
"correct_answer": "Python"
|
49 |
+
}
|
50 |
+
]
|
51 |
+
}
|
52 |
+
|
53 |
+
|
54 |
+
router = APIRouter()
|
55 |
+
router = APIRouter()
|
56 |
+
quiz_generator = QuizGenerator(provider=settings.AI_PROVIDER)
|
57 |
+
|
58 |
+
@router.post("/quiz")
|
59 |
+
async def create_quiz(request: QuizRequest):
|
60 |
+
try:
|
61 |
+
questions = await quiz_generator.generate_quiz(
|
62 |
+
theme=request.theme,
|
63 |
+
num_questions=request.num_questions
|
64 |
+
)
|
65 |
+
# Créer un ID unique pour le quiz
|
66 |
+
quiz_id = f"quiz_{uuid.uuid4().hex[:8]}"
|
67 |
+
|
68 |
+
quiz_data = {
|
69 |
+
"id": quiz_id,
|
70 |
+
"theme": request.theme,
|
71 |
+
"questions": [
|
72 |
+
{
|
73 |
+
"question": q.question,
|
74 |
+
"options": q.options,
|
75 |
+
"correct_answer": q.correct_answer
|
76 |
+
} for q in questions
|
77 |
+
]
|
78 |
+
}
|
79 |
+
|
80 |
+
return quiz_data
|
81 |
+
except Exception as e:
|
82 |
+
logger.error(f"Erreur dans create_quiz: {str(e)}")
|
83 |
+
raise HTTPException(status_code=500, detail=str(e))
|
84 |
+
|
85 |
+
|
86 |
+
@router.post("/quiz/{quiz_id}/video")
|
87 |
+
async def generate_video(quiz_id: str, quiz_data: dict):
|
88 |
+
try:
|
89 |
+
logger.info(f"Début de la génération de vidéo pour le quiz {quiz_id}")
|
90 |
+
logger.debug(f"Données du quiz reçues: {quiz_data}")
|
91 |
+
|
92 |
+
# Vérifier que les données nécessaires sont présentes
|
93 |
+
if not quiz_data.get("questions") or quiz_data.get("styleConfig") is None:
|
94 |
+
raise HTTPException(status_code=400, detail="Données du quiz ou style manquants")
|
95 |
+
|
96 |
+
# Générer et streamer la vidéo
|
97 |
+
return await VideoService.generate_quiz_video(quiz_data)
|
98 |
+
|
99 |
+
except Exception as e:
|
100 |
+
logger.error(f"Erreur lors de la génération de la vidéo: {str(e)}", exc_info=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
raise HTTPException(status_code=500, detail=str(e))
|