Spaces:
Sleeping
Sleeping
File size: 3,288 Bytes
5ea6795 9cf3310 88bc3e2 c2295d1 88bc3e2 5ea6795 eb0d83d 88bc3e2 5ea6795 6141da1 88bc3e2 c2295d1 eb0d83d 88bc3e2 ff73cbe eb0d83d 88bc3e2 ff73cbe 6141da1 88bc3e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 |
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
import openai
import os
# Set your OpenAI key here or use Hugging Face Secrets Manager
openai.api_key = os.getenv("OPENAI_API_KEY")
st.set_page_config(page_title="Smart Factory RAG Assistant", layout="wide")
st.title("π Industry 5.0 | Smart Factory RAG Assistant")
# File Upload
uploaded_file = st.file_uploader("π€ Upload your factory CSV data", type=["csv"])
if uploaded_file:
df = pd.read_csv(uploaded_file)
st.success("β
File uploaded and loaded!")
# Basic Preview
st.subheader("π Data Preview")
st.dataframe(df.head())
# Descriptive Stats
st.subheader("π Descriptive Statistics")
st.dataframe(df.describe().T)
# Correlation Analysis
st.subheader("π Parameter Correlation Heatmap")
fig, ax = plt.subplots(figsize=(10, 6))
corr = df.corr(numeric_only=True)
sns.heatmap(corr, annot=True, cmap="coolwarm", fmt=".2f", ax=ax)
st.pyplot(fig)
# Anomaly Detection
st.subheader("β οΈ Anomaly Detection using Isolation Forest")
num_df = df.select_dtypes(include='number').dropna()
scaler = StandardScaler()
X_scaled = scaler.fit_transform(num_df)
iso = IsolationForest(contamination=0.05)
df['Anomaly'] = iso.fit_predict(X_scaled)
anomalies = df[df['Anomaly'] == -1]
st.write(f"Detected {len(anomalies)} anomalies")
st.dataframe(anomalies.head(10))
# Prepare context for GPT
st.subheader("π§ Role-Based Decision Assistant")
role = st.selectbox("Select your role", ["Engineer", "Operator"])
question = st.text_input("Ask a question based on the data analysis")
if question:
with st.spinner("Thinking..."):
summary = df.describe().to_string()
corr_text = corr.to_string()
anomaly_count = len(anomalies)
context = f"""
You are a highly skilled {role} working in a smart manufacturing facility.
Here is a summary of the uploaded data:
STATISTICAL SUMMARY:
{summary}
PARAMETER CORRELATIONS:
{corr_text}
ANOMALY DETECTION:
{anomaly_count} anomalies detected using Isolation Forest method.
Based on this context, answer the following question in a clear, technically accurate manner and suggest best decisions from the point of view of a {role}.
"""
final_prompt = f"""{context}
QUESTION: {question}
ANSWER:"""
try:
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": f"You are an expert {role} in a smart factory."},
{"role": "user", "content": final_prompt}
],
temperature=0.5,
max_tokens=500
)
answer = response['choices'][0]['message']['content']
st.success("β
Recommendation:")
st.markdown(f"**{answer}**")
except Exception as e:
st.error(f"β οΈ Error calling GPT API: {str(e)}")
else:
st.info("π Please upload a factory CSV data file to begin analysis.")
|