Spaces:
Sleeping
Sleeping
Sobro API
commited on
Commit
·
c914f37
0
Parent(s):
Initial SobroJuriBert deployment with JuriBERT integration
Browse files- .gitignore +26 -0
- Dockerfile +27 -0
- README.md +66 -0
- app/__init__.py +1 -0
- app/models/__init__.py +1 -0
- app/utils/__init__.py +1 -0
- main.py +333 -0
- main_endpoints.py +160 -0
- requirements.txt +31 -0
.gitignore
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
__pycache__/
|
2 |
+
*.py[cod]
|
3 |
+
*$py.class
|
4 |
+
*.so
|
5 |
+
.Python
|
6 |
+
build/
|
7 |
+
develop-eggs/
|
8 |
+
dist/
|
9 |
+
downloads/
|
10 |
+
eggs/
|
11 |
+
.eggs/
|
12 |
+
lib/
|
13 |
+
lib64/
|
14 |
+
parts/
|
15 |
+
sdist/
|
16 |
+
var/
|
17 |
+
wheels/
|
18 |
+
*.egg-info/
|
19 |
+
.installed.cfg
|
20 |
+
*.egg
|
21 |
+
.env
|
22 |
+
venv/
|
23 |
+
ENV/
|
24 |
+
.vscode/
|
25 |
+
.idea/
|
26 |
+
*.log
|
Dockerfile
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10-slim
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
# Install system dependencies
|
6 |
+
RUN apt-get update && apt-get install -y \
|
7 |
+
git \
|
8 |
+
build-essential \
|
9 |
+
libpq-dev \
|
10 |
+
&& rm -rf /var/lib/apt/lists/*
|
11 |
+
|
12 |
+
# Copy requirements and install Python dependencies
|
13 |
+
COPY requirements.txt .
|
14 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
15 |
+
|
16 |
+
# Download required NLTK data
|
17 |
+
RUN python -m nltk.downloader punkt stopwords
|
18 |
+
|
19 |
+
# Copy application code
|
20 |
+
COPY app/ ./app/
|
21 |
+
COPY main.py .
|
22 |
+
|
23 |
+
# Expose port
|
24 |
+
EXPOSE 7860
|
25 |
+
|
26 |
+
# Run the application
|
27 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: SobroJuriBert
|
3 |
+
emoji: ⚖️
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: indigo
|
6 |
+
sdk: docker
|
7 |
+
pinned: true
|
8 |
+
license: apache-2.0
|
9 |
+
---
|
10 |
+
|
11 |
+
# SobroJuriBert - French Legal AI Assistant
|
12 |
+
|
13 |
+
Production-ready API for French legal document analysis powered by JuriBERT.
|
14 |
+
|
15 |
+
## Features
|
16 |
+
|
17 |
+
### Core Capabilities
|
18 |
+
- **Mask Filling**: Complete masked tokens in French legal text using JuriBERT
|
19 |
+
- **Embeddings**: Generate semantic embeddings for legal documents
|
20 |
+
- **Named Entity Recognition**: Extract legal entities (courts, articles, parties, dates)
|
21 |
+
- **Question Answering**: Answer questions about legal documents
|
22 |
+
- **Document Classification**: Classify legal documents by type and domain
|
23 |
+
- **Contract Analysis**: Comprehensive contract analysis with risk assessment
|
24 |
+
|
25 |
+
### Models Used
|
26 |
+
- **JuriBERT**: French legal BERT trained on 6.3GB of Légifrance data
|
27 |
+
- **CamemBERT-NER**: For named entity recognition
|
28 |
+
|
29 |
+
### API Endpoints
|
30 |
+
|
31 |
+
#### Text Analysis
|
32 |
+
- `POST /mask-fill` - Fill [MASK] tokens in legal text
|
33 |
+
- `POST /embeddings` - Generate text embeddings
|
34 |
+
- `POST /ner` - Extract named entities
|
35 |
+
- `POST /qa` - Question answering
|
36 |
+
- `POST /classify` - Document classification
|
37 |
+
- `POST /analyze-contract` - Contract analysis
|
38 |
+
|
39 |
+
## Usage
|
40 |
+
|
41 |
+
### Example: Mask Filling
|
42 |
+
```python
|
43 |
+
import requests
|
44 |
+
|
45 |
+
response = requests.post(
|
46 |
+
"https://sobroinc-sobrojuribert.hf.space/mask-fill",
|
47 |
+
json={
|
48 |
+
"text": "Le contrat est signé entre les [MASK].",
|
49 |
+
"top_k": 3
|
50 |
+
}
|
51 |
+
)
|
52 |
+
```
|
53 |
+
|
54 |
+
### Example: Named Entity Recognition
|
55 |
+
```python
|
56 |
+
response = requests.post(
|
57 |
+
"https://sobroinc-sobrojuribert.hf.space/ner",
|
58 |
+
json={
|
59 |
+
"text": "Le Tribunal de Grande Instance de Paris a rendu sa décision le 15 janvier 2024"
|
60 |
+
}
|
61 |
+
)
|
62 |
+
```
|
63 |
+
|
64 |
+
## About
|
65 |
+
Created by Sobro Inc. for French legal professionals.
|
66 |
+
Powered by JuriBERT and state-of-the-art French NLP models.
|
app/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
# SobroJuriBert App Package
|
app/models/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
# Models package
|
app/utils/__init__.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
# Utils package
|
main.py
ADDED
@@ -0,0 +1,333 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import logging
|
4 |
+
from datetime import datetime
|
5 |
+
from typing import List, Dict, Any, Optional
|
6 |
+
from fastapi import FastAPI, HTTPException, File, UploadFile, Form
|
7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
8 |
+
from pydantic import BaseModel, Field
|
9 |
+
import torch
|
10 |
+
from transformers import (
|
11 |
+
AutoTokenizer,
|
12 |
+
AutoModel,
|
13 |
+
AutoModelForMaskedLM,
|
14 |
+
AutoModelForTokenClassification,
|
15 |
+
AutoModelForQuestionAnswering,
|
16 |
+
AutoModelForSequenceClassification,
|
17 |
+
pipeline
|
18 |
+
)
|
19 |
+
import numpy as np
|
20 |
+
|
21 |
+
# Configure logging
|
22 |
+
logging.basicConfig(level=logging.INFO)
|
23 |
+
logger = logging.getLogger(__name__)
|
24 |
+
|
25 |
+
# Initialize FastAPI app
|
26 |
+
app = FastAPI(
|
27 |
+
title="SobroJuriBert API",
|
28 |
+
description="French Legal AI API powered by JuriBERT for comprehensive legal document analysis",
|
29 |
+
version="1.0.0"
|
30 |
+
)
|
31 |
+
|
32 |
+
# Add CORS middleware
|
33 |
+
app.add_middleware(
|
34 |
+
CORSMiddleware,
|
35 |
+
allow_origins=["*"],
|
36 |
+
allow_credentials=True,
|
37 |
+
allow_methods=["*"],
|
38 |
+
allow_headers=["*"],
|
39 |
+
)
|
40 |
+
|
41 |
+
# Global model storage
|
42 |
+
models = {}
|
43 |
+
tokenizers = {}
|
44 |
+
|
45 |
+
# Pydantic models
|
46 |
+
class TextRequest(BaseModel):
|
47 |
+
text: str = Field(..., description="Text to analyze")
|
48 |
+
|
49 |
+
class MaskFillRequest(BaseModel):
|
50 |
+
text: str = Field(..., description="Text with [MASK] tokens")
|
51 |
+
top_k: int = Field(5, description="Number of predictions to return")
|
52 |
+
|
53 |
+
class NERRequest(BaseModel):
|
54 |
+
text: str = Field(..., description="Legal text for entity extraction")
|
55 |
+
|
56 |
+
class QARequest(BaseModel):
|
57 |
+
context: str = Field(..., description="Legal document context")
|
58 |
+
question: str = Field(..., description="Question about the document")
|
59 |
+
|
60 |
+
class ClassificationRequest(BaseModel):
|
61 |
+
text: str = Field(..., description="Legal document to classify")
|
62 |
+
|
63 |
+
class EmbeddingRequest(BaseModel):
|
64 |
+
texts: List[str] = Field(..., description="List of texts to embed")
|
65 |
+
|
66 |
+
class JurisprudenceSearchRequest(BaseModel):
|
67 |
+
query: str = Field(..., description="Search query")
|
68 |
+
filters: Optional[Dict[str, Any]] = Field(None, description="Filters for search")
|
69 |
+
limit: int = Field(10, description="Number of results")
|
70 |
+
|
71 |
+
class ContractAnalysisRequest(BaseModel):
|
72 |
+
text: str = Field(..., description="Contract text to analyze")
|
73 |
+
contract_type: Optional[str] = Field(None, description="Type of contract")
|
74 |
+
|
75 |
+
@app.on_event("startup")
|
76 |
+
async def load_models():
|
77 |
+
"""Load all required models on startup"""
|
78 |
+
logger.info("Loading French legal models...")
|
79 |
+
|
80 |
+
try:
|
81 |
+
# Load JuriBERT base model for embeddings and mask filling
|
82 |
+
logger.info("Loading JuriBERT base model...")
|
83 |
+
models['juribert_base'] = AutoModel.from_pretrained('dascim/juribert-base')
|
84 |
+
tokenizers['juribert_base'] = AutoTokenizer.from_pretrained('dascim/juribert-base')
|
85 |
+
models['juribert_mlm'] = AutoModelForMaskedLM.from_pretrained('dascim/juribert-base')
|
86 |
+
|
87 |
+
# Load CamemBERT models as fallback/complement
|
88 |
+
logger.info("Loading CamemBERT models...")
|
89 |
+
models['camembert_ner'] = pipeline(
|
90 |
+
'ner',
|
91 |
+
model='Jean-Baptiste/camembert-ner-with-dates',
|
92 |
+
aggregation_strategy="simple"
|
93 |
+
)
|
94 |
+
|
95 |
+
# Load legal-specific models
|
96 |
+
logger.info("Loading French legal classification model...")
|
97 |
+
models['legal_classifier'] = pipeline(
|
98 |
+
'text-classification',
|
99 |
+
model='nlptown/bert-base-multilingual-uncased-sentiment' # Placeholder
|
100 |
+
)
|
101 |
+
|
102 |
+
logger.info("All models loaded successfully!")
|
103 |
+
|
104 |
+
except Exception as e:
|
105 |
+
logger.error(f"Error loading models: {e}")
|
106 |
+
raise
|
107 |
+
|
108 |
+
@app.get("/")
|
109 |
+
async def root():
|
110 |
+
"""Root endpoint with API information"""
|
111 |
+
return {
|
112 |
+
"name": "SobroJuriBert API",
|
113 |
+
"version": "1.0.0",
|
114 |
+
"description": "French Legal AI API for lawyers",
|
115 |
+
"endpoints": {
|
116 |
+
"mask_fill": "/mask-fill - Fill masked tokens in legal text",
|
117 |
+
"embeddings": "/embeddings - Generate legal text embeddings",
|
118 |
+
"ner": "/ner - Extract legal entities",
|
119 |
+
"qa": "/qa - Answer questions about legal documents",
|
120 |
+
"classify": "/classify - Classify legal documents",
|
121 |
+
"analyze_contract": "/analyze-contract - Analyze legal contracts",
|
122 |
+
"search_jurisprudence": "/search-jurisprudence - Search case law",
|
123 |
+
"extract_articles": "/extract-articles - Extract legal article references",
|
124 |
+
"check_compliance": "/check-compliance - Check legal compliance",
|
125 |
+
"generate_summary": "/generate-summary - Generate legal summaries"
|
126 |
+
},
|
127 |
+
"models": {
|
128 |
+
"base": "dascim/juribert-base",
|
129 |
+
"ner": "Jean-Baptiste/camembert-ner-with-dates",
|
130 |
+
"training_data": "6.3GB French legal texts from Légifrance + 100k+ court decisions"
|
131 |
+
}
|
132 |
+
}
|
133 |
+
|
134 |
+
@app.post("/mask-fill")
|
135 |
+
async def mask_fill(request: MaskFillRequest):
|
136 |
+
"""Fill [MASK] tokens in French legal text"""
|
137 |
+
try:
|
138 |
+
tokenizer = tokenizers['juribert_base']
|
139 |
+
model = models['juribert_mlm']
|
140 |
+
|
141 |
+
# Create pipeline
|
142 |
+
fill_mask = pipeline('fill-mask', model=model, tokenizer=tokenizer)
|
143 |
+
|
144 |
+
# Get predictions
|
145 |
+
predictions = fill_mask(request.text, top_k=request.top_k)
|
146 |
+
|
147 |
+
return {
|
148 |
+
"input": request.text,
|
149 |
+
"predictions": [
|
150 |
+
{
|
151 |
+
"sequence": pred['sequence'],
|
152 |
+
"score": pred['score'],
|
153 |
+
"token": pred['token_str']
|
154 |
+
}
|
155 |
+
for pred in predictions
|
156 |
+
]
|
157 |
+
}
|
158 |
+
|
159 |
+
except Exception as e:
|
160 |
+
logger.error(f"Mask fill error: {e}")
|
161 |
+
raise HTTPException(status_code=500, detail=str(e))
|
162 |
+
|
163 |
+
@app.post("/embeddings")
|
164 |
+
async def generate_embeddings(request: EmbeddingRequest):
|
165 |
+
"""Generate embeddings for French legal texts"""
|
166 |
+
try:
|
167 |
+
tokenizer = tokenizers['juribert_base']
|
168 |
+
model = models['juribert_base']
|
169 |
+
|
170 |
+
embeddings = []
|
171 |
+
for text in request.texts:
|
172 |
+
# Tokenize
|
173 |
+
inputs = tokenizer(
|
174 |
+
text,
|
175 |
+
return_tensors="pt",
|
176 |
+
truncation=True,
|
177 |
+
max_length=512,
|
178 |
+
padding=True
|
179 |
+
)
|
180 |
+
|
181 |
+
# Generate embeddings
|
182 |
+
with torch.no_grad():
|
183 |
+
outputs = model(**inputs)
|
184 |
+
# Use CLS token embedding
|
185 |
+
embedding = outputs.last_hidden_state[:, 0, :].squeeze().numpy()
|
186 |
+
embeddings.append(embedding.tolist())
|
187 |
+
|
188 |
+
return {
|
189 |
+
"embeddings": embeddings,
|
190 |
+
"dimension": len(embeddings[0]) if embeddings else 0
|
191 |
+
}
|
192 |
+
|
193 |
+
except Exception as e:
|
194 |
+
logger.error(f"Embedding error: {e}")
|
195 |
+
raise HTTPException(status_code=500, detail=str(e))
|
196 |
+
|
197 |
+
@app.post("/ner")
|
198 |
+
async def extract_entities(request: NERRequest):
|
199 |
+
"""Extract named entities from French legal text"""
|
200 |
+
try:
|
201 |
+
# Use CamemBERT NER model
|
202 |
+
ner_pipeline = models['camembert_ner']
|
203 |
+
entities = ner_pipeline(request.text)
|
204 |
+
|
205 |
+
# Format results
|
206 |
+
formatted_entities = []
|
207 |
+
for entity in entities:
|
208 |
+
formatted_entities.append({
|
209 |
+
"text": entity['word'],
|
210 |
+
"type": entity['entity_group'],
|
211 |
+
"score": entity['score'],
|
212 |
+
"start": entity['start'],
|
213 |
+
"end": entity['end']
|
214 |
+
})
|
215 |
+
|
216 |
+
return {
|
217 |
+
"entities": formatted_entities,
|
218 |
+
"text": request.text
|
219 |
+
}
|
220 |
+
|
221 |
+
except Exception as e:
|
222 |
+
logger.error(f"NER error: {e}")
|
223 |
+
raise HTTPException(status_code=500, detail=str(e))
|
224 |
+
|
225 |
+
@app.post("/qa")
|
226 |
+
async def question_answering(request: QARequest):
|
227 |
+
"""Answer questions about French legal documents"""
|
228 |
+
try:
|
229 |
+
# Simple implementation for now
|
230 |
+
# In production, use a fine-tuned QA model
|
231 |
+
|
232 |
+
return {
|
233 |
+
"question": request.question,
|
234 |
+
"answer": "This feature requires a fine-tuned QA model. Please check back later.",
|
235 |
+
"confidence": 0.0,
|
236 |
+
"relevant_articles": [],
|
237 |
+
"explanation": "QA model is being fine-tuned on French legal data"
|
238 |
+
}
|
239 |
+
|
240 |
+
except Exception as e:
|
241 |
+
logger.error(f"QA error: {e}")
|
242 |
+
raise HTTPException(status_code=500, detail=str(e))
|
243 |
+
|
244 |
+
@app.post("/classify")
|
245 |
+
async def classify_document(request: ClassificationRequest):
|
246 |
+
"""Classify French legal documents"""
|
247 |
+
try:
|
248 |
+
# Simple keyword-based classification for now
|
249 |
+
text_lower = request.text.lower()
|
250 |
+
|
251 |
+
categories = {
|
252 |
+
"contract": ["contrat", "accord", "convention", "parties"],
|
253 |
+
"litigation": ["tribunal", "jugement", "litige", "procès"],
|
254 |
+
"corporate": ["société", "sarl", "sas", "entreprise"],
|
255 |
+
"employment": ["travail", "salarié", "employeur", "licenciement"]
|
256 |
+
}
|
257 |
+
|
258 |
+
scores = {}
|
259 |
+
for category, keywords in categories.items():
|
260 |
+
score = sum(1 for kw in keywords if kw in text_lower)
|
261 |
+
if score > 0:
|
262 |
+
scores[category] = score
|
263 |
+
|
264 |
+
if not scores:
|
265 |
+
primary_category = "general"
|
266 |
+
else:
|
267 |
+
primary_category = max(scores, key=scores.get)
|
268 |
+
|
269 |
+
return {
|
270 |
+
"primary_category": primary_category,
|
271 |
+
"categories": [{"category": cat, "score": score} for cat, score in scores.items()],
|
272 |
+
"confidence": 0.8 if scores else 0.5,
|
273 |
+
"document_type": "legal_document",
|
274 |
+
"legal_domain": primary_category
|
275 |
+
}
|
276 |
+
|
277 |
+
except Exception as e:
|
278 |
+
logger.error(f"Classification error: {e}")
|
279 |
+
raise HTTPException(status_code=500, detail=str(e))
|
280 |
+
|
281 |
+
@app.post("/analyze-contract")
|
282 |
+
async def analyze_contract(request: ContractAnalysisRequest):
|
283 |
+
"""Analyze French legal contracts"""
|
284 |
+
try:
|
285 |
+
# Extract entities first
|
286 |
+
entities_response = await extract_entities(NERRequest(text=request.text))
|
287 |
+
|
288 |
+
# Basic contract analysis
|
289 |
+
text_lower = request.text.lower()
|
290 |
+
|
291 |
+
analysis = {
|
292 |
+
"contract_type": request.contract_type or "general",
|
293 |
+
"parties": [e for e in entities_response['entities'] if e['type'] in ['PER', 'ORG']],
|
294 |
+
"key_clauses": [],
|
295 |
+
"obligations": [],
|
296 |
+
"risks": [],
|
297 |
+
"missing_clauses": [],
|
298 |
+
"recommendations": [],
|
299 |
+
"legal_references": []
|
300 |
+
}
|
301 |
+
|
302 |
+
# Check for key clauses
|
303 |
+
clause_checks = [
|
304 |
+
("price", ["prix", "montant", "coût"]),
|
305 |
+
("duration", ["durée", "période", "terme"]),
|
306 |
+
("termination", ["résiliation", "rupture", "fin"])
|
307 |
+
]
|
308 |
+
|
309 |
+
for clause_name, keywords in clause_checks:
|
310 |
+
if any(kw in text_lower for kw in keywords):
|
311 |
+
analysis['key_clauses'].append(clause_name)
|
312 |
+
else:
|
313 |
+
analysis['missing_clauses'].append(f"Missing {clause_name} clause")
|
314 |
+
analysis['recommendations'].append(f"Add {clause_name} clause")
|
315 |
+
|
316 |
+
return analysis
|
317 |
+
|
318 |
+
except Exception as e:
|
319 |
+
logger.error(f"Contract analysis error: {e}")
|
320 |
+
raise HTTPException(status_code=500, detail=str(e))
|
321 |
+
|
322 |
+
@app.get("/health")
|
323 |
+
async def health_check():
|
324 |
+
"""Health check endpoint"""
|
325 |
+
return {
|
326 |
+
"status": "healthy",
|
327 |
+
"models_loaded": list(models.keys()),
|
328 |
+
"timestamp": datetime.utcnow().isoformat()
|
329 |
+
}
|
330 |
+
|
331 |
+
if __name__ == "__main__":
|
332 |
+
import uvicorn
|
333 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
main_endpoints.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This file contains the endpoint implementations
|
2 |
+
# In production, merge this with main.py
|
3 |
+
|
4 |
+
@app.post("/mask-fill")
|
5 |
+
async def mask_fill(request: MaskFillRequest):
|
6 |
+
"""Fill [MASK] tokens in French legal text"""
|
7 |
+
try:
|
8 |
+
tokenizer = tokenizers['juribert_base']
|
9 |
+
model = models['juribert_mlm']
|
10 |
+
|
11 |
+
# Create pipeline
|
12 |
+
fill_mask = pipeline('fill-mask', model=model, tokenizer=tokenizer)
|
13 |
+
|
14 |
+
# Get predictions
|
15 |
+
predictions = fill_mask(request.text, top_k=request.top_k)
|
16 |
+
|
17 |
+
return {
|
18 |
+
"input": request.text,
|
19 |
+
"predictions": [
|
20 |
+
{
|
21 |
+
"sequence": pred['sequence'],
|
22 |
+
"score": pred['score'],
|
23 |
+
"token": pred['token_str']
|
24 |
+
}
|
25 |
+
for pred in predictions
|
26 |
+
]
|
27 |
+
}
|
28 |
+
|
29 |
+
except Exception as e:
|
30 |
+
logger.error(f"Mask fill error: {e}")
|
31 |
+
raise HTTPException(status_code=500, detail=str(e))
|
32 |
+
|
33 |
+
@app.post("/embeddings")
|
34 |
+
async def generate_embeddings(request: EmbeddingRequest):
|
35 |
+
"""Generate embeddings for French legal texts"""
|
36 |
+
try:
|
37 |
+
tokenizer = tokenizers['juribert_base']
|
38 |
+
model = models['juribert_base']
|
39 |
+
|
40 |
+
embeddings = []
|
41 |
+
for text in request.texts:
|
42 |
+
# Tokenize
|
43 |
+
inputs = tokenizer(
|
44 |
+
text,
|
45 |
+
return_tensors="pt",
|
46 |
+
truncation=True,
|
47 |
+
max_length=512,
|
48 |
+
padding=True
|
49 |
+
)
|
50 |
+
|
51 |
+
# Generate embeddings
|
52 |
+
with torch.no_grad():
|
53 |
+
outputs = model(**inputs)
|
54 |
+
# Use CLS token embedding
|
55 |
+
embedding = outputs.last_hidden_state[:, 0, :].squeeze().numpy()
|
56 |
+
embeddings.append(embedding.tolist())
|
57 |
+
|
58 |
+
return {
|
59 |
+
"embeddings": embeddings,
|
60 |
+
"dimension": len(embeddings[0]) if embeddings else 0
|
61 |
+
}
|
62 |
+
|
63 |
+
except Exception as e:
|
64 |
+
logger.error(f"Embedding error: {e}")
|
65 |
+
raise HTTPException(status_code=500, detail=str(e))
|
66 |
+
|
67 |
+
@app.post("/ner")
|
68 |
+
async def extract_entities(request: NERRequest):
|
69 |
+
"""Extract named entities from French legal text"""
|
70 |
+
try:
|
71 |
+
# Use CamemBERT NER model
|
72 |
+
ner_pipeline = models['camembert_ner']
|
73 |
+
entities = ner_pipeline(request.text)
|
74 |
+
|
75 |
+
# Format results
|
76 |
+
formatted_entities = []
|
77 |
+
for entity in entities:
|
78 |
+
formatted_entities.append({
|
79 |
+
"text": entity['word'],
|
80 |
+
"type": entity['entity_group'],
|
81 |
+
"score": entity['score'],
|
82 |
+
"start": entity['start'],
|
83 |
+
"end": entity['end']
|
84 |
+
})
|
85 |
+
|
86 |
+
return {
|
87 |
+
"entities": formatted_entities,
|
88 |
+
"text": request.text
|
89 |
+
}
|
90 |
+
|
91 |
+
except Exception as e:
|
92 |
+
logger.error(f"NER error: {e}")
|
93 |
+
raise HTTPException(status_code=500, detail=str(e))
|
94 |
+
|
95 |
+
@app.post("/qa")
|
96 |
+
async def question_answering(request: QARequest):
|
97 |
+
"""Answer questions about French legal documents"""
|
98 |
+
try:
|
99 |
+
# Simple implementation for now
|
100 |
+
# In production, use a fine-tuned QA model
|
101 |
+
|
102 |
+
return {
|
103 |
+
"question": request.question,
|
104 |
+
"answer": "This feature requires a fine-tuned QA model. Please check back later.",
|
105 |
+
"confidence": 0.0,
|
106 |
+
"relevant_articles": [],
|
107 |
+
"explanation": "QA model is being fine-tuned on French legal data"
|
108 |
+
}
|
109 |
+
|
110 |
+
except Exception as e:
|
111 |
+
logger.error(f"QA error: {e}")
|
112 |
+
raise HTTPException(status_code=500, detail=str(e))
|
113 |
+
|
114 |
+
@app.post("/classify")
|
115 |
+
async def classify_document(request: ClassificationRequest):
|
116 |
+
"""Classify French legal documents"""
|
117 |
+
try:
|
118 |
+
# Simple keyword-based classification for now
|
119 |
+
text_lower = request.text.lower()
|
120 |
+
|
121 |
+
categories = {
|
122 |
+
"contract": ["contrat", "accord", "convention", "parties"],
|
123 |
+
"litigation": ["tribunal", "jugement", "litige", "procès"],
|
124 |
+
"corporate": ["société", "sarl", "sas", "entreprise"],
|
125 |
+
"employment": ["travail", "salarié", "employeur", "licenciement"]
|
126 |
+
}
|
127 |
+
|
128 |
+
scores = {}
|
129 |
+
for category, keywords in categories.items():
|
130 |
+
score = sum(1 for kw in keywords if kw in text_lower)
|
131 |
+
if score > 0:
|
132 |
+
scores[category] = score
|
133 |
+
|
134 |
+
if not scores:
|
135 |
+
primary_category = "general"
|
136 |
+
else:
|
137 |
+
primary_category = max(scores, key=scores.get)
|
138 |
+
|
139 |
+
return {
|
140 |
+
"primary_category": primary_category,
|
141 |
+
"categories": [{"category": cat, "score": score} for cat, score in scores.items()],
|
142 |
+
"confidence": 0.8 if scores else 0.5,
|
143 |
+
"document_type": "legal_document",
|
144 |
+
"legal_domain": primary_category
|
145 |
+
}
|
146 |
+
|
147 |
+
except Exception as e:
|
148 |
+
logger.error(f"Classification error: {e}")
|
149 |
+
raise HTTPException(status_code=500, detail=str(e))
|
150 |
+
|
151 |
+
@app.get("/health")
|
152 |
+
async def health_check():
|
153 |
+
"""Health check endpoint"""
|
154 |
+
return {
|
155 |
+
"status": "healthy",
|
156 |
+
"models_loaded": list(models.keys()),
|
157 |
+
"timestamp": datetime.utcnow().isoformat()
|
158 |
+
}
|
159 |
+
|
160 |
+
# Add this to main.py when deploying
|
requirements.txt
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.104.1
|
2 |
+
uvicorn==0.24.0
|
3 |
+
transformers==4.35.2
|
4 |
+
torch==2.1.0
|
5 |
+
sentencepiece==0.1.99
|
6 |
+
protobuf==3.20.3
|
7 |
+
numpy==1.24.3
|
8 |
+
pandas==2.0.3
|
9 |
+
scikit-learn==1.3.0
|
10 |
+
python-multipart==0.0.6
|
11 |
+
aiofiles==23.2.1
|
12 |
+
pydantic==2.5.0
|
13 |
+
python-jose[cryptography]==3.3.0
|
14 |
+
httpx==0.25.1
|
15 |
+
beautifulsoup4==4.12.2
|
16 |
+
lxml==4.9.3
|
17 |
+
pypdf2==3.0.1
|
18 |
+
pdfplumber==0.10.3
|
19 |
+
Pillow==10.1.0
|
20 |
+
openpyxl==3.1.2
|
21 |
+
python-docx==1.1.0
|
22 |
+
nltk==3.8.1
|
23 |
+
spacy==3.7.2
|
24 |
+
sacremoses==0.1.1
|
25 |
+
fugashi==1.3.0
|
26 |
+
unidic-lite==1.0.8
|
27 |
+
elasticsearch==8.11.0
|
28 |
+
redis==5.0.1
|
29 |
+
psycopg2-binary==2.9.9
|
30 |
+
sqlalchemy==2.0.23
|
31 |
+
alembic==1.12.1
|