Sarathkumar1304ai commited on
Commit
9af104f
·
verified ·
1 Parent(s): 5007b5e

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +102 -0
  2. models/model.pkl +3 -0
  3. models/pca.pkl +3 -0
  4. models/tsne.pkl +3 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import requests
4
+ import joblib
5
+ import json
6
+ import os
7
+ import numpy as np
8
+ from sklearn.decomposition import PCA
9
+
10
+ # Load local model components
11
+ pca_path = "models/pca.pkl"
12
+ local_model_path = "models/model.pkl"
13
+
14
+ # Load models if they exist
15
+
16
+ pca = joblib.load(pca_path) if os.path.exists(pca_path) else None
17
+ local_model = joblib.load(local_model_path) if os.path.exists(local_model_path) else None
18
+
19
+ # MLflow Server Endpoint
20
+ MLFLOW_SERVER_URL = "http://127.0.0.1:8000/invocations"
21
+
22
+ # Streamlit UI
23
+ st.title("Imago AI Vomitoxin Prediction System")
24
+ st.write("Upload a CSV file to predict vomitoxin levels in corn samples.")
25
+
26
+ # Upload CSV File
27
+ uploaded_file = st.file_uploader("Upload CSV File", type=["csv"])
28
+
29
+ # Prediction Function
30
+ def predict(data):
31
+ """Predict using MLflow server if available, else use local model."""
32
+
33
+ # Try MLflow first
34
+ try:
35
+ st.write("Checking MLflow Server...")
36
+
37
+ # Convert data to JSON format for MLflow
38
+ json_data = json.dumps({"inputs": df.to_numpy().tolist()}) # Convert to NumPy first
39
+
40
+
41
+ # Send request to MLflow
42
+ response = requests.post(MLFLOW_SERVER_URL, json={"inputs": json_data})
43
+
44
+ # If successful, return MLflow prediction
45
+ if response.status_code == 200:
46
+ predictions = response.json()
47
+ st.success("Prediction done using MLflow Server!")
48
+ return np.array(predictions)
49
+ else:
50
+ st.warning("MLflow Server not responding. Falling back to local model.")
51
+
52
+ except requests.exceptions.RequestException:
53
+ st.warning("MLflow Server is not running. Using local model.")
54
+
55
+ # If MLflow fails, use local model
56
+ if local_model and pca:
57
+ # Apply Scaling & PCA
58
+ # scaled_data = scaler.transform(data)
59
+ pca_data = pca.transform(data)
60
+
61
+ # Predict with local model
62
+ predictions = local_model.predict(pca_data)
63
+ st.success("Prediction done using Local Model!")
64
+ return predictions
65
+ else:
66
+ st.error("No model available for prediction.")
67
+ return None
68
+
69
+ # Process File & Predict
70
+ if uploaded_file:
71
+ try:
72
+ # Read CSV
73
+ df = pd.read_csv(uploaded_file)
74
+
75
+ # Ensure required columns exist
76
+ if "hsi_id" not in df.columns:
77
+ st.error("CSV file must contain `hsi_id` column.")
78
+ else:
79
+ # Extract Features (Drop hsi_id)
80
+ X = df.drop(columns=["hsi_id"])
81
+
82
+ # Make Predictions
83
+ predictions = predict(X)
84
+
85
+ if predictions is not None:
86
+ # Create output DataFrame
87
+ df["vomitoxin_ppb"] = predictions
88
+
89
+ # Mark Harmful Cases (vomitoxin > 1000)
90
+ df["Harmful"] = df["vomitoxin_ppb"].apply(lambda x: "⚠️ Yes" if x > 1000 else "✅ No")
91
+
92
+ # Display DataFrame
93
+ st.subheader("Prediction Results")
94
+ st.write(df[["hsi_id", "vomitoxin_ppb", "Harmful"]])
95
+
96
+ # Highlight Harmful Cases
97
+ st.subheader("Harmful Cases")
98
+ harmful_df = df[df["vomitoxin_ppb"] > 1000]
99
+ st.write(harmful_df[["hsi_id", "vomitoxin_ppb"]])
100
+
101
+ except Exception as e:
102
+ st.error(f"Error processing file: {e}")
models/model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1364d694a2903dbe637c0496b055c29eef75866bf2695f6e14654f955027ebd7
3
+ size 183743
models/pca.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d1431b403e519ce7e0c8004d39e170470e12be008c2ee840d8c52d16b55fdb4a
3
+ size 17038
models/tsne.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d610993cd9dfe855a877a1ca6a3e219844e415a1e4af3c0afb8927cad11cd040
3
+ size 9679