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76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
"""
SAP-RPT-1-OSS Playground Utilities

Functions for handling dataset uploads, previews, training, and results export.
"""

import pandas as pd
import numpy as np
from pathlib import Path
from typing import Tuple, Optional, Dict, Any
import tempfile
import os


def load_dataset(file_path: str) -> Tuple[pd.DataFrame, Optional[str]]:
    """
    Load dataset from file (CSV, Parquet, or JSON).
    
    Args:
        file_path: Path to the dataset file
        
    Returns:
        Tuple of (DataFrame, error_message)
    """
    try:
        file_ext = Path(file_path).suffix.lower()
        
        if file_ext == '.csv':
            df = pd.read_csv(file_path)
        elif file_ext == '.parquet':
            df = pd.read_parquet(file_path)
        elif file_ext in ['.json', '.jsonl']:
            df = pd.read_json(file_path, lines=(file_ext == '.jsonl'))
        else:
            return None, f"Unsupported file format: {file_ext}"
        
        return df, None
    except Exception as e:
        return None, f"Error loading file: {str(e)}"


def detect_task_type(filename: str) -> str:
    """
    Detect task type from filename.
    
    Args:
        filename: Name of the uploaded file
        
    Returns:
        'classification' or 'regression'
    """
    filename_lower = filename.lower()
    if 'classification' in filename_lower:
        return 'classification'
    elif 'regression' in filename_lower:
        return 'regression'
    return 'classification'  # Default


def get_dataset_info(df: pd.DataFrame) -> Dict[str, Any]:
    """
    Get information about the dataset.
    
    Args:
        df: DataFrame to analyze
        
    Returns:
        Dictionary with dataset information
    """
    info = {
        'num_rows': len(df),
        'num_columns': len(df.columns),
        'columns': list(df.columns),
        'numeric_columns': list(df.select_dtypes(include=[np.number]).columns),
        'categorical_columns': list(df.select_dtypes(include=['object', 'category']).columns),
        'missing_values': df.isnull().sum().to_dict(),
        'dtypes': df.dtypes.astype(str).to_dict()
    }
    return info


def auto_select_target_column(df: pd.DataFrame, task_type: str) -> Optional[str]:
    """
    Auto-select target column (defaults to last column).
    
    Args:
        df: DataFrame
        task_type: 'classification' or 'regression'
        
    Returns:
        Column name or None
    """
    if len(df.columns) == 0:
        return None
    
    # Default to last column
    target = df.columns[-1]
    
    # If task type is regression, prefer numeric columns
    if task_type == 'regression':
        numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
        if numeric_cols:
            # Prefer last numeric column
            for col in reversed(numeric_cols):
                if col == target or df[col].dtype in [np.float64, np.int64]:
                    return col
    
    return target


def detect_task_type_from_column(df: pd.DataFrame, target_column: str) -> str:
    """
    Detect task type from target column's data type.
    
    Args:
        df: DataFrame
        target_column: Name of target column
        
    Returns:
        'classification' or 'regression'
    """
    if target_column not in df.columns:
        return 'classification'  # Default
    
    target_series = df[target_column]
    
    # Check if numeric
    if not pd.api.types.is_numeric_dtype(target_series):
        return 'classification'  # Non-numeric = classification
    
    # Check if integer-like
    try:
        unique_values = target_series.dropna().nunique()
        if unique_values <= 20:  # Few unique values = likely classification
            # Check if values are integer-like
            sample = target_series.dropna().head(100)
            int_values = sample.astype(int)
            float_values = sample.astype(float)
            if (int_values == float_values).all():
                return 'classification'
        
        # Many unique numeric values = regression
        return 'regression'
    except:
        return 'regression'  # Default to regression for numeric


def prepare_train_test_split(
    df: pd.DataFrame,
    target_column: str,
    test_size: float = 0.2
) -> Tuple[pd.DataFrame, pd.Series, pd.DataFrame, pd.Series]:
    """
    Prepare train/test split.
    
    Args:
        df: Full dataset
        target_column: Name of target column
        test_size: Proportion of test set (0.1 to 0.5)
        
    Returns:
        Tuple of (X_train, y_train, X_test, y_test)
    """
    if target_column not in df.columns:
        raise ValueError(f"Target column '{target_column}' not found in dataset")
    
    # Separate features and target
    X = df.drop(columns=[target_column])
    y = df[target_column]
    
    # Handle missing values in target
    valid_mask = ~y.isnull()
    X = X[valid_mask]
    y = y[valid_mask]
    
    # Calculate split index
    n_total = len(X)
    n_test = int(n_total * test_size)
    n_train = n_total - n_test
    
    if n_test == 0:
        n_test = 1
        n_train = n_total - 1
    
    # Split
    X_train = X.iloc[:n_train]
    y_train = y.iloc[:n_train]
    X_test = X.iloc[n_train:n_train + n_test]
    y_test = y.iloc[n_train:n_train + n_test]
    
    return X_train, y_train, X_test, y_test


def preprocess_data(
    df: pd.DataFrame,
    handle_missing: str = 'mean',
    normalize: bool = False
) -> pd.DataFrame:
    """
    Preprocess dataset.
    
    Args:
        df: DataFrame to preprocess
        handle_missing: How to handle missing values ('mean', 'median', 'drop', 'zero')
        normalize: Whether to normalize numeric columns
        
    Returns:
        Preprocessed DataFrame
    """
    df_processed = df.copy()
    
    # Handle missing values
    numeric_cols = df_processed.select_dtypes(include=[np.number]).columns
    
    if handle_missing == 'mean':
        df_processed[numeric_cols] = df_processed[numeric_cols].fillna(
            df_processed[numeric_cols].mean()
        )
    elif handle_missing == 'median':
        df_processed[numeric_cols] = df_processed[numeric_cols].fillna(
            df_processed[numeric_cols].median()
        )
    elif handle_missing == 'zero':
        df_processed[numeric_cols] = df_processed[numeric_cols].fillna(0)
    elif handle_missing == 'drop':
        df_processed = df_processed.dropna(subset=numeric_cols)
    
    # Normalize numeric columns
    if normalize:
        from sklearn.preprocessing import StandardScaler
        scaler = StandardScaler()
        df_processed[numeric_cols] = scaler.fit_transform(df_processed[numeric_cols])
    
    return df_processed


def export_results(
    X_test: pd.DataFrame,
    y_test: pd.Series,
    predictions: np.ndarray,
    task_type: str,
    filename_prefix: str = "results"
) -> str:
    """
    Export results to CSV file.
    
    Args:
        X_test: Test features
        y_test: True target values
        predictions: Model predictions
        task_type: 'classification' or 'regression'
        filename_prefix: Prefix for output filename
        
    Returns:
        Path to exported CSV file
    """
    # Create results DataFrame
    results_df = X_test.copy()
    results_df['true_value'] = y_test.values
    
    if task_type == 'classification':
        results_df['predicted_class'] = predictions
    else:
        # Regression - format with comma for decimal separator
        results_df['predicted_value'] = predictions
    
    # Save to temporary file
    temp_dir = tempfile.gettempdir()
    output_path = os.path.join(temp_dir, f"{filename_prefix}_results.csv")
    
    results_df.to_csv(output_path, index=False)
    
    return output_path


def check_embedding_server() -> Tuple[bool, str]:
    """
    Check if embedding server is running.
    
    Returns:
        Tuple of (is_running, message)
    """
    # First check if packages are available (but don't fail if import path is different)
    try:
        import zmq
    except ImportError:
        return False, "pyzmq package not installed. Install with: pip install pyzmq"
    
    # Try to check if server is running (don't check package installation here)
    try:
        context = zmq.Context()
        socket = context.socket(zmq.REQ)
        socket.setsockopt(zmq.LINGER, 0)
        socket.settimeout(1000)  # 1 second timeout
        socket.connect("tcp://localhost:5555")
        socket.send_string("ping")
        
        # Try to receive with timeout
        poller = zmq.Poller()
        poller.register(socket, zmq.POLLIN)
        if poller.poll(1000):  # 1 second timeout
            response = socket.recv_string()
            socket.close()
            context.term()
            return True, "Embedding server is running and responding"
        else:
            socket.close()
            context.term()
            return False, "Embedding server not responding on port 5555"
    except zmq.ZMQError as e:
        if "Connection refused" in str(e) or "No such file or directory" in str(e):
            return False, "Embedding server is not running"
        return False, f"Connection error: {str(e)}"
    except Exception as e:
        return False, f"Error checking embedding server: {str(e)}"


def is_sap_rpt_oss_installed() -> bool:
    """Check if sap-rpt-oss package is installed."""
    try:
        import sap_rpt_oss
        return True
    except ImportError:
        try:
            # Try alternative import paths
            from sap_rpt_oss import SAP_RPT_OSS_Classifier
            return True
        except ImportError:
            return False


def start_embedding_server(gpu_idx: Optional[int] = None) -> Tuple[bool, str]:
    """
    Start the embedding server automatically.
    
    Args:
        gpu_idx: GPU index to use (None for CPU)
        
    Returns:
        Tuple of (success, message)
    """
    # Check if package is installed
    if not is_sap_rpt_oss_installed():
        return False, "sap-rpt-oss package not found. Install with: pip install git+https://github.com/SAP-samples/sap-rpt-1-oss"
    
    # Check if server is already running
    is_running, _ = check_embedding_server()
    if is_running:
        return True, "Embedding server is already running"
    
    try:
        # Try multiple import paths
        start_func = None
        import_paths = [
            "sap_rpt_oss.scripts.start_embedding_server",
            "sap_rpt_oss.start_embedding_server",
            "sap_rpt_oss.data.tokenizer",  # Sometimes server is in tokenizer
        ]
        
        for import_path in import_paths:
            try:
                module = __import__(import_path, fromlist=['start_embedding_server'])
                if hasattr(module, 'start_embedding_server'):
                    start_func = getattr(module, 'start_embedding_server')
                    break
            except (ImportError, AttributeError):
                continue
        
        if start_func is None:
            # Try using threading approach - the server might start automatically when model is used
            # For now, just inform user that server will start when needed
            return False, "Embedding server will start automatically when the model makes predictions. No manual start needed."
        
        # Use threading to start server in background (simpler than subprocess)
        import threading
        
        def run_server():
            try:
                start_func(
                    sentence_embedding_model_name="sentence-transformers/all-MiniLM-L6-v2",
                    gpu_idx=gpu_idx
                )
            except Exception as e:
                # Server might block, that's okay
                pass
        
        # Start server in background thread
        server_thread = threading.Thread(target=run_server, daemon=True)
        server_thread.start()
        
        # Wait a moment for server to start
        import time
        time.sleep(3)
        
        # Check if it started
        is_running, message = check_embedding_server()
        if is_running:
            return True, "Embedding server started successfully in background"
        else:
            # Server might take longer to start, or it starts on-demand
            return False, "Server thread started. The embedding server will be available when the model needs it (starts on-demand during predictions)."
            
    except Exception as e:
        # If we can't start it manually, that's okay - SAP-RPT-OSS might start it automatically
        return False, f"Manual start not available: {str(e)}. The embedding server will start automatically when the model makes predictions."


def ensure_embedding_server_running() -> Tuple[bool, str]:
    """
    Ensure embedding server is running, start it if not.
    Note: SAP-RPT-OSS may start the server automatically when needed.
    
    Returns:
        Tuple of (is_running, message)
    """
    is_running, message = check_embedding_server()
    if is_running:
        return True, message
    
    # Try to start it (but don't fail if we can't - server may start on-demand)
    success, start_message = start_embedding_server(None)
    if success:
        return True, f"Auto-started: {start_message}"
    else:
        # Server not running, but SAP-RPT-OSS may start it automatically when model is used
        # This is not a fatal error - the model will attempt to start it when needed
        return False, f"Server not currently running. {start_message}"