Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import gc
|
|
| 4 |
# --- 🛡️ SHIELDS ---
|
| 5 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 6 |
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
| 7 |
-
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 8 |
|
| 9 |
# --- 🩹 Keras Monkey Patch (Fixes MTCNN Error) ---
|
| 10 |
import tensorflow as tf
|
|
@@ -14,15 +13,6 @@ sys.modules['tensorflow.keras'] = keras
|
|
| 14 |
sys.modules['tensorflow.keras.layers'] = keras.layers
|
| 15 |
sys.modules['tensorflow.keras.models'] = keras.models
|
| 16 |
|
| 17 |
-
# Compatibility Shim for Gradio 4.44 vs HF Hub 0.25+
|
| 18 |
-
import huggingface_hub
|
| 19 |
-
try: from huggingface_hub import HfFolder
|
| 20 |
-
except ImportError:
|
| 21 |
-
class MockHfFolder:
|
| 22 |
-
@staticmethod
|
| 23 |
-
def get_token(): return None
|
| 24 |
-
huggingface_hub.HfFolder = MockHfFolder
|
| 25 |
-
|
| 26 |
import gradio as gr
|
| 27 |
import cv2
|
| 28 |
import numpy as np
|
|
@@ -32,7 +22,7 @@ import pandas as pd
|
|
| 32 |
import threading
|
| 33 |
import time
|
| 34 |
|
| 35 |
-
# --- Config
|
| 36 |
DETECTOR = "mediapipe"
|
| 37 |
RESIZE_FACTOR = 0.5
|
| 38 |
COOL_DOWN_SEC = 5
|
|
@@ -58,12 +48,10 @@ def get_startup_report():
|
|
| 58 |
try:
|
| 59 |
DeepFace.build_model("VGG-Face")
|
| 60 |
report.append("🧠 AI Brain: TensorFlow (VGG) Ready")
|
| 61 |
-
gc.collect()
|
| 62 |
-
|
| 63 |
db_files = [f for f in os.listdir("faces_db") if f.endswith(('.jpg', '.jpeg', '.png'))]
|
| 64 |
report.append(f"📁 Database: {len(db_files)} Students")
|
| 65 |
report.append(f"🔍 Detector: Google MediaPipe Active")
|
| 66 |
-
|
| 67 |
if not os.path.exists(LOG_FILE): pd.DataFrame(columns=["Name", "Time"]).to_csv(LOG_FILE, index=False)
|
| 68 |
report.append("💾 Storage: Write Access OK")
|
| 69 |
except Exception as e: report.append(f"⚠️ Startup Error: {e}")
|
|
@@ -120,20 +108,20 @@ def process_frame(frame):
|
|
| 120 |
finally:
|
| 121 |
with process_lock: is_processing = False
|
| 122 |
|
| 123 |
-
with gr.Blocks(
|
| 124 |
gr.Markdown("# 🏆 Pro Attendance AI (MediaPipe + TF)")
|
| 125 |
with gr.Row():
|
| 126 |
with gr.Column(scale=2):
|
| 127 |
-
|
|
|
|
| 128 |
status_terminal = gr.Textbox(label="💻 Logic Terminal", lines=8, interactive=False)
|
| 129 |
with gr.Column(scale=1):
|
| 130 |
gr.Markdown("### 📝 Present Today")
|
| 131 |
attendance_sheet = gr.Textbox(label="", value="System Booting...", lines=18, interactive=False)
|
| 132 |
status_log = gr.Textbox(value=get_startup_report(), label="System Status", lines=5, interactive=False)
|
| 133 |
|
| 134 |
-
webcam.stream(process_frame, inputs=[webcam], outputs=[webcam, attendance_sheet, status_terminal]
|
| 135 |
-
|
| 136 |
-
demo.max_file_size = None
|
| 137 |
|
| 138 |
if __name__ == "__main__":
|
| 139 |
-
|
|
|
|
|
|
| 4 |
# --- 🛡️ SHIELDS ---
|
| 5 |
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 6 |
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
|
|
|
|
| 7 |
|
| 8 |
# --- 🩹 Keras Monkey Patch (Fixes MTCNN Error) ---
|
| 9 |
import tensorflow as tf
|
|
|
|
| 13 |
sys.modules['tensorflow.keras.layers'] = keras.layers
|
| 14 |
sys.modules['tensorflow.keras.models'] = keras.models
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
import gradio as gr
|
| 17 |
import cv2
|
| 18 |
import numpy as np
|
|
|
|
| 22 |
import threading
|
| 23 |
import time
|
| 24 |
|
| 25 |
+
# --- Config ---
|
| 26 |
DETECTOR = "mediapipe"
|
| 27 |
RESIZE_FACTOR = 0.5
|
| 28 |
COOL_DOWN_SEC = 5
|
|
|
|
| 48 |
try:
|
| 49 |
DeepFace.build_model("VGG-Face")
|
| 50 |
report.append("🧠 AI Brain: TensorFlow (VGG) Ready")
|
| 51 |
+
gc.collect()
|
|
|
|
| 52 |
db_files = [f for f in os.listdir("faces_db") if f.endswith(('.jpg', '.jpeg', '.png'))]
|
| 53 |
report.append(f"📁 Database: {len(db_files)} Students")
|
| 54 |
report.append(f"🔍 Detector: Google MediaPipe Active")
|
|
|
|
| 55 |
if not os.path.exists(LOG_FILE): pd.DataFrame(columns=["Name", "Time"]).to_csv(LOG_FILE, index=False)
|
| 56 |
report.append("💾 Storage: Write Access OK")
|
| 57 |
except Exception as e: report.append(f"⚠️ Startup Error: {e}")
|
|
|
|
| 108 |
finally:
|
| 109 |
with process_lock: is_processing = False
|
| 110 |
|
| 111 |
+
with gr.Blocks(title="Attendance Pro") as demo:
|
| 112 |
gr.Markdown("# 🏆 Pro Attendance AI (MediaPipe + TF)")
|
| 113 |
with gr.Row():
|
| 114 |
with gr.Column(scale=2):
|
| 115 |
+
# Gradio 3.x Syntax
|
| 116 |
+
webcam = gr.Image(source="webcam", streaming=True, label="Live View")
|
| 117 |
status_terminal = gr.Textbox(label="💻 Logic Terminal", lines=8, interactive=False)
|
| 118 |
with gr.Column(scale=1):
|
| 119 |
gr.Markdown("### 📝 Present Today")
|
| 120 |
attendance_sheet = gr.Textbox(label="", value="System Booting...", lines=18, interactive=False)
|
| 121 |
status_log = gr.Textbox(value=get_startup_report(), label="System Status", lines=5, interactive=False)
|
| 122 |
|
| 123 |
+
webcam.stream(process_frame, inputs=[webcam], outputs=[webcam, attendance_sheet, status_terminal])
|
|
|
|
|
|
|
| 124 |
|
| 125 |
if __name__ == "__main__":
|
| 126 |
+
# The 'share=False' fixes the ValueError on Hugging Face
|
| 127 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
|