A professional FastAPI application designed for commercial real estate professionals to evaluate tenant solvency for rental properties. The system processes financial statements (PDF documents) using a sophisticated three-step LLM pipeline to extract data, calculate 41+ financial ratios, and provide comprehensive tenant risk assessment in French.
- 🏢 Tenant Solvency Evaluation: Specialized analysis for commercial rental decisions with risk levels and recommendations
- 📊 Comprehensive Financial Analysis: 41+ ratios across 6 major categories (Structure, Exploitation, Rentabilité, etc.)
- 📄 PDF Processing: Robust download and analysis of financial statements from URLs with retry logic
- 🤖 Three-Step AI Pipeline:
- Gemini 2.5 Flash: Raw financial data extraction from balance sheets & income statements
- Claude 4: Financial ratio calculation (41+ ratios with precise formulas)
- Claude 4: Professional tenant solvency analysis with 800-word French report
- 💰 Rental Capacity Analysis: Integrates annual rent amount for capacity evaluation (loyer/CA, loyer/EBE ratios)
- 🎯 Risk Assessment: Clear tenant risk levels (faible/moyen/élevé) with specific recommendations
- 🇫🇷 French Professional Output: Optimized for French financial terminology and rental market
- ⚡ High-Performance Architecture: Sequential processing with robust error handling
- 🔗 API-Ready: FastAPI with CORS support for frontend integration
SpiritGenius employs a professional-grade, sequential three-step AI pipeline designed specifically for tenant solvency evaluation in commercial real estate:
- 📥 PDF Download: Robust document retrieval with 120s timeout and 3-retry exponential backoff
- 🔍 Data Extraction (Gemini 2.5 Flash): Raw financial data extraction from PDF documents
- 🧮 Ratio Calculation (Claude 4): Professional computation of 41+ financial ratios
- 📋 Final Analysis (Claude 4): Comprehensive tenant solvency evaluation with risk assessment
- FastAPI Backend: RESTful API with comprehensive CORS support
- Async PDF Processing: High-performance document download with error handling
- Sequential LLM Pipeline: Each step builds on previous outputs for maximum accuracy
- Modular Design: Clean separation of concerns for maintainability
- Professional Error Handling: Comprehensive logging and error recovery at each step
main.py: FastAPI application with CORS and request/response handlingapp.py: Orchestrates the complete 4-step tenant evaluation pipelineclients.py: Initializes and manages AI client connections (Gemini & Claude)pdf_handler.py: Robust PDF download with timeout and retry logicgemini_service.py: Financial data extraction from PDF documents (balance sheets, income statements)claude_ratio_service.py: Professional calculation of 41+ financial ratios with precise formulasclaude_service.py: Final tenant solvency analysis with 800-word French evaluation and risk assessmentlogger.py&logging_config.py: Comprehensive logging infrastructure
Accepts a JSON payload with PDF URL, company name, and annual rent amount, returns comprehensive tenant solvency evaluation in French.
Request:
{
"pdfUrl": "https://example.com/company-accounts.pdf",
"companyName": "Example Company",
"annualRent": "50000"
}Response:
Complete tenant solvency evaluation structured in JSON format:
{
"ratios": {
"structure_financiere": {
"ressources_propres_n": 85420,
"frng_n": 12450,
"bfr_n": 8730,
"tresorerie_nette_n": 3720,
"independance_financiere_n": 0.68,
// ... 15 structure ratios
},
"activite_exploitation": {
"marge_globale_n": 0.312,
"valeur_ajoutee_n": 47560,
"ebe_n": 14230,
"caf_n": 12180,
// ... 12 exploitation ratios
},
"rentabilite": {
"roe_n": 0.085,
"rentabilite_economique_n": 0.092,
// ... 5 profitability ratios
},
"evolution": {
"taux_variation_ca": 0.035,
"taux_variation_resultat_net": -0.932,
// ... 4 evolution ratios
},
"tresorerie_financement": {
"capacite_generation_cash_n": 12180,
"capacite_remboursement_n": 3.16,
// ... 3 treasury ratios
},
"delais_paiement": {
"delai_creances_clients_jours": 45,
"delai_dettes_fournisseurs_jours": 38
}
},
"chiffres_cles": {
"chiffre_affaires_n": "152 450 K€",
"resultat_exploitation_n": "8 450 K€",
"marge_exploitation_n": "5.54%",
"resultat_net_n": "1 274 K€",
"capitaux_propres_n": "85 420 K€",
"dette_financiere_n": "38 450 K€",
"loyer_sur_ca_n": "3.28%",
"loyer_sur_ebe_n": "35.14%",
// ... 24 key figures total
},
"analyse": "Comprehensive 800-word French analysis covering financial evolution, structure, profitability, cash flow, operations, and risk assessment with clear tenant recommendation",
"processing_time": 45.67
}Analysis Structure:
- Évolution des indicateurs: Year-over-year financial performance
- Structure financière: Balance sheet strength and stability
- Rentabilité: Profitability metrics and trends
- CAF et trésorerie: Cash generation and financial capacity
- Exploitation: Operational efficiency analysis
- Cycle d'exploitation: Working capital management
- Conclusion: Tenant risk level (faible/moyen/élevé) with specific recommendations
Health check endpoint to verify the API is running.
# 1. Clone the repository
git clone https://github.com/yourusername/spiritgenius.git
cd spiritgenius
# 2. Install dependencies
pip install -r requirements.txt
# 3. Configure environment variables
cp .env.example .env
# Edit .env with your API keys
# 4. Run the application
hypercorn main:app --reload --bind "localhost:8000"Local URL: http://localhost:8000/api/insights
curl -X POST "http://localhost:8000/api/insights" \
-H "Content-Type: application/json" \
-d '{
"pdfUrl": "https://example.com/financial-statements.pdf",
"companyName": "Test Company SAS",
"annualRent": "75000"
}'Create a .env file in the project root:
# Required API Keys
CLAUDE_API_KEY=your_claude_api_key_here
GEMINI_API_KEY=your_gemini_api_key_here- Fork this repository to your GitHub account
- Create new Railway project from your forked repository
- Configure environment variables in Railway dashboard:
CLAUDE_API_KEY: Your Anthropic Claude API keyGEMINI_API_KEY: Your Google Gemini API key
- Deploy automatically - Railway will use
railway.jsonconfiguration
# Install dependencies
pip install -r requirements.txt
# Set environment variables
export CLAUDE_API_KEY="your_key_here"
export GEMINI_API_KEY="your_key_here"
# Run the application
hypercorn main:app --bind "0.0.0.0:8000"Production URL: https://your-app.railway.app/api/insights
SpiritGenius follows a precise 4-step sequential pipeline:
graph TD
A[📄 PDF URL] --> B[📥 PDF Download<br/>120s timeout, 3 retries]
B --> C[🔍 Gemini 2.5 Flash<br/>Financial Data Extraction]
C --> D[🧮 Claude 4<br/>Ratio Calculation 41+]
D --> E[📋 Claude 4<br/>Tenant Analysis & Risk Assessment]
E --> F[📊 JSON Output<br/>Ratios + Analysis + Risk Level]
-
📥 PDF Download (30-60s)
- Robust aiohttp download with exponential backoff
- 120-second timeout with 3 retry attempts
- Handles network errors and server issues
-
🔍 Data Extraction (15-30s)
- Gemini 2.5 Flash processes PDF content
- Extracts 25+ financial line items
- Returns structured JSON data array
-
🧮 Ratio Calculation (10-20s)
- Claude 4 calculates 41+ professional ratios
- Uses precise French accounting formulas
- Includes rental-specific metrics
-
📋 Final Analysis (20-40s)
- Claude 4 performs comprehensive tenant evaluation
- 800-word French analysis across 7 sections
- Risk assessment with actionable recommendations
Total Processing Time: ~75-150 seconds
- Property Managers: Evaluate prospective tenants before lease signing
- Real Estate Agencies: Provide professional tenant assessments to property owners
- Banks & Lenders: Assess tenant quality for property financing decisions
- Investment Funds: Evaluate tenant portfolios for acquisition due diligence
- Accountants: Generate comprehensive ratio analysis reports
- Financial Advisors: Assess client company financial health
- Business Consultants: Provide structured financial evaluations
- Credit Analysts: Evaluate borrower financial capacity
SpiritGenius calculates 41+ professional financial ratios using precise formulas across 6 major categories:
- Ressources propres: Capitaux propres + Provisions pour risques
- Ressources stables: Ressources propres + Dettes financières LT
- FRNG: Ressources stables - Actif immobilisé net
- BFR: (Stocks + Créances) - (Dettes fournisseurs + Dettes fiscales/sociales)
- Trésorerie nette: Disponibilités - Concours bancaires CT
- Surface financière: Ressources stables / Immobilisations nettes
- Indépendance financière: Capitaux propres / Total passif
- Couverture immobilisations, Capital d'exploitation, etc.
- Marge globale: (CA - Achats) / CA
- Valeur ajoutée: CA - Consommations externes
- EBE: VA - Charges de personnel - Impôts/taxes
- CAF: Résultat net + Dotations amortissements/provisions
- Charges personnel/VA, Impôts/VA, Charges financières/VA
- Taux marge bénéficiaire, Taux marge brute d'exploitation
- Marge brute autofinancement: CAF / CA
- ROE: Résultat net / Capitaux propres moyens
- Rentabilité économique: (Résultat + Charges financières) / Total actif
- Rentabilité financière: Résultat financier / Dettes financières
- Rentabilité brute/nette ressources stables et capital exploitation
- Taux variation CA: (CA_n - CA_n-1) / CA_n-1
- Taux variation VA: (VA_n - VA_n-1) / VA_n-1
- Taux variation résultat net: (RN_n - RN_n-1) / RN_n-1
- Taux variation capitaux propres: (CP_n - CP_n-1) / CP_n-1
- Capacité génération cash (Méthode 1): EBE - Variation BFR
- Capacité génération cash (Méthode 2): CAF - Variation BFR
- Capacité remboursement: Dettes financières / CAF
- Délai créances clients: (Créances clients / CA TTC) × 365
- Délai dettes fournisseurs: (Dettes fournisseurs / Achats TTC) × 365
- Loyer/CA: Annual rent / Revenue (rental capacity indicator)
- Loyer/EBE: Annual rent / EBITDA (cash flow coverage)
All ratios calculated for current year (N) and previous year (N-1) when data available.
SpiritGenius provides professional tenant solvency evaluation specifically designed for commercial real estate:
- 🟢 Risque faible: Strong financial health, favorable rental recommendation
- 🟡 Risque moyen: Mixed financial indicators, recommendation with conditions/guarantees
- 🔴 Risque élevé: Concerning financial situation, unfavorable rental recommendation
- 📈 Revenue Analysis: Turnover stability, growth trends, and market position
- 🏗️ Financial Structure: Equity strength, debt levels, and financial independence
- 💰 Rental Capacity: Loyer/CA and Loyer/EBE ratios for payment capacity assessment
- 💸 Cash Flow: CAF analysis and cash generation sustainability
- 📊 Profitability: Margin evolution and operational efficiency
- ⏰ Working Capital: BFR management and payment cycle optimization
- 🔄 Evolution Trends: Year-over-year performance indicators
- Payment Capacity: Rent as % of revenue and EBITDA
- Financial Stability: Multi-year trend analysis
- Operational Efficiency: Margin and cost structure evaluation
- Growth Sustainability: Revenue and profitability trends
- Risk Mitigation: Specific recommendations for rental agreements
spiritgenius/
├── main.py # 🚀 FastAPI application with CORS support
├── app.py # 🔄 4-step tenant evaluation pipeline orchestrator
├── clients.py # 🤖 AI client initialization (Gemini & Claude)
├── pdf_handler.py # 📄 Robust PDF download with retry logic
├── gemini_service.py # 🔍 Gemini 2.5 Flash - financial data extraction
├── claude_ratio_service.py # 🧮 Claude 4 - professional ratio calculation (41+ ratios)
├── claude_service.py # 📋 Claude 4 - final tenant solvency analysis
├── logger.py # 📝 Logger instance
├── logging_config.py # ⚙️ Comprehensive logging configuration
├── requirements.txt # 📦 Python dependencies
├── .env # 🔐 Environment variables (API keys)
├── railway.json # 🚂 Railway deployment configuration
└── README.md # 📖 Project documentation
- Backend: FastAPI 0.100.0 with Hypercorn ASGI server
- AI Models: Google Gemini 2.5 Flash + Anthropic Claude 4
- PDF Processing: aiohttp with async download and retry logic
- Deployment: Railway.app with Nixpacks builder
- Dependencies: python-dotenv, anthropic, google-genai, pydantic 2.0+