generated from streamlit/streamlit-hello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHello-2-float64.py
160 lines (128 loc) · 5.8 KB
/
Hello-2-float64.py
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
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
import pandas as pd
import streamlit as st
import requests
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
# st.markdown("""
# <style>
# .custom-font {font-size: 16px; font-weight: bold;}
# </style> """, unsafe_allow_html=True)
# st.markdown('<p class="custom-font">Absorbance data :</p>', unsafe_allow_html=True)
def json_data():
# First API call
api_url1 = "https://x8ki-letl-twmt.n7.xano.io/api:3Ws6ADLi/bgdata"
payload1 = {}
response1 = requests.get(api_url1, params=payload1)
if response1.status_code == 200:
data1 = response1.json()
else:
st.write("Error in first API call:", response1.status_code)
return None
# Second API call
api_url2 = "https://x8ki-letl-twmt.n7.xano.io/api:Qc5crfn2/spectraldata"
payload2 = {}
response2 = requests.get(api_url2, params=payload2)
if response2.status_code == 200:
data2 = response2.json()
else:
st.write("Error in second API call:", response2.status_code)
return None
# Extract first line of data from both API responses and convert to numeric
df1 = pd.DataFrame(data1).iloc[:1].apply(pd.to_numeric, errors='coerce')
df2 = pd.DataFrame(data2).iloc[:1].apply(pd.to_numeric, errors='coerce')
st.write('Bg:')
st.write(df1)
st.write('Spectral:')
st.write(df2)
wavelengths = df1.columns
# Element-wise division of the dataframes & convert absorbance data to csv
absorbance_df = df1.div(df2.values).pow(2)
st.write('Absorbance:')
st.write(absorbance_df)
# Convert DataFrame to CSV
absorbance_df.to_csv('absorbance_data.csv', index=False)
# # First row of absorbance data
# absorbance_data = absorbance_df.iloc[0]
return absorbance_df, wavelengths
def load_model(model_dir):
if model_dir.endswith('.tflite'): # Check if model is a TensorFlow Lite model
# Load TensorFlow Lite model
interpreter = tf.lite.Interpreter(model_path=model_dir)
interpreter.allocate_tensors()
return interpreter
else:
# Load TensorFlow SavedModel
model = tf.saved_model.load(model_dir)
return model
def predict_with_model(model, input_data):
if isinstance(model, tf.lite.Interpreter): # Check if model is TensorFlow Lite Interpreter
input_details = model.get_input_details()
output_details = model.get_output_details()
# Assuming input_data is already in the correct shape and type
model.set_tensor(input_details[0]['index'], input_data)
model.invoke()
predictions = model.get_tensor(output_details[0]['index'])
return predictions # This will be a numpy array
else:
# Existing prediction code for TensorFlow SavedModel
input_array = input_data.to_numpy(dtype='float64')
input_array_reshaped = input_array.reshape(-1, 19) # Adjust to match the number of features your model expects
input_tensor = tf.convert_to_tensor(input_array_reshaped, dtype=tf.float64)
predictions = model(input_tensor)
return predictions.numpy() # Convert predictions to numpy array if needed
def main():
# Define model paths with labels
# Define model paths with labels
model_paths_with_labels = [
('Ori (R39)', 'reva-lablink-hb-125-(original-data).csv_r2_0.39_2024-02-15_11-55-27')
]
# model_paths_with_labels = [
# ('SNV + br (R49)', 'snv_baseline_removed_pls_top_10_float32.parquet_best_model_2024-03-31_13-29-57'),
# ('TFLite', 'tflite_model_snv_br_10.tflite'),
# ('TFLite Q', 'tflite_model_snv_br_10_quant.tflite')
# ]
# model_paths_with_labels = [
# ('TF (SNV + br)', 'snv_baseline_removed_pls_top_10_float32.parquet_best_model_2024-04-03_04-18-56'),
# ('TFLite', 'tflite_model_snv_br_10_2024-04-03_04-18-56.tflite'),
# ('TFLite Q', 'tflite_model_snv_br_10_quant_2024-04-03_04-18-56.tflite')
# ]
# model_paths_with_labels = [
# ('TF (SNV + euc)', 'snv_normalized_euclidean_pls_top_10_float32.parquet_best_model_2024-03-30_02-03-57'),
# ('TFLite', 'tflite_model_snv_euc_10_2024-03-30_02-03-57.tflite'),
# ('TFLite Q', 'tflite_model_snv_euc_10_quant_2024-03-30_02-03-57.tflite')
# ]
# Get data from server (simulated here)
absorbance_df, wavelengths = json_data()
for label, model_path in model_paths_with_labels:
# Load the model
model = load_model(model_path)
# st.write(model)
# Predict
predictions = predict_with_model(model, absorbance_df)
predictions_value = predictions[0][0]
st.markdown("""
<style>
.label {font-size: 16px; font-weight: bold; color: black;}
.value {font-size: 60px; font-weight: bold; color: blue;}
.high-value {font-size: 60px; font-weight: bold; color: red;}
</style> """, unsafe_allow_html=True)
# Add condition for prediction value
if predictions_value > 25:
display_value = f'<span class="high-value">High value : ({predictions_value:.2f} g/dL)</span>'
else:
display_value = f'<span class="value">{predictions_value:.2f} g/dL</span>'
# Display label and prediction value
st.markdown(f'<span class="label">Haemoglobin ({label}):</span><br>{display_value}</p>', unsafe_allow_html=True)
# Plotting
plt.figure(figsize=(10, 4))
plt.plot(wavelengths, absorbance_data.iloc[0], marker='o', linestyle='-', color='b')
plt.xlabel('Wavelength (nm)', fontweight='bold', fontsize=14)
plt.ylabel('Absorbance', fontweight='bold', fontsize=14)
plt.xticks(rotation='vertical', fontweight='bold', fontsize=12)
plt.yticks(fontweight='bold', fontsize=12)
plt.tight_layout()
plt.show()
st.pyplot(plt)
if __name__ == "__main__":
main()