IOT_ess2 / app.py
EngTig's picture
Update app.py
89b94f5 verified
Raw History Blame Contribute Delete
3.92 kB
import streamlit as st
import numpy as np
import pandas as pd
from tensorflow import keras
from firebase import firebase
import json # لاستخدام json.dumps لتحويل البيانات إلى JSON
# إعداد Firebase (ضع بياناتك هنا)
config = {
'apiKey': "AIzaSyDZCCMjd003FcPDRcik5jCHCEQL56pkeFY",
'authDomain': "jsonfile-esp32-mohiot.firebaseapp.com",
'databaseURL': "https://jsonfile-esp32-mohiot-default-rtdb.europe-west1.firebasedatabase.app",
'projectId': "jsonfile-esp32-mohiot",
'storageBucket': "jsonfile-esp32-mohiot.firebasestorage.app",
'messagingSenderId': "710233105459",
'appId': "1:710233105459:web:6a5dd9b611e6f558c36f46"
}
firebase = firebase.FirebaseApplication(config['databaseURL'], None)
# تحميل بيانات السكر
csv_data = pd.read_csv('sugar_level.csv')
sugar_levels = csv_data['Sugar level']
current_csv_index = 0
# تحميل النماذج
try:
Seizure_Detection_model = keras.models.load_model("seizure_detection.h5", compile=False)
Seizure_Detection_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
Fall_Detection_model = keras.models.load_model("Fall_Detection.h5", compile=False)
Fall_Detection_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
except Exception as e:
st.error(f"Error loading models: {e}") # عرض الخطأ في Streamlit
st.stop() # إيقاف التطبيق في حالة وجود خطأ
# دالة جلب مستوى السكر التالي
def get_next_sugar_level():
global current_csv_index
if current_csv_index >= len(sugar_levels):
return sugar_levels.iloc[-1]
value = sugar_levels.iloc[current_csv_index]
current_csv_index += 1
return value
# دالة التنبؤ
def process_sensor_data():
try:
data = firebase.get('/sensors', None)
if not data:
return {"message": "لم يتم استقبال بيانات من Firebase"}
heart_rate = data.get('MAX30102', 88)
spo2 = data.get('MAX30102', 98)
Temperature = data.get('MAX30205', 37)
Distance = data.get('VL53L0X', 100)
sugar_level = get_next_sugar_level()
GSR = data.get('GSR', 0)
# التنبؤ بالنوبات
X_test_seizure = np.array([[heart_rate, Temperature, spo2]])
seizure_prediction = Seizure_Detection_model.predict(X_test_seizure)
seizure_result = "يوجد نوبة" if seizure_prediction[0][0] > 0.5 else "لا يوجد نوبة"
# التنبؤ بالسقوط
X_test_fall = np.array([[Distance, heart_rate, sugar_level, spo2]])
fall_prediction = Fall_Detection_model.predict(X_test_fall)
fall_class = np.argmax(fall_prediction, axis=1)[0]
fall_result = {0: "لا يوجد سقوط", 1: "احتمال سقوط", 2: "تم السقوط"}.get(fall_class, "غير معروف")
results = {
"معدل_ضربات_القلب": heart_rate,
"نسبة_الأكسجين_في_الدم": spo2,
"درجة_الحرارة": Temperature,
"مستوى_السكر": sugar_level,
"معدل_التعرق": GSR,
"حالة_النوبات": seizure_result,
"حالة_السقوط": fall_result
}
return json.dumps(results, ensure_ascii=False) # تحويل النتائج إلى JSON باستخدام json.dumps
except Exception as e:
return {"error": str(e)}
# إنشاء Streamlit UI
st.title("نموذج الذكاء الاصطناعي للكشف عن النوبات والسقوط")
st.write("والقياسات الحيوية")
if st.button("تشغيل التنبؤ"):
with st.spinner("جاري معالجة البيانات..."):
result = process_sensor_data()
st.write(result) # عرض نتائج JSON في Streamlit