bhagwandas commited on
Commit
5a9c5f9
Β·
verified Β·
1 Parent(s): 5f49e69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -44
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py - FactoryGPT 5.0: Predictive Maintenance with Technical Expert GPT
2
 
3
  import streamlit as st
4
  import pandas as pd
@@ -7,14 +7,14 @@ from sentence_transformers import SentenceTransformer
7
  from transformers import pipeline
8
  from sklearn.ensemble import IsolationForest
9
 
10
- # Page setup
11
  st.set_page_config(
12
- page_title="FactoryGPT 5.0 – Predictive Maintenance Expert",
13
  page_icon="🧠",
14
  layout="wide"
15
  )
16
 
17
- # Dark mode CSS
18
  st.markdown("""
19
  <style>
20
  html, body, [class*="css"] {
@@ -36,28 +36,28 @@ st.markdown("""
36
  # Header
37
  st.markdown("""
38
  <div style='text-align: center;'>
39
- <h1 style='color: #58a6ff;'>🏭 FactoryGPT 5.0 – Predictive Maintenance Assistant</h1>
40
- <p style='color: #bbb;'>Sensor-Driven Diagnostics | Role-Based Expert Assistant | Industry 5.0 Ready</p>
41
  <hr style='border-top: 2px solid #888;'>
42
  </div>
43
  """, unsafe_allow_html=True)
44
 
45
- # Load NLP models
46
  EMBED_MODEL = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
47
  GEN_MODEL = pipeline('text2text-generation', model='google/flan-t5-base')
48
 
49
- # Upload data
50
- uploaded_file = st.sidebar.file_uploader("πŸ“‚ Upload your sensor CSV", type=["csv"])
51
 
52
  if uploaded_file:
53
  df = pd.read_csv(uploaded_file)
54
  numeric_cols = df.select_dtypes(include=np.number).columns.tolist()
55
- st.success("βœ… Sensor log loaded!")
56
 
57
- st.markdown("### πŸ“Š Sensor Data Preview")
58
  st.dataframe(df.head(), use_container_width=True)
59
 
60
- # Prepare vector chunks
61
  def convert_to_chunks(df):
62
  return [f"[Entry {i}] " + ", ".join([f"{col}: {row[col]:.2f}" for col in numeric_cols]) for i, row in df.iterrows()]
63
 
@@ -67,61 +67,69 @@ if uploaded_file:
67
  st.session_state.chunks = chunks
68
  st.session_state.embeddings = embeddings
69
 
70
- # Predict machine status
71
- st.markdown("### πŸ” Equipment Status Prediction")
72
  iso = IsolationForest(contamination=0.02)
73
  labels = iso.fit_predict(df[numeric_cols])
74
- df['status'] = ['❌ Faulty' if x == -1 else 'βœ… OK' for x in labels]
75
- df['maintenance_flag'] = ['πŸ”§ Action Required' if x == -1 else '🟒 Normal' for x in labels]
76
  st.dataframe(df[['status', 'maintenance_flag'] + numeric_cols].head(), use_container_width=True)
77
 
78
- # Role-based assistant
79
- st.markdown("### πŸ’¬ Role-Based Technical Assistant")
80
  roles = {
81
- "Operator": (
82
- "You are a senior machine operator. Based on sensor logs, identify abnormal conditions in real-time operation. "
83
- "Explain deviations clearly and suggest next steps to the floor team."
84
- ),
85
- "Maintenance": (
86
- "You are a certified maintenance technician. Analyze anomalous sensor readings, reference maintenance schedules, "
87
- "and recommend immediate actions or predictive interventions with justification."
88
- ),
89
- "Engineer": (
90
- "You are a reliability and control systems engineer. Provide a detailed root cause analysis from the sensor logs, "
91
- "explain what failure modes may be emerging (e.g., torque drift, PID lag), and propose technical mitigations."
92
- )
 
 
 
 
 
 
 
 
 
93
  }
94
 
95
- role = st.selectbox("πŸ‘€ Select your role for technical insights", list(roles.keys()))
96
 
97
  if 'chat_history' not in st.session_state:
98
  st.session_state.chat_history = []
99
 
100
- user_input = st.text_input("🧠 Ask FactoryGPT a technical question about the machine behavior")
101
 
102
  if user_input:
103
- # Retrieve relevant context from logs
104
  query_vec = EMBED_MODEL.encode([user_input])[0]
105
  sims = np.dot(st.session_state.embeddings, query_vec)
106
  top_idxs = np.argsort(sims)[-5:][::-1]
107
  context = "\n".join([st.session_state.chunks[i] for i in top_idxs])
108
 
109
- # Prompt Engineering
110
- system_prompt = (
111
- f"ROLE CONTEXT: {roles[role]}\n\n"
 
 
112
  f"DATA CONTEXT:\n{context}\n\n"
113
- f"INSTRUCTION:\nPlease respond with a technical explanation that includes sensor-based reasoning, terminology, and if possible, an action plan.\n"
 
114
  )
115
- full_prompt = f"{system_prompt}\nUser Query: {user_input}"
116
- reply = GEN_MODEL(full_prompt, max_length=350)[0]['generated_text']
117
 
118
- # Record conversation
119
- st.session_state.chat_history.append((f"πŸ‘€ You ({role})", user_input))
120
- st.session_state.chat_history.append(("πŸ€– FactoryGPT", reply))
121
 
122
- # Show chat
123
  for speaker, msg in st.session_state.chat_history[-10:]:
124
  st.markdown(f"<div style='margin-bottom: 10px;'><b>{speaker}:</b><br>{msg}</div>", unsafe_allow_html=True)
125
 
126
  else:
127
- st.info("πŸ‘ˆ Please upload a CSV file containing numeric sensor logs to begin analysis.")
 
1
+ # app.py - FactoryGPT 5.0: Role-Differentiated Technical Assistant
2
 
3
  import streamlit as st
4
  import pandas as pd
 
7
  from transformers import pipeline
8
  from sklearn.ensemble import IsolationForest
9
 
10
+ # App Config
11
  st.set_page_config(
12
+ page_title="FactoryGPT 5.0 – Technical Role-Based Maintenance Assistant",
13
  page_icon="🧠",
14
  layout="wide"
15
  )
16
 
17
+ # Styling
18
  st.markdown("""
19
  <style>
20
  html, body, [class*="css"] {
 
36
  # Header
37
  st.markdown("""
38
  <div style='text-align: center;'>
39
+ <h1 style='color: #58a6ff;'>🏭 FactoryGPT 5.0 – Predictive Maintenance AI</h1>
40
+ <p style='color: #bbb;'>Role-Based Technical Assistant β€’ Sensor Intelligence β€’ Industry 5.0</p>
41
  <hr style='border-top: 2px solid #888;'>
42
  </div>
43
  """, unsafe_allow_html=True)
44
 
45
+ # Load NLP Models
46
  EMBED_MODEL = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
47
  GEN_MODEL = pipeline('text2text-generation', model='google/flan-t5-base')
48
 
49
+ # File Upload
50
+ uploaded_file = st.sidebar.file_uploader("πŸ“‚ Upload sensor log (CSV)", type=["csv"])
51
 
52
  if uploaded_file:
53
  df = pd.read_csv(uploaded_file)
54
  numeric_cols = df.select_dtypes(include=np.number).columns.tolist()
55
+ st.success("βœ… Sensor log successfully loaded.")
56
 
57
+ st.markdown("### πŸ“Š Sensor Data Snapshot")
58
  st.dataframe(df.head(), use_container_width=True)
59
 
60
+ # Vector Chunking for RAG
61
  def convert_to_chunks(df):
62
  return [f"[Entry {i}] " + ", ".join([f"{col}: {row[col]:.2f}" for col in numeric_cols]) for i, row in df.iterrows()]
63
 
 
67
  st.session_state.chunks = chunks
68
  st.session_state.embeddings = embeddings
69
 
70
+ # Predictive Anomaly Detection
71
+ st.markdown("### πŸ”Ž Machine Health Assessment")
72
  iso = IsolationForest(contamination=0.02)
73
  labels = iso.fit_predict(df[numeric_cols])
74
+ df['status'] = ['❌ Fault Detected' if x == -1 else 'βœ… Healthy' for x in labels]
75
+ df['maintenance_flag'] = ['πŸ”§ Inspect Required' if x == -1 else '🟒 Stable' for x in labels]
76
  st.dataframe(df[['status', 'maintenance_flag'] + numeric_cols].head(), use_container_width=True)
77
 
78
+ # Role-based Response Logic
79
+ st.markdown("### πŸ’¬ Technical Assistant by Role")
80
  roles = {
81
+ "Operator": {
82
+ "description": "Focus on simple equipment behavior. Identify unusual patterns and suggest if operator actions are needed.",
83
+ "style": (
84
+ "Explain in clear and simple terms. Focus on sensor changes, safe operation, and when to escalate to maintenance. "
85
+ "Avoid deep technical jargon."
86
+ )
87
+ },
88
+ "Maintenance": {
89
+ "description": "Examine possible fault conditions. Reference symptoms, parts affected, and recommend procedural maintenance actions.",
90
+ "style": (
91
+ "Give a technical breakdown of what components may be degrading. Recommend maintenance steps and cite related sensor evidence. "
92
+ "Use concise, technician-friendly language."
93
+ )
94
+ },
95
+ "Engineer": {
96
+ "description": "Perform data-driven diagnostics. Detect systemic issues, control instability, or potential root causes.",
97
+ "style": (
98
+ "Provide a structured root cause analysis using engineering language. Mention potential failure modes (e.g., drift, thermal loss, PID lag), "
99
+ "anomaly thresholds, and corrective engineering strategies."
100
+ )
101
+ }
102
  }
103
 
104
+ role = st.selectbox("πŸ‘€ Select your role", roles.keys())
105
 
106
  if 'chat_history' not in st.session_state:
107
  st.session_state.chat_history = []
108
 
109
+ user_input = st.text_input("🧠 Ask a question about equipment behavior")
110
 
111
  if user_input:
 
112
  query_vec = EMBED_MODEL.encode([user_input])[0]
113
  sims = np.dot(st.session_state.embeddings, query_vec)
114
  top_idxs = np.argsort(sims)[-5:][::-1]
115
  context = "\n".join([st.session_state.chunks[i] for i in top_idxs])
116
 
117
+ # Build Prompt
118
+ system_instruction = (
119
+ f"ROLE: {role}\n"
120
+ f"RESPONSIBILITIES: {roles[role]['description']}\n"
121
+ f"COMMUNICATION STYLE: {roles[role]['style']}\n\n"
122
  f"DATA CONTEXT:\n{context}\n\n"
123
+ f"QUESTION:\n{user_input}\n\n"
124
+ f"ANSWER AS {role.upper()}:\n"
125
  )
126
+ response = GEN_MODEL(system_instruction, max_length=400)[0]['generated_text']
 
127
 
128
+ st.session_state.chat_history.append((f"πŸ‘€ {role}", user_input))
129
+ st.session_state.chat_history.append(("πŸ€– FactoryGPT", response))
 
130
 
 
131
  for speaker, msg in st.session_state.chat_history[-10:]:
132
  st.markdown(f"<div style='margin-bottom: 10px;'><b>{speaker}:</b><br>{msg}</div>", unsafe_allow_html=True)
133
 
134
  else:
135
+ st.info("πŸ‘ˆ Upload a CSV sensor log to begin.")