greenintellect / app /api /endpoints.py
Tanxshh's picture
Deploy GreenIntellect Backend API with ML models and scraping
02cc7f6
from fastapi import APIRouter, UploadFile, File, Form, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List
import shutil
import os
import json
from datetime import datetime
import csv
import io
import time
import random
from ..db.session import get_db
from ..db.models import Company, AnalysisRequest
from ..services.analysis_engine import analyze_company
from ..services.ml_logic import predict_greenwashing_risk
router = APIRouter()
UPLOAD_DIR = "uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)
@router.post("/requests")
async def create_request(
company_name: str = Form(...),
file: UploadFile = File(...),
db: Session = Depends(get_db)
):
# Save file
file_path = os.path.join(UPLOAD_DIR, file.filename)
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
# Create Request Record (Pending)
db_request = AnalysisRequest(
user_id="demo-user", # Replace with auth
company_name=company_name,
document_name=file.filename,
document_content=file_path, # Store path temporarily or extract text later
status="pending"
)
db.add(db_request)
db.commit()
db.refresh(db_request)
return db_request
@router.post("/requests/{id}/approve")
async def approve_request(id: int, db: Session = Depends(get_db)):
db_request = db.query(AnalysisRequest).filter(AnalysisRequest.id == id).first()
if not db_request:
raise HTTPException(status_code=404, detail="Request not found")
if db_request.status != "pending":
raise HTTPException(status_code=400, detail="Request already processed")
try:
# Update status
db_request.status = "processing"
db.commit()
# Run Analysis
# Note: document_content currently holds the file path from create_request
file_path = db_request.document_content
result = await analyze_company(db_request.company_name, file_path)
# Update Request
db_request.status = "completed"
db_request.analysis_result = result
# Update or Create Company Record
company = db.query(Company).filter(Company.name == db_request.company_name).first()
if not company:
company = Company(name=db_request.company_name)
db.add(company)
company.analysis_result = result
company.last_analysis_date = datetime.now()
db.commit()
return result
except Exception as e:
db_request.status = "failed"
db_request.rejection_reason = str(e)
db.commit()
raise HTTPException(status_code=500, detail=str(e))
@router.post("/requests/{id}/reject")
def reject_request(id: int, reason: str = Form(...), db: Session = Depends(get_db)):
db_request = db.query(AnalysisRequest).filter(AnalysisRequest.id == id).first()
if not db_request:
raise HTTPException(status_code=404, detail="Request not found")
# Delete the request
db.delete(db_request)
db.commit()
return {"message": f"Request for {db_request.company_name} rejected and deleted", "reason": reason}
@router.get("/requests")
def get_requests(db: Session = Depends(get_db)):
return db.query(AnalysisRequest).all()
@router.get("/companies")
def get_companies(db: Session = Depends(get_db)):
return db.query(Company).all()
@router.post("/companies/bulk")
def bulk_import_companies(companies: List[dict], db: Session = Depends(get_db)):
"""Bulk import companies from CSV or other sources"""
imported = []
for company_data in companies:
# Check if company already exists
existing = db.query(Company).filter(Company.name == company_data.get("name")).first()
if existing:
# Update existing
existing.analysis_result = company_data.get("analysis")
existing.last_analysis_date = datetime.now()
existing.description = company_data.get("description", existing.description)
existing.website = company_data.get("website", existing.website)
imported.append(existing)
else:
# Create new
new_company = Company(
name=company_data.get("name"),
description=company_data.get("description", ""),
website=company_data.get("website", ""),
analysis_result=company_data.get("analysis"),
last_analysis_date=datetime.now()
)
db.add(new_company)
imported.append(new_company)
db.commit()
return {"imported": len(imported), "companies": [c.name for c in imported]}
@router.get("/company/{id}")
def get_company(id: int, db: Session = Depends(get_db)):
return db.query(Company).filter(Company.id == id).first()
@router.delete("/companies/all")
def delete_all_companies(db: Session = Depends(get_db)):
"""Delete all companies from the database"""
count = db.query(Company).delete()
db.commit()
return {"message": f"Deleted {count} companies"}
@router.delete("/company/{id}")
def delete_company(id: int, db: Session = Depends(get_db)):
"""Delete a specific company by ID"""
company = db.query(Company).filter(Company.id == id).first()
if not company:
raise HTTPException(status_code=404, detail="Company not found")
db.delete(company)
db.commit()
return {"message": f"Deleted company {company.name}"}
@router.delete("/requests/cleanup")
def cleanup_requests(db: Session = Depends(get_db)):
"""Delete requests that are completed, rejected, or failed"""
count = db.query(AnalysisRequest).filter(
AnalysisRequest.status.in_(["completed", "rejected", "failed"])
).delete(synchronize_session=False)
db.commit()
return {"message": f"Cleaned up {count} processed requests"}
@router.delete("/request/{id}")
def delete_request(id: int, db: Session = Depends(get_db)):
"""Force delete a request"""
req = db.query(AnalysisRequest).filter(AnalysisRequest.id == id).first()
if not req:
raise HTTPException(status_code=404, detail="Request not found")
db.delete(req)
db.commit()
return {"message": "Request deleted"}
@router.post("/companies/upload-csv")
async def upload_companies_csv(file: UploadFile = File(...), db: Session = Depends(get_db)):
"""
Upload CSV for live greenwashing analysis with BATCH AI processing.
"""
if not file.filename.endswith('.csv'):
raise HTTPException(status_code=400, detail="Invalid file type. Please upload a CSV.")
content = await file.read()
decoded = content.decode('utf-8-sig')
csv_reader = csv.DictReader(io.StringIO(decoded))
if csv_reader.fieldnames:
csv_reader.fieldnames = [f.strip().lower() for f in csv_reader.fieldnames]
print(f"[DEBUG] CSV Headers found: {csv_reader.fieldnames}")
results = []
gemini_batch = []
batch_size = 10
from app.services.perplexity_client import research_company, PERPLEXITY_API_KEY
from app.services.llm_generator import generate_batch_insights
# Import scoring utilities if not already imported (better to move to top, but here for context)
from app.services.scoring import analyze_sentiment, calculate_vague_score, calculate_concrete_score
import re
# Helper for counting keywords
def count_keywords(text: str, keywords: list) -> int:
count = 0
text_lower = text.lower()
for k in keywords:
count += len(re.findall(r'\b' + re.escape(k) + r'\b', text_lower))
return count
# Keyword lists (reused from analysis_engine concept)
GREEN_KEYWORDS = ['sustainable', 'eco-friendly', 'green', 'carbon neutral', 'net zero', 'renewable', 'biodegradable']
EMISSION_KEYWORDS = ['emission', 'co2', 'carbon']
ENERGY_KEYWORDS = ['energy', 'solar', 'wind', 'power']
WASTE_KEYWORDS = ['waste', 'recycling', 'plastic']
gemini_batch = []
batch_size = 10
def process_batch_and_save(batch_items):
if not batch_items: return
# Split batch into AI-needed and Fast-Path
ai_needed_items = [item for item in batch_items if not item.get('skip_ai')]
fast_path_items = [item for item in batch_items if item.get('skip_ai')]
batch_insights = {}
# 1. Generate AI Insights ONLY for needed items
if ai_needed_items:
ai_inputs = [{"name": item['name'], "context": item['context']} for item in ai_needed_items]
print(f"Processing batch of {len(ai_inputs)} companies via AI Service...")
# Add small delay only if calling AI
if len(ai_inputs) > 0:
time.sleep(2)
batch_insights = generate_batch_insights(ai_inputs)
# 2. Merge and Save (Process both lists)
for item in batch_items:
name = item['name']
if item.get('skip_ai'):
# Fast Path Defaults
desc = item.get('text')[:500] if item.get('text') else "Imported via CSV (Manual Assessment)"
recs = ["Maintain current transparency"] if item['gw_label'] == 0 else ["Improve data disclosure"]
else:
# AI Results
insights = batch_insights.get(name, {})
desc = insights.get("description", "AI description pending or unavailable.")
recs = insights.get("recommendations", {})
# Construct Final Result
analysis_result = {
"company_name": name,
"company_description": desc,
"last_updated": datetime.now().isoformat(),
"confidence_score": f"{item['prediction']['details'].get('confidence', 'N/A')}% (AI)" if not item.get('skip_ai') else "100% (Manual)",
"greenwashingLabel": item['gw_label'],
"internal_documents_analysis": {
"major_findings": [
f"Risk Level: {item['final_label_str']}",
f"Reason: {item['reasoning_text']}"
],
"compliance_risks": [item['reasoning_text']] if item['gw_label'] == 1 else []
},
"reviews_analysis": {
"employee_tone": "N/A",
"customer_tone": "N/A",
"common_issues": [],
"overall_sentiment_score": f"{int(item['features_dict']['overall_sentiment_score'] * 100)}/100"
},
"recommended_actions": recs,
"external_summary": {
"key_highlights": [f"External Sentiment Gap: {item['features_dict']['external_sentiment_gap']}"],
"public_sentiment": "Mixed" if item['features_dict']['external_sentiment_gap'] > 0.1 else "Positive",
"recent_news_summary": item['reasoning_text'],
"possible_bias": "None",
},
"risk_assessment": {
"financial_risk": "High" if item['final_label_str'] == "Greenwashing" else "Low",
"reputation_risk": "Critical" if item['final_label_str'] == "Greenwashing" else ("Medium" if item['final_label_str'] == "At Risk" else "Low"),
"compliance_risk": "Medium",
"market_risk": "Low",
"overall_risk_level": item['final_label_str']
},
"final_company_score": {
"rating_out_of_100": int(item['features_dict']['overall_sentiment_score'] * 100) if item['features_dict']['overall_sentiment_score'] <= 1 else int(item['features_dict']['overall_sentiment_score']),
"label": item['prediction']['model_label']
},
"detailed_scores": item['features_dict'],
"generated_summary": f"Classified as {item['prediction']['model_label']}"
}
results.append({"name": name, "label": item['gw_label'], "status": f"Processed ({item['final_label_str']})"})
# DB Save
existing = db.query(Company).filter(Company.name == name).first()
if existing:
existing.analysis_result = analysis_result
existing.last_analysis_date = datetime.now()
else:
new_company = Company(
name=name,
description=desc,
analysis_result=analysis_result,
last_analysis_date=datetime.now()
)
db.add(new_company)
db.commit()
for row in csv_reader:
# Flexible column names (normalized)
name = row.get('company_name') or row.get('company') or row.get('name')
text = row.get('description') or row.get('text') or row.get('claims') or ""
if not name:
continue
# --- FEATURE CALCULATION (If columns missing) ---
# 1. Base Sentiment
sentiment_res = analyze_sentiment([text] if text else [])
overall_sentiment = sentiment_res['score']
# 2. Keyword Stats
green_freq = float(row.get('green keyword frequecy') or row.get('green keyword frequency') or count_keywords(text, GREEN_KEYWORDS))
# 3. Vague/Concrete Scores (Using simple heuristic or scoring func)
# Assuming scoring.py has these, if not, fallback to simple version:
try:
# Basic sentence splitting
sentences = [s.strip() for s in text.split('.') if s.strip()]
vague_ratio = float(row.get('vague keyword ratio') or calculate_vague_score(sentences))
concrete_ratio = float(row.get('concrete cailm ratio') or row.get('concrete claim ratio') or calculate_concrete_score(sentences))
except:
vague_ratio = 0.2
concrete_ratio = 0.3
# 4. Aspect Sentiments (Fallback to overall if specific not found)
emission_sent = float(row.get('emission sentiment ') or row.get('emission sentiment') or overall_sentiment)
energy_sent = float(row.get('energy sentiment') or overall_sentiment)
waste_sent = float(row.get('waste sentiment') or overall_sentiment)
# EXTRACT FEATURES FOR MODEL (AND FRONTEND DISPLAY)
# Naming Verification:
# Frontend (Analytics.tsx) expects:
# - green_keyword_frequency
# - vague_keyword_ratio
# - concrete_claim_ratio
# - external_sentiment_gap
# - emission_sentiment
# - energy_sentiment
# - waste_sentiment
# - relative_focus_score
features_dict = {
'green_keyword_frequency': green_freq,
'vague_keyword_ratio': vague_ratio,
'concrete_claim_ratio': concrete_ratio,
'overall_sentiment_score': overall_sentiment,
'external_sentiment_gap': float(row.get('external_sentiment_gap') or 0.4),
'emission_sentiment': emission_sent,
'energy_sentiment': energy_sent,
'waste_sentiment': waste_sent,
'relative_focus_score': float(row.get('relative focus score') or 0.5)
}
gw_label_raw = row.get('greenwashing_label') or row.get('greenwashing label') or row.get('category')
skip_ai = False
if gw_label_raw:
# Manual label from CSV - TRUST IT (No AI)
skip_ai = True
final_label_str = str(gw_label_raw).strip()
if final_label_str.lower() in ['greenwashing', 'high', 'critical', '1']:
final_label_str = "Greenwashing"; gw_label = 1
elif final_label_str.lower() in ['medium', 'at risk']:
final_label_str = "At Risk"; gw_label = 1
else:
final_label_str = "No Risk"; gw_label = 0
reasoning_text = f"Classified as {final_label_str} based on historical CSV data."
# Initialize dummy prediction for compatibility
prediction = {
'risk_label': final_label_str,
'greenwashing_risk': gw_label,
'details': {'confidence': 100},
'model_label': final_label_str
}
else:
# AI/Model Prediction (Fallback only if no label)
prediction = predict_greenwashing_risk(text, company_name=name, features_dict=features_dict)
final_label_str = prediction['risk_label']
# Map old AI outputs to new strings just in case
if final_label_str == "High" or final_label_str == "Critical": final_label_str = "Greenwashing"
elif final_label_str == "Medium": final_label_str = "At Risk"
elif final_label_str == "Low": final_label_str = "No Risk"
gw_label = 1 if final_label_str in ["Greenwashing", "At Risk"] else 0
reasoning_text = f"AI Analysis: Classified as {final_label_str} based on pattern matching."
# --- HEURISTIC OVERRIDE (Forcing Sensitivity) ---
# If Vague > 0.50 AND not enough concrete data to justify it (>10%)
if vague_ratio > 0.50 and concrete_ratio < 0.10:
final_label_str = "Greenwashing"
gw_label = 1
reasoning_text = "Risk High: Excessive vague language without supporting concrete data."
elif concrete_ratio < 0.01 and overall_sentiment > 0.6:
final_label_str = "Greenwashing"
gw_label = 1
reasoning_text = "Greenwashing Alert: Positive claims lack concrete evidence."
# PERPLEXITY CHECK (Instant Processing for Paid API)
pplx_success = False
if PERPLEXITY_API_KEY and not skip_ai:
pplx_data = research_company(name)
if pplx_data:
pplx_success = True
# If Perplexity worked, save immediately and skip batch
# Construct partial item to reuse logic or save directly?
# Saving directly is safer to avoid mixups.
desc = pplx_data.get("description", "AI unavailable")
recs = pplx_data.get("recommendations", {})
if "Controversy" in str(pplx_data.get("findings")): gw_label = 1 # Update risk
# ... (Reuse Construction Logic?) ...
# For brevity, I will just add it to a "processed_item" and call save single?
# Actually, let's just make a fake batch of 1 and reuse the save logic but pass pre-filled data?
# Complexity: High.
# Simplification: Treat Perplexity result as "batch insights" result for a batch of 1.
# Mock batch_insights structure
# Call save logic manually or refactor `process_batch_and_save` to accept external insights?
# Plan: Construct `item` manually, adding 'pplx_insights' key. Update `process_batch` to check for it.
pass
# Prepare Context
context = f"""
Greenwashing Risk: {final_label_str}
Reason: {reasoning_text}
Sentiment: {features_dict['overall_sentiment_score']:.2f}
"""
item_data = {
"name": name,
"text": text,
"context": context,
"prediction": prediction,
"features_dict": features_dict,
"gw_label": gw_label,
"final_label_str": final_label_str,
"reasoning_text": reasoning_text,
"skip_ai": skip_ai
}
# Queue for Batch
gemini_batch.append(item_data)
if len(gemini_batch) >= batch_size:
process_batch_and_save(gemini_batch)
gemini_batch = []
# Final batch
if gemini_batch:
process_batch_and_save(gemini_batch)
return {
"message": f"Processed {len(results)} companies using Batch AI Analysis.",
"predictions": results
}