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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import os
import re
import logging
from datetime import datetime
from typing import List, Dict, Any, Optional
from fastapi import FastAPI, HTTPException, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
import torch
from transformers import (
    AutoTokenizer, 
    AutoModel,
    AutoModelForMaskedLM,
    pipeline
)
import numpy as np

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Initialize FastAPI app
app = FastAPI(
    title="SobroJuriBert API - Full Version",
    description="French Legal AI API powered by JuriBERT with complete functionality",
    version="2.0.0"
)

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

# Global model storage
models = {}
tokenizers = {}
models_loaded = False

# Pydantic models
class TextRequest(BaseModel):
    text: str = Field(..., description="Text to analyze")
    
class MaskFillRequest(BaseModel):
    text: str = Field(..., description="Text with [MASK] tokens")
    top_k: int = Field(5, description="Number of predictions to return")
    
class NERRequest(BaseModel):
    text: str = Field(..., description="Legal text for entity extraction")
    
class QARequest(BaseModel):
    context: str = Field(..., description="Legal document context")
    question: str = Field(..., description="Question about the document")
    
class ClassificationRequest(BaseModel):
    text: str = Field(..., description="Legal document to classify")
    
class EmbeddingRequest(BaseModel):
    texts: List[str] = Field(..., description="List of texts to embed")

async def load_models_on_demand():
    """Load models on first request"""
    global models_loaded
    if models_loaded:
        return
    
    logger.info("Loading JuriBERT models on demand...")
    try:
        # Load JuriBERT for embeddings and mask filling
        models['juribert_base'] = AutoModel.from_pretrained(
            'dascim/juribert-base', 
            cache_dir="/app/.cache/huggingface"
        )
        tokenizers['juribert_base'] = AutoTokenizer.from_pretrained(
            'dascim/juribert-base',
            cache_dir="/app/.cache/huggingface"
        )
        models['juribert_mlm'] = AutoModelForMaskedLM.from_pretrained(
            'dascim/juribert-base',
            cache_dir="/app/.cache/huggingface"
        )
        models_loaded = True
        logger.info("JuriBERT models loaded successfully!")
    except Exception as e:
        logger.error(f"Error loading models: {e}")
        raise HTTPException(status_code=503, detail="Models could not be loaded")

@app.get("/")
async def root():
    """Root endpoint with API information"""
    return {
        "name": "SobroJuriBert API - Full Version",
        "version": "2.0.0",
        "description": "Complete French Legal AI API",
        "status": "operational",
        "endpoints": {
            "mask_fill": "/mask-fill - Fill masked tokens in legal text",
            "embeddings": "/embeddings - Generate legal text embeddings",
            "ner": "/ner - Extract legal entities (enhanced)",
            "qa": "/qa - Answer questions about legal documents",
            "classify": "/classify - Classify legal documents",
            "health": "/health - Health check"
        },
        "models": {
            "base": "dascim/juribert-base",
            "status": "loaded" if models_loaded else "on-demand"
        }
    }

@app.post("/mask-fill")
async def mask_fill(request: MaskFillRequest):
    """Fill [MASK] tokens in French legal text using JuriBERT"""
    await load_models_on_demand()
    
    try:
        tokenizer = tokenizers['juribert_base']
        model = models['juribert_mlm']
        
        # Create pipeline
        fill_mask = pipeline(
            'fill-mask', 
            model=model, 
            tokenizer=tokenizer,
            device=-1  # CPU
        )
        
        # Get predictions
        predictions = fill_mask(request.text, top_k=request.top_k)
        
        return {
            "input": request.text,
            "predictions": [
                {
                    "sequence": pred['sequence'],
                    "score": float(pred['score']),
                    "token": pred['token_str']
                }
                for pred in predictions
            ]
        }
        
    except Exception as e:
        logger.error(f"Mask fill error: {e}")
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/embeddings")
async def generate_embeddings(request: EmbeddingRequest):
    """Generate embeddings for French legal texts using JuriBERT"""
    await load_models_on_demand()
    
    try:
        tokenizer = tokenizers['juribert_base']
        model = models['juribert_base']
        
        embeddings = []
        for text in request.texts:
            # Tokenize
            inputs = tokenizer(
                text, 
                return_tensors="pt", 
                truncation=True, 
                max_length=512,
                padding=True
            )
            
            # Generate embeddings
            with torch.no_grad():
                outputs = model(**inputs)
                # Use mean pooling
                attention_mask = inputs['attention_mask']
                token_embeddings = outputs.last_hidden_state
                input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
                embedding = torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
                embeddings.append(embedding.squeeze().numpy().tolist())
        
        return {
            "embeddings": embeddings,
            "dimension": len(embeddings[0]) if embeddings else 0,
            "model": "juribert-base"
        }
        
    except Exception as e:
        logger.error(f"Embedding error: {e}")
        raise HTTPException(status_code=500, detail=str(e))

def extract_enhanced_entities(text: str) -> List[Dict[str, Any]]:
    """Enhanced entity extraction for French legal text"""
    entities = []
    
    # Extract persons (PER)
    person_patterns = [
        r'\b(?:M\.|Mme|Mlle|Me|Dr|Prof\.?)\s+[A-Z][a-zÀ-ÿ]+(?:\s+[A-Z][a-zÀ-ÿ]+)*',
        r'\b[A-Z][a-zÀ-ÿ]+\s+[A-Z][A-Z]+\b',  # Jean DUPONT
    ]
    
    for pattern in person_patterns:
        for match in re.finditer(pattern, text):
            entities.append({
                "text": match.group(),
                "type": "PER",
                "start": match.start(),
                "end": match.end()
            })
    
    # Extract money amounts (MONEY)
    money_patterns = [
        r'\b\d{1,3}(?:\s?\d{3})*(?:[,\.]\d{2})?\s?(?:€|EUR|euros?)\b',
        r'\b(?:€|EUR)\s?\d{1,3}(?:\s?\d{3})*(?:[,\.]\d{2})?\b',
    ]
    
    for pattern in money_patterns:
        for match in re.finditer(pattern, text, re.IGNORECASE):
            entities.append({
                "text": match.group(),
                "type": "MONEY",
                "start": match.start(),
                "end": match.end()
            })
    
    # Extract legal references (LEGAL_REF)
    legal_patterns = [
        r'article\s+(?:L\.?)?\d+(?:-\d+)?(?:\s+(?:alinéa|al\.)\s+\d+)?',
        r'articles?\s+\d+\s+(?:à|et)\s+\d+',
        r'(?:loi|décret|ordonnance)\s+n°\s*\d{4}-\d+',
        r'directive\s+\d{4}/\d+/[A-Z]+',
    ]
    
    for pattern in legal_patterns:
        for match in re.finditer(pattern, text, re.IGNORECASE):
            entities.append({
                "text": match.group(),
                "type": "LEGAL_REF",
                "start": match.start(),
                "end": match.end()
            })
    
    # Extract dates (DATE)
    date_patterns = [
        r'\b\d{1,2}[/-]\d{1,2}[/-]\d{2,4}\b',
        r'\b\d{1,2}\s+(?:janvier|février|mars|avril|mai|juin|juillet|août|septembre|octobre|novembre|décembre)\s+\d{4}\b',
    ]
    
    for pattern in date_patterns:
        for match in re.finditer(pattern, text, re.IGNORECASE):
            entities.append({
                "text": match.group(),
                "type": "DATE",
                "start": match.start(),
                "end": match.end()
            })
    
    # Extract organizations (ORG)
    org_patterns = [
        r'\b(?:SARL|SAS|SA|EURL|SCI|SASU|SNC)\s+[A-Z][A-Za-zÀ-ÿ\s&\'-]+',
        r'\b(?:Société|Entreprise|Compagnie|Association)\s+[A-Z][A-Za-zÀ-ÿ\s&\'-]+',
    ]
    
    for pattern in org_patterns:
        for match in re.finditer(pattern, text):
            entities.append({
                "text": match.group(),
                "type": "ORG",
                "start": match.start(),
                "end": match.end()
            })
    
    # Extract courts (COURT)
    court_patterns = [
        r'(?:Cour|Tribunal|Conseil)\s+(?:de\s+)?[A-Za-zÀ-ÿ\s\'-]+?(?=\s|,|\.)',
    ]
    
    for pattern in court_patterns:
        for match in re.finditer(pattern, text, re.IGNORECASE):
            entities.append({
                "text": match.group().strip(),
                "type": "COURT",
                "start": match.start(),
                "end": match.end()
            })
    
    # Remove duplicates and sort by position
    seen = set()
    unique_entities = []
    for ent in sorted(entities, key=lambda x: x['start']):
        key = (ent['text'], ent['type'], ent['start'])
        if key not in seen:
            seen.add(key)
            unique_entities.append(ent)
    
    return unique_entities

@app.post("/ner")
async def extract_entities(request: NERRequest):
    """Enhanced NER for French legal text"""
    try:
        entities = extract_enhanced_entities(request.text)
        
        # Group by type for summary
        entity_summary = {}
        for ent in entities:
            if ent['type'] not in entity_summary:
                entity_summary[ent['type']] = []
            entity_summary[ent['type']].append(ent['text'])
        
        return {
            "entities": entities,
            "summary": {
                ent_type: list(set(texts))  # Unique entities per type
                for ent_type, texts in entity_summary.items()
            },
            "total": len(entities),
            "text": request.text
        }
        
    except Exception as e:
        logger.error(f"NER error: {e}")
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/qa")
async def question_answering(request: QARequest):
    """Answer questions about French legal documents"""
    await load_models_on_demand()
    
    try:
        # Generate embeddings for context and question
        embedding_req = EmbeddingRequest(texts=[request.context, request.question])
        embeddings = await generate_embeddings(embedding_req)
        
        context_emb = np.array(embeddings['embeddings'][0])
        question_emb = np.array(embeddings['embeddings'][1])
        
        # Calculate similarity
        similarity = np.dot(context_emb, question_emb) / (np.linalg.norm(context_emb) * np.linalg.norm(question_emb))
        
        # Extract relevant part of context based on question keywords
        question_words = set(request.question.lower().split())
        sentences = request.context.split('.')
        
        relevant_sentences = []
        for sent in sentences:
            sent_words = set(sent.lower().split())
            overlap = len(question_words & sent_words)
            if overlap > 0:
                relevant_sentences.append((sent.strip(), overlap))
        
        # Sort by relevance
        relevant_sentences.sort(key=lambda x: x[1], reverse=True)
        
        if relevant_sentences:
            answer = relevant_sentences[0][0]
            confidence = min(0.9, similarity + 0.3)
        else:
            answer = "Aucune réponse trouvée dans le contexte fourni."
            confidence = 0.1
        
        return {
            "question": request.question,
            "answer": answer,
            "confidence": float(confidence),
            "context_relevance": float(similarity),
            "model": "juribert-base (similarity-based QA)"
        }
        
    except Exception as e:
        logger.error(f"QA error: {e}")
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/classify")
async def classify_document(request: ClassificationRequest):
    """Enhanced document classification"""
    try:
        text_lower = request.text.lower()
        
        # Enhanced categories with more keywords
        categories = {
            "contract": {
                "keywords": ["contrat", "accord", "convention", "parties", "obligations", "clause", "engagement"],
                "weight": 1.0
            },
            "litigation": {
                "keywords": ["tribunal", "jugement", "litige", "procès", "avocat", "défendeur", "demandeur", "arrêt", "décision"],
                "weight": 1.2
            },
            "corporate": {
                "keywords": ["société", "sarl", "sas", "entreprise", "capital", "associés", "statuts", "assemblée"],
                "weight": 1.0
            },
            "employment": {
                "keywords": ["travail", "salarié", "employeur", "licenciement", "contrat de travail", "cdi", "cdd", "rupture"],
                "weight": 1.1
            },
            "real_estate": {
                "keywords": ["immobilier", "location", "bail", "propriété", "locataire", "propriétaire", "loyer"],
                "weight": 1.0
            },
            "intellectual_property": {
                "keywords": ["brevet", "marque", "propriété intellectuelle", "invention", "droit d'auteur", "œuvre"],
                "weight": 1.0
            }
        }
        
        scores = {}
        matched_keywords = {}
        
        for category, info in categories.items():
            score = 0
            keywords_found = []
            for keyword in info['keywords']:
                if keyword in text_lower:
                    count = text_lower.count(keyword)
                    score += count * info['weight']
                    keywords_found.append(keyword)
            
            if score > 0:
                scores[category] = score
                matched_keywords[category] = keywords_found
        
        if not scores:
            primary_category = "general"
            confidence = 0.3
        else:
            total_score = sum(scores.values())
            primary_category = max(scores, key=scores.get)
            confidence = min(0.95, scores[primary_category] / total_score + 0.2)
        
        return {
            "primary_category": primary_category,
            "categories": [
                {
                    "category": cat, 
                    "score": score,
                    "keywords_found": matched_keywords.get(cat, [])
                } 
                for cat, score in sorted(scores.items(), key=lambda x: x[1], reverse=True)
            ],
            "confidence": float(confidence),
            "document_type": "legal_document"
        }
        
    except Exception as e:
        logger.error(f"Classification error: {e}")
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/health")
async def health_check():
    """Health check endpoint"""
    return {
        "status": "healthy",
        "timestamp": datetime.utcnow().isoformat(),
        "version": "2.0.0",
        "models_loaded": models_loaded,
        "available_models": list(models.keys())
    }

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)