Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, HTTPException | |
from fastapi.middleware.cors import CORSMiddleware | |
from pydantic import BaseModel | |
from typing import Optional, List, Dict | |
import json | |
app = FastAPI( | |
title="SobroJuriBert API", | |
description="Legal text analysis service (Vercel deployment)", | |
version="1.0.0" | |
) | |
# CORS middleware | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
class AnalysisRequest(BaseModel): | |
text: str | |
analysis_type: Optional[str] = "full" | |
class AnalysisResponse(BaseModel): | |
text: str | |
analysis_type: str | |
results: Dict | |
status: str = "success" | |
async def root(): | |
return { | |
"name": "SobroJuriBert", | |
"version": "1.0.0", | |
"description": "Legal text analysis service", | |
"status": "operational", | |
"note": "Running in lightweight mode on Vercel", | |
"endpoints": [ | |
"/analyze", | |
"/extract_entities", | |
"/classify", | |
"/health" | |
] | |
} | |
async def analyze_text(request: AnalysisRequest): | |
"""Analyze legal text (demo mode)""" | |
try: | |
# In production, this would use JuriBERT model | |
# For Vercel deployment, we return mock results | |
results = { | |
"entities": [ | |
{"text": "contrat", "type": "LEGAL_CONCEPT", "confidence": 0.95}, | |
{"text": "obligation", "type": "LEGAL_CONCEPT", "confidence": 0.92} | |
], | |
"classification": { | |
"category": "contract_law", | |
"confidence": 0.88 | |
}, | |
"summary": "Legal document analysis completed", | |
"keywords": ["contrat", "obligation", "juridique"], | |
"language": "fr" | |
} | |
return AnalysisResponse( | |
text=request.text[:100] + "...", | |
analysis_type=request.analysis_type, | |
results=results | |
) | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |
async def extract_entities(request: AnalysisRequest): | |
"""Extract legal entities from text""" | |
return { | |
"entities": [ | |
{"text": "partie contractante", "type": "PARTY", "confidence": 0.9}, | |
{"text": "tribunal", "type": "INSTITUTION", "confidence": 0.95} | |
], | |
"text_length": len(request.text) | |
} | |
async def classify_document(request: AnalysisRequest): | |
"""Classify legal document type""" | |
return { | |
"classification": { | |
"primary": "contract", | |
"secondary": ["commercial", "services"], | |
"confidence": 0.85 | |
} | |
} | |
async def health_check(): | |
return { | |
"status": "healthy", | |
"service": "SobroJuriBert", | |
"mode": "vercel_lightweight" | |
} | |
# For Vercel | |
handler = app |