File size: 16,172 Bytes
a738d20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from fastapi import FastAPI, HTTPException, Depends, UploadFile, File, Form
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from typing import List, Optional, Dict, Any
import pandas as pd
import numpy as np
import pandas as pd
import sys
import os
import io
from pathlib import Path

sys.path.append(str(Path(__file__).parent.parent))

from src.supabase_client import get_supabase_client
from src.gemini_solver import GeminiMCQSolver
from src.mock_solver import MockMCQSolver
from src.profile_generator import build_probability_matrix
from src.crowd_generator import CrowdGenerator
from src.rasch_model import RaschModel
from src.comparator import PsychometricComparator
from config import GEMINI_API_KEY, DEEPSEEK_API_KEY, DEEPSEEK_MODEL

app = FastAPI(title="PsychoCrowd API - V3")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

os.makedirs("data", exist_ok=True)
os.makedirs("outputs/plots", exist_ok=True)
app.mount("/outputs", StaticFiles(directory="outputs"), name="outputs")
app.mount("/data", StaticFiles(directory="data"), name="data")

def get_db():
    return get_supabase_client()

# --- Schemas ---

class MCQItem(BaseModel):
    item_id: Optional[int] = None
    question: str
    option_a: str
    option_b: str
    option_c: str
    option_d: str
    correct_option: str
    difficulty_expert: str

class CalibrateRequest(BaseModel):
    items: List[dict]
    use_gemini: bool = True

class SimulateRequest(BaseModel):
    items: List[dict] # Calibrated items (with ai_confidence, distractor_weights etc)
    n_per_profile: int = 50

class RaschRequest(BaseModel):
    response_matrix: List[List[int]] # N_students x N_items
    items: List[dict]

# --- Endpoints ---

@app.get("/")
def read_root():
    return {"message": "PsychoCrowd V3 API Gateway"}

@app.get("/items")
def get_items(db=Depends(get_db)):
    """Fetch MCQ bank from Supabase."""
    response = db.table("mcq_bank").select("*").execute()
    return {"data": response.data}

@app.post("/items")
def create_item(item: MCQItem, db=Depends(get_db)):
    """Add an item to the bank."""
    data = item.dict(exclude_unset=True)
    response = db.table("mcq_bank").insert(data).execute()
    return {"status": "success", "data": response.data}

@app.post("/calibrate")
def calibrate_items(req: CalibrateRequest):
    """Calibrate items using Gemini or Mock Solver."""
    df = pd.DataFrame(req.items)
    
    if req.use_gemini and GEMINI_API_KEY:
        solver = GeminiMCQSolver()
    else:
        solver = MockMCQSolver()
        
    calibrated_df = solver.solve_all(df)
    return {"data": calibrated_df.to_dict(orient="records")}

@app.post("/simulate")
def simulate_crowd(req: SimulateRequest):
    """Generate artificial crowd responses based on calibrated items."""
    mcq_df = pd.DataFrame(req.items)
    prob_matrix = build_probability_matrix(mcq_df)
    
    crowd_gen = CrowdGenerator(mcq_df, prob_matrix, n_per_profile=req.n_per_profile)
    crowd_df = crowd_gen.generate_crowd()
    response_matrix = crowd_gen.get_response_matrix()
    accuracy_df = crowd_gen.get_accuracy_by_profile()
    
    return {
        "crowd_responses": crowd_df.to_dict(orient="records"),
        "response_matrix": response_matrix.tolist(),
        "accuracy_by_profile": accuracy_df.to_dict(orient="records")
    }

@app.post("/rasch")
def fit_rasch(req: RaschRequest):
    """Fit JMLE Rasch model on a response matrix."""
    matrix = np.array(req.response_matrix)
    rasch = RaschModel()
    rasch.fit(matrix)
    
    if not rasch.converged:
        return {"status": "warning", "message": "Model did not converge completely.", "summary": rasch.summary()}
        
    fit_stats = rasch.item_fit_stats(matrix)
    
    return {
        "status": "success",
        "summary": rasch.summary(),
        "difficulties": rasch.get_item_difficulties().tolist(),
        "abilities": rasch.theta.tolist(),
        "item_fit": fit_stats.to_dict(orient="records")
    }

@app.post("/analyze")
def analyze_results(req: Dict[str, Any]):
    """NLP analysis of the Rasch comparison results using Claude."""
    return {"status": "success", "analysis": "This is a placeholder for Claude's NLP analysis of the psychometric results."}

@app.post("/api/upload")
async def upload_file(file_type: str = Form(...), file: UploadFile = File(...)):
    contents = await file.read()
    filename = "mcq_bank.csv" if file_type == "mcq" else "human_responses.csv"
    with open(f"data/{filename}", "wb") as f:
        f.write(contents)
    return {"status": "success"}

def read_csv_robust(contents: bytes) -> pd.DataFrame:
    for enc in ["utf-8", "windows-1252", "latin1"]:
        try:
            df = pd.read_csv(io.BytesIO(contents), sep=";", encoding=enc)
            if len(df.columns) < 2:
                df = pd.read_csv(io.BytesIO(contents), sep=",", encoding=enc)
            return df
        except UnicodeDecodeError:
            continue
    raise ValueError("Format d'encodage non supporté. Veuillez sauvegarder en UTF-8.")

@app.post("/api/run-pipeline")
async def run_pipeline(
    use_gemini: str = Form("false"), 
    api_key: str = Form(""),
    mcq_file: Optional[UploadFile] = File(None),
    human_file: Optional[UploadFile] = File(None)
):
    if not mcq_file:
        raise HTTPException(status_code=400, detail="Veuillez d'abord uploader un fichier MCQ Bank (CSV).")
        
    try:
        contents = await mcq_file.read()
        mcq_df = read_csv_robust(contents)
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Erreur lors de la lecture du fichier CSV: {str(e)}")
    
    # Normalize column names: lowercase and strip whitespace
    mcq_df.columns = [c.strip().lower() for c in mcq_df.columns]
    
    # Map common header variants to expected names
    col_map = {
        'choice_a': 'option_a', 'choice_b': 'option_b', 'choice_c': 'option_c', 'choice_d': 'option_d',
        'correct_answer': 'correct_option', 'difficulty': 'difficulty_expert',
        'question_text': 'question', 'q': 'question',
    }
    mcq_df.rename(columns=col_map, inplace=True)
    
    # Add item_id if not present
    if 'item_id' not in mcq_df.columns:
        mcq_df.insert(0, 'item_id', range(len(mcq_df)))
    
    try:
        use_ai = use_gemini.lower() == "true"

        if use_ai and (api_key or GEMINI_API_KEY):
            solver = GeminiMCQSolver(api_key=api_key or GEMINI_API_KEY)
        else:
            solver = MockMCQSolver()
            
        calibrated_df = solver.solve_all(mcq_df)
        prob_matrix = build_probability_matrix(calibrated_df)
        
        crowd_gen = CrowdGenerator(calibrated_df, prob_matrix)
        crowd_gen.generate_crowd()
        art_matrix = crowd_gen.get_response_matrix()
        
        human_matrix = art_matrix # Fallback to artificial if no human data
        if human_file:
            try:
                h_contents = await human_file.read()
                h_df = read_csv_robust(h_contents)
                human_matrix = h_df.pivot_table(index="student_id", columns="item_id", values="response", fill_value=0).values
            except Exception as e:
                print(f"[WARNING] Could not parse human_file: {e}") # fallback to art_matrix
            
        human_rasch = RaschModel()
        human_rasch.fit(human_matrix)
        
        art_rasch = RaschModel()
        art_rasch.fit(art_matrix)
        
        comp = PsychometricComparator(human_rasch, art_rasch, calibrated_df)
        
        # Save outputs for frontend
        crowd_gen.crowd_df.to_csv("outputs/response_matrix.csv", index=False)
        comp.compare_df().to_csv("outputs/rasch_comparison.csv", index=False)
        
        # Generate plots
        comp.plot_scatter("outputs/plots/scatter_comparison.png")
        comp.plot_difficulty_distributions("outputs/plots/difficulty_distribution.png")
        comp.plot_wright_map("outputs/plots/wright_map.png")
        
        report = {
            "human_rasch": {
                "converged": human_rasch.converged,
                "n_iterations": getattr(human_rasch, "iterations", 0),
                "mean_ability": float(np.mean(human_rasch.theta)),
                "std_ability": float(np.std(human_rasch.theta)),
                "mean_difficulty": float(np.mean(human_rasch.b)),
                "std_difficulty": float(np.std(human_rasch.b)),
            },
            "art_rasch": {
                "converged": art_rasch.converged,
                "n_iterations": getattr(art_rasch, "iterations", 0),
                "mean_ability": float(np.mean(art_rasch.theta)),
                "std_ability": float(np.std(art_rasch.theta)),
                "mean_difficulty": float(np.mean(art_rasch.b)),
                "std_difficulty": float(np.std(art_rasch.b)),
            },
            "comparison": comp.full_metrics(),
            "human_thetas": {f"H_{i}": float(human_rasch.theta[i]) for i in range(len(human_rasch.theta))},
            "art_thetas": {f"A_{i}": float(art_rasch.theta[i]) for i in range(len(art_rasch.theta))},
            "context_summary": "Analyse effectuée avec succès via Gemini API.",
            "ai_recommendations": [],
            "plots_data": {
                "scatter": comp.get_scatter_data(),
                "distribution": comp.get_distribution_data(),
                "wright": comp.get_wright_map_data()
            }
        }
        
        return {"report": report}

    except Exception as e:
        import traceback
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=f"Erreur pipeline: {str(e)}")



# ── AI STUDIO ENDPOINTS (DEEPSEEK) ────────────────────────────────────────

class ChatMessage(BaseModel):
    role: str   # "user" or "assistant"
    content: str

class ChatRequest(BaseModel):
    messages: List[ChatMessage]
    report_context: Optional[Dict] = None
    api_key: Optional[str] = ""

class InterpretRequest(BaseModel):
    report: Dict
    api_key: Optional[str] = ""

class ArticleRequest(BaseModel):
    report: Dict
    api_key: Optional[str] = ""


def _get_deepseek_client(api_key: str = ""):
    import openai
    key = api_key or DEEPSEEK_API_KEY
    if not key:
        raise HTTPException(status_code=400, detail="Clé API DeepSeek requise pour cette fonctionnalité.")
    return openai.OpenAI(api_key=key, base_url="https://api.deepseek.com")


@app.post("/api/ai/interpret-rasch")
async def interpret_rasch(req: InterpretRequest):
    """Génère une interprétation pédagogique des résultats Rasch via DeepSeek."""
    client = _get_deepseek_client(req.api_key)
    r = req.report
    prompt = f"""Tu es un expert en psychométrie et en pédagogie. 
Voici les résultats d'une calibration par le Modèle de Rasch (1PL) sur une banque de questions MCQ.

=== RÉSULTATS HUMAINS ===
- Convergence : {r.get('human_rasch', {}).get('converged', False)}
- Capacité moyenne (θ) : {r.get('human_rasch', {}).get('mean_ability', 0):.3f} logits
- Écart-type des capacités : {r.get('human_rasch', {}).get('std_ability', 0):.3f}
- Difficulté moyenne (b) : {r.get('human_rasch', {}).get('mean_difficulty', 0):.3f} logits
- Écart-type des difficultés : {r.get('human_rasch', {}).get('std_difficulty', 0):.3f}

=== RÉSULTATS FOULE ARTIFICIELLE ===
- Convergence : {r.get('art_rasch', {}).get('converged', False)}
- Capacité moyenne (θ) : {r.get('art_rasch', {}).get('mean_ability', 0):.3f} logits
- Écart-type des capacités : {r.get('art_rasch', {}).get('std_ability', 0):.3f}
- Difficulté moyenne (b) : {r.get('art_rasch', {}).get('mean_difficulty', 0):.3f} logits

=== MÉTRIQUES DE COMPARAISON ===
- Pearson r : {r.get('comparison', {}).get('pearson_r', 0):.4f}
- Spearman ρ : {r.get('comparison', {}).get('spearman_r', 0):.4f}
- MAE : {r.get('comparison', {}).get('mae', 0):.4f}
- Verdict : {r.get('comparison', {}).get('interpretation', 'N/A')}

Rédige une interprétation pédagogique complète en français (400-600 mots) structurée en 3 parties :
1. **Analyse des capacités étudiantes** : que révèle la distribution θ sur le niveau général de la classe ?
2. **Analyse des difficultés des items** : les items sont-ils bien calibrés pour ce niveau ?
3. **Qualité de la simulation artificielle** : que signifient les métriques de corrélation ? La foule artificielle est-elle représentative ?

Conclus avec 2-3 recommandations concrètes pour l'enseignant."""

    response = client.chat.completions.create(
        model=DEEPSEEK_MODEL,
        messages=[{"role": "user", "content": prompt}]
    )
    return {"interpretation": response.choices[0].message.content}


@app.post("/api/ai/generate-article")
async def generate_article(req: ArticleRequest):
    """Génère une section 'Résultats' pour un article scientifique à partir du rapport Rasch via DeepSeek."""
    client = _get_deepseek_client(req.api_key)
    r = req.report
    prompt = f"""Tu es un chercheur en sciences de l'éducation spécialisé en psychométrie.
Rédige la section "Résultats" d'un article scientifique académique (format APA) en français,
basée sur les données de calibration Rasch suivantes :

Capacité humaine moyenne : θ̄ = {r.get('human_rasch', {}).get('mean_ability', 0):.3f} (σ={r.get('human_rasch', {}).get('std_ability', 0):.3f})
Difficulté humaine moyenne : b̄ = {r.get('human_rasch', {}).get('mean_difficulty', 0):.3f} (σ={r.get('human_rasch', {}).get('std_difficulty', 0):.3f})
Capacité artificielle moyenne : θ̄ = {r.get('art_rasch', {}).get('mean_ability', 0):.3f} (σ={r.get('art_rasch', {}).get('std_ability', 0):.3f})
Corrélation de Pearson (humain/artificiel) : r = {r.get('comparison', {}).get('pearson_r', 0):.4f}
Corrélation de Spearman : ρ = {r.get('comparison', {}).get('spearman_r', 0):.4f}
MAE : {r.get('comparison', {}).get('mae', 0):.4f}
Verdict : {r.get('comparison', {}).get('interpretation', 'N/A')}

La section doit :
- Être rédigée au passé, en style académique neutre
- Inclure les statistiques descriptives dans le corps du texte
- Commenter la convergence du modèle et la signification des paramètres Rasch
- Comparer systématiquement les données humaines aux données artificielles
- Se terminer par une phrase de transition vers la section Discussion
- Faire environ 400-500 mots"""

    response = client.chat.completions.create(
        model=DEEPSEEK_MODEL,
        messages=[{"role": "user", "content": prompt}]
    )
    return {"article_section": response.choices[0].message.content}


@app.post("/api/ai/chat")
async def analytical_chat(req: ChatRequest):
    """Chat analytique NLP sur les données psychométriques via DeepSeek."""
    client = _get_deepseek_client(req.api_key)

    system_prompt = """Tu es PSYCHO, l'assistant analytique de PsychoCrowd, expert en psychométrie, 
modèle de Rasch, théorie de réponse à l'item (IRT), et analyse de données éducatives.
Tu réponds en français, de manière précise et pédagogique.
Si des données de rapport sont fournies, base-toi sur elles pour répondre.
Si une question sort du domaine psychométrique/éducatif, redirige poliment vers le domaine."""

    if req.report_context:
        r = req.report_context
        context = f"""
=== DONNÉES DE LA SESSION EN COURS ===
Capacité humaine moyenne : {r.get('human_rasch', {}).get('mean_ability', 'N/A')}
Difficulté humaine moyenne : {r.get('human_rasch', {}).get('mean_difficulty', 'N/A')}
Pearson r (humain/artificiel) : {r.get('comparison', {}).get('pearson_r', 'N/A')}
Verdict : {r.get('comparison', {}).get('interpretation', 'N/A')}
==="""
        system_prompt += context

    messages = [{"role": "system", "content": system_prompt}]
    messages.extend([{"role": m.role, "content": m.content} for m in req.messages])

    response = client.chat.completions.create(
        model=DEEPSEEK_MODEL,
        messages=messages
    )
    return {"reply": response.choices[0].message.content}