Download app.py from EngTig/IOT_ess: direct link, hf CLI and curl.
- Browser
- Download file 4.03 kB
-
https://huggingface.co/spaces/EngTig/IOT_ess/resolve/main/app.py
- Command line
-
hf download hf://spaces/EngTig/IOT_ess/app.py
-
curl -L -o app.py https://huggingface.co/spaces/EngTig/IOT_ess/resolve/main/app.py
4.03 kB
| import gradio as gr | |
| import numpy as np | |
| import pandas as pd | |
| from tensorflow import keras | |
| from firebase import firebase | |
| # إعداد 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: | |
| print(f"Error loading models: {e}") # طباعة الخطأ لتصحيحه | |
| exit() # إيقاف البرنامج إذا لم يتم تحميل النماذج | |
| # دالة جلب مستوى السكر التالي | |
| 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"} # رسالة خطأ بتنسيق JSON | |
| 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, "غير معروف") | |
| return { | |
| "معدل_ضربات_القلب": heart_rate, | |
| "نسبة_الأكسجين_في_الدم": spo2, | |
| "درجة_الحرارة": Temperature, | |
| "مستوى_السكر": sugar_level, | |
| "معدل_التعرق": GSR, | |
| "حالة_النوبات": seizure_result, | |
| "حالة_السقوط": fall_result | |
| } | |
| except Exception as e: | |
| return {"error": str(e)} # إرجاع خطأ بتنسيق JSON | |
| # إنشاء Gradio UI | |
| iface = gr.Interface( | |
| fn=process_sensor_data, | |
| inputs=[], | |
| outputs="json", # هام: تحديد نوع الخرج ك JSON | |
| title="نموذج الذكاء الاصطناعي للكشف عن النوبات والسقوط", | |
| description="يتم جلب البيانات من Firebase وإجراء التنبؤات باستخدام نماذج الذكاء الاصطناعي." | |
| ) | |
| # تشغيل التطبيق | |
| iface.launch(server_name="0.0.0.0", server_port=7860, share=True) |