Spaces:
Sleeping
Sleeping
| import io | |
| from fastapi.responses import StreamingResponse | |
| from datetime import datetime | |
| from fastapi import APIRouter, HTTPException | |
| from app.core.data_provider import IndianDataProvider | |
| from app.core.swarms_client import create_indian_compliance_swarm | |
| from app.models.schemas import ( | |
| StockDataRequest, StockDataResponse, | |
| ComplianceAnalysisRequest, ComplianceAnalysisResponse, GenerateCSVRequest | |
| ) | |
| from app.utils.helpers import create_comprehensive_csv_data | |
| router = APIRouter() | |
| data_provider = IndianDataProvider() | |
| async def get_stock_data(request: StockDataRequest): | |
| """Fetch NSE stock data for a given symbol""" | |
| try: | |
| stock_data = data_provider.get_nse_stock_data(request.symbol) | |
| if not stock_data.get('success'): | |
| return StockDataResponse( | |
| success=False, | |
| error=stock_data.get('error', 'Unknown error') | |
| ) | |
| formatted_data = data_provider.format_stock_data_for_analysis(stock_data) | |
| return StockDataResponse( | |
| success=True, | |
| data=stock_data, | |
| formatted_data=formatted_data | |
| ) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Error fetching stock data: {str(e)}") | |
| async def run_compliance_analysis(request: ComplianceAnalysisRequest): | |
| """Run compliance analysis on financial data""" | |
| try: | |
| result = create_indian_compliance_swarm( | |
| request.financial_data, | |
| request.company_info | |
| ) | |
| if "error" in result: | |
| return ComplianceAnalysisResponse( | |
| success=False, | |
| error=result["error"] | |
| ) | |
| return ComplianceAnalysisResponse( | |
| success=True, | |
| result=result | |
| ) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Error running compliance analysis: {str(e)}") | |
| # Update the generate-csv endpoint | |
| async def generate_csv_report(request: GenerateCSVRequest): | |
| """Generate CSV report for compliance analysis and return as downloadable file""" | |
| try: | |
| df = create_comprehensive_csv_data( | |
| request.data_source, | |
| request.company_info, | |
| request.accounting_standards, | |
| request.regulatory_frameworks, | |
| request.result | |
| ) | |
| # Create a stream for the CSV data | |
| stream = io.StringIO() | |
| df.to_csv(stream, index=False) | |
| # Create a response that will download the CSV file | |
| response = StreamingResponse( | |
| iter([stream.getvalue()]), | |
| media_type="text/csv", | |
| headers={ | |
| "Content-Disposition": f"attachment; filename=compliance_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv" | |
| } | |
| ) | |
| return response | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Error generating CSV: {str(e)}") | |
| async def get_popular_stocks(): | |
| """Get list of popular NSE stocks""" | |
| popular_stocks = [ | |
| 'ADANIENT', 'ADANIPORTS', 'APOLLOHOSP', 'ASIANPAINT', | |
| 'AXISBANK', 'BAJAJ-AUTO', 'BAJFINANCE', 'BAJAJFINSV', | |
| 'BEL', 'BHARTIARTL', 'CIPL', 'COALINDIA', 'DRREDDY', | |
| 'EICHERMOT', 'GRASIM', 'HCLTECH', 'HDFCBANK', 'HDFCLIFE', | |
| 'HINDALCO', 'HINDUNILVR', 'ICICIBANK', 'INFY', 'ITC', | |
| 'JIOFIN', 'JSWSTEEL', 'KOTAKBANK', 'LT', 'M&M', 'MARUTI', | |
| 'MAXHEALTH', 'NESTLEIND', 'NTPC', 'ONGC', 'POWERGRID', | |
| 'RELIANCE', 'SBILIFE', 'SBIN', 'SHRIRAMFIN', 'SUNPHARMA', | |
| 'TATACONSUM', 'TCS', 'TATAMOTORS', 'TATASTEEL', 'TECHM', | |
| 'TITAN', 'TRENT', 'ULTRACEMCO', 'WIPRO', 'INDIGO','ETERNAL', | |
| 'HINDPETRO', 'TATAPOWER', 'BAJAJHLDNG', 'HEROMOTOCO', 'INDUSINDBK' | |
| ] | |
| return {"stocks": popular_stocks} |