import joblib import pandas as pd import numpy as np import gradio as gr from tensorflow.keras.models import load_model from sklearn.preprocessing import MinMaxScaler import tensorflow as tf mm = joblib.load("/home/user/app/mm_X_scaler.pkl") mm_y = joblib.load("/home/user/app/mm_y_scaler.pkl") # Load the trained model model = load_model('/home/user/app/model_superior_Superior.h5') # Update with your model path def preprocess_input(csv_file): """ Preprocess the input CSV file containing vibrational data """ try: # Read the CSV file df = pd.read_csv(csv_file.name,index_col=0) # Preprocess the data data = df.values data_scaled = mm.fit_transform(data) data_reshaped = data_scaled.reshape(data_scaled.shape[0], 1, data_scaled.shape[1]) # Make predictions predictions = model.predict(data_reshaped) predictions = mm_y.inverse_transform(predictions) # Create results DataFrame results_df = pd.DataFrame({ 'Timestamp': df['AssignedDate'] if 'AssignedDate' in df.columns else range(len(predictions)), 'Prediction': predictions.flatten(), 'Maintenance Type': ['Predictive' if p >= 0.5 else 'Preventive' for p in predictions.flatten()] }) # Save results to CSV results_path = "predictions.csv" results_df.to_csv(results_path, index=True) # Calculate summary statistics total_samples = len(predictions) predictive_count = sum(predictions.flatten() >= 0.5) preventive_count = total_samples - predictive_count summary = f""" Analysis Results: ---------------- Total Samples: {total_samples} Predictive Maintenance Required: {predictive_count} ({predictive_count/total_samples*100:.1f}%) Preventive Maintenance Required: {preventive_count} ({preventive_count/total_samples*100:.1f}%) Detailed results have been saved to 'predictions.csv' """ return summary except Exception as e: return f"Error processing file: {str(e)}" # Create Gradio interface iface = gr.Interface( fn=preprocess_input, inputs=[ gr.File( label="Upload Vibrational Data CSV", type="filepath", file_types=[".csv"] ) ], outputs=gr.Textbox( label="Prediction Results", lines=10 ), title="CNC Machine Maintenance Predictor", description=""" Upload a CSV file containing CNC machine vibrational data to predict maintenance types. The CSV should contain the following columns: PC1, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC441, PC442. The model will classify each record as either Predictive or Preventive maintenance. """, examples=[ ["/home/user/app/X.csv"] # Add example data file if available ] ) # Launch the interface if __name__ == "__main__": iface.launch(share=True)