""" BI Storyteller Web Interface Professional HTTP server with REST API - Standard Library Only """ import json import os from http.server import HTTPServer, BaseHTTPRequestHandler from urllib.parse import urlparse, parse_qs import mimetypes from main import BIStoryteller class BIStoryteller_WebHandler(BaseHTTPRequestHandler): """HTTP request handler for BI Storyteller web interface""" def __init__(self, *args, **kwargs): self.bi = BIStoryteller() super().__init__(*args, **kwargs) def do_GET(self): """Handle GET requests""" if self.path == '/' or self.path == '/index.html': self.serve_main_page() elif self.path.startswith('/api/status'): self.get_status() else: self.send_error(404, "Page not found") def do_POST(self): """Handle POST requests""" content_length = int(self.headers.get('Content-Length', 0)) post_data = self.rfile.read(content_length) try: data = json.loads(post_data.decode('utf-8')) except: self.send_error(400, "Invalid JSON") return if self.path == '/api/set_api_key': result = self.bi.set_groq_api_key(data.get('api_key', '')) self._send_json_response(result) elif self.path == '/api/extract_variables': result = self.bi.extract_variables(data.get('business_problem', '')) self._send_json_response(result) elif self.path == '/api/generate_questionnaire': result = self.bi.generate_questionnaire( data.get('variables', []), data.get('business_problem', '') ) self._send_json_response(result) elif self.path == '/api/generate_data': result = self.bi.generate_sample_data( data.get('variables', []), data.get('sample_size', 1000) ) self._send_json_response(result) elif self.path == '/api/clean_data': result = self.bi.clean_data(data.get('data', [])) self._send_json_response(result) elif self.path == '/api/perform_eda': result = self.bi.perform_eda(data.get('data', [])) self._send_json_response(result) elif self.path == '/api/train_model': result = self.bi.train_predictive_model( data.get('data', []), data.get('algorithm', 'Random Forest') ) self._send_json_response(result) elif self.path == '/api/analyze_trends': result = self.bi.analyze_trends( data.get('data', []), data.get('time_period', 'Monthly') ) self._send_json_response(result) elif self.path == '/api/analyze_sentiment': result = self.bi.analyze_sentiment(data.get('data', [])) self._send_json_response(result) elif self.path == '/api/run_ab_test': result = self.bi.run_ab_test( data.get('data', []), data.get('test_variable', ''), data.get('success_metric', '') ) self._send_json_response(result) elif self.path == '/api/chat': result = self.bi.chat_with_data(data.get('question', '')) self._send_json_response(result) elif self.path == '/api/export': result = self.bi.export_results(data.get('filename')) self._send_json_response(result) elif self.path == '/api/import': result = self.bi.import_results(data.get('filename')) self._send_json_response(result) else: self.send_error(404, "API endpoint not found") def get_status(self): """Get current analysis status""" status = { "modules_completed": [], "current_step": 1, "total_steps": 12 } if self.bi.variables: status["modules_completed"].append("Variable Extraction") status["current_step"] = 2 if self.bi.questionnaire: status["modules_completed"].append("Questionnaire Generation") status["current_step"] = 3 if self.bi.sample_data: status["modules_completed"].append("Data Generation") status["current_step"] = 4 if self.bi.cleaned_data: status["modules_completed"].append("Data Cleaning") status["current_step"] = 5 if self.bi.eda_results: status["modules_completed"].append("EDA Analysis") status["current_step"] = 6 if self.bi.model_results: status["modules_completed"].append("Predictive Modeling") status["current_step"] = 7 if self.bi.trend_results: status["modules_completed"].append("Trend Analysis") status["current_step"] = 8 if self.bi.sentiment_results: status["modules_completed"].append("Sentiment Analysis") status["current_step"] = 9 if self.bi.ab_test_results: status["modules_completed"].append("A/B Testing") status["current_step"] = 10 if self.bi.chat_history: status["modules_completed"].append("Chat Interface") status["current_step"] = 11 self._send_json_response(status) def serve_main_page(self): """Serve the main HTML page""" html_content = """
Marketing Analysis Automation Platform
Generate survey questions based on extracted variables.
Remove outliers, handle missing values, and preprocess data.
Perform statistical analysis and generate insights.
Analyze customer feedback and sentiment patterns.