Testys commited on
Commit
b9d2bb7
Β·
verified Β·
1 Parent(s): a37de14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -57
app.py CHANGED
@@ -12,13 +12,20 @@ from transformers import AutoTokenizer, AutoFeatureExtractor
12
  from streamer import ParlerTTSStreamer # local file
13
 
14
  from src.detection.factory import get_detector
15
- from src.alerting.alert_system import get_alerter
16
 
17
  # ──────────────────────────────────────────────────────────
18
  # CONFIG & BACKEND SET-UP
19
  # ──────────────────────────────────────────────────────────
20
  load_dotenv()
21
 
 
 
 
 
 
 
 
22
  with open("config.yaml", "r") as f:
23
  config = yaml.safe_load(f)
24
 
@@ -26,57 +33,13 @@ secrets = {"gemini_api_key": os.getenv("GEMINI_API_KEY")}
26
 
27
  print("Initializing detector and alerter …")
28
  detector = get_detector(config)
29
- alerter = get_alerter(config, secrets["gemini_api_key"])
30
- print("Backend ready.")
31
-
32
- # ──────────────────────────────────────────────────────────
33
- # TTS MODEL (Parler-TTS mini)
34
- # ──────────────────────────────────────────────────────────
35
- device = "cuda:0" if torch.cuda.is_available() else "cpu"
36
- if device == "cpu":
37
- print("\n⚠️ Running TTS on CPU will be slow; only β€˜Very Drowsy’ alerts will use it.\n")
38
-
39
- model_dtype = torch.float16 if device != "cpu" else torch.float32
40
- repo_id = "parler-tts/parler_tts_mini_v0.1"
41
 
42
- print("Loading Parler-TTS …")
43
- model = ParlerTTSForConditionalGeneration.from_pretrained(repo_id,
44
- torch_dtype=model_dtype).to(device)
45
- tokenizer = AutoTokenizer.from_pretrained(repo_id)
46
- feature_extractor = AutoFeatureExtractor.from_pretrained(repo_id)
47
- print("TTS loaded.")
48
 
49
- # ──────────────────────────────────────────────────────────
50
- # AUDIO STREAMER
51
- # ──────────────────────────────────────────────────────────
52
- def stream_alert_audio(text_prompt: str):
53
- """Yields (sampling_rate, np.ndarray) chunks for Gradio streaming."""
54
- sampling_rate = model.config.sampling_rate
55
- voice_desc = "Jenny is a female speaker with a clear and urgent voice."
56
-
57
- prompt_ids = tokenizer(text_prompt, return_tensors="pt").input_ids.to(device)
58
- desc_ids = tokenizer(voice_desc, return_tensors="pt").input_ids.to(device)
59
-
60
- streamer = ParlerTTSStreamer(model, device, play_steps=int(sampling_rate * 2.0))
61
-
62
- gen_kwargs = dict(
63
- input_ids=desc_ids,
64
- prompt_input_ids=prompt_ids,
65
- streamer=streamer,
66
- do_sample=True,
67
- temperature=1.0,
68
- repetition_penalty=1.2,
69
- )
70
-
71
- thread = Thread(target=model.generate, kwargs=gen_kwargs, daemon=True)
72
 
73
- try:
74
- thread.start()
75
- for chunk in streamer:
76
- yield (sampling_rate, chunk)
77
- finally:
78
- thread.join(timeout=0.1)
79
- alerter.reset_alert()
80
 
81
  # ──────────────────────────────────────────────────────────
82
  # FRAME PROCESSOR
@@ -85,26 +48,28 @@ def process_live_frame(frame):
85
  if frame is None:
86
  return np.zeros((480, 640, 3), np.uint8), "Status: Inactive", None
87
 
 
 
88
  processed, indicators, _ = detector.process_frame(frame)
89
  level = indicators.get("drowsiness_level", "Awake")
90
  lighting = indicators.get("lighting", "Good")
91
  score = indicators.get("details", {}).get("Score", 0)
92
 
 
 
 
93
  status_txt = f"Lighting: {lighting}\n"
94
  status_txt += ("Detection paused due to low light."
95
  if lighting == "Low"
96
  else f"Status: {level}\nScore: {score:.2f}")
97
 
 
98
  audio_out = None
99
  if level != "Awake" and lighting != "Low":
100
- payload = alerter.trigger_alert(level=level)
101
- if payload:
102
- # Static file path β†’ bytes, Dynamic Gemini path β†’ str
103
- if isinstance(payload, bytes):
104
- # Return raw bytes (Gradio accepts bytes for .wav / .mp3)
105
- audio_out = payload
106
- elif isinstance(payload, str):
107
- audio_out = stream_alert_audio(payload)
108
 
109
  return processed, status_txt, audio_out
110
 
@@ -133,4 +98,5 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue")) as app:
133
  )
134
 
135
  if __name__ == "__main__":
 
136
  app.launch(debug=True)
 
12
  from streamer import ParlerTTSStreamer # local file
13
 
14
  from src.detection.factory import get_detector
15
+ from src.alerting.alert_system import FileAlertSystem
16
 
17
  # ──────────────────────────────────────────────────────────
18
  # CONFIG & BACKEND SET-UP
19
  # ──────────────────────────────────────────────────────────
20
  load_dotenv()
21
 
22
+ logging.basicConfig(
23
+ level=logging.INFO,
24
+ format="%(asctime)s %(levelname)s β”‚ %(message)s",
25
+ datefmt="%H:%M:%S",
26
+ )
27
+
28
+
29
  with open("config.yaml", "r") as f:
30
  config = yaml.safe_load(f)
31
 
 
33
 
34
  print("Initializing detector and alerter …")
35
  detector = get_detector(config)
36
+ alerter = FileAlertSystem(CONFIG)
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ if alerter.audio_bytes is None:
39
+ logging.warning("No alert sound loaded; driver will not hear any audio!")
 
 
 
 
40
 
41
+ print("Backend ready.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
 
 
 
 
 
 
 
43
 
44
  # ──────────────────────────────────────────────────────────
45
  # FRAME PROCESSOR
 
48
  if frame is None:
49
  return np.zeros((480, 640, 3), np.uint8), "Status: Inactive", None
50
 
51
+ t0 = time.time()
52
+
53
  processed, indicators, _ = detector.process_frame(frame)
54
  level = indicators.get("drowsiness_level", "Awake")
55
  lighting = indicators.get("lighting", "Good")
56
  score = indicators.get("details", {}).get("Score", 0)
57
 
58
+ dt_ms = (time.time() - t0) * 1000.0
59
+ logging.info(f"{dt_ms:6.1f} ms β”‚ {lighting:<4} β”‚ {level:<14} β”‚ score={score:.2f}")
60
+
61
  status_txt = f"Lighting: {lighting}\n"
62
  status_txt += ("Detection paused due to low light."
63
  if lighting == "Low"
64
  else f"Status: {level}\nScore: {score:.2f}")
65
 
66
+ audio_out = None
67
  audio_out = None
68
  if level != "Awake" and lighting != "Low":
69
+ audio_bytes = alerter.trigger_alert(level=level)
70
+ logging.info(f"Printing {audio_bytes}")
71
+ if audio_bytes:
72
+ audio_out = audio_bytes
 
 
 
 
73
 
74
  return processed, status_txt, audio_out
75
 
 
98
  )
99
 
100
  if __name__ == "__main__":
101
+ logging.info("Launching Gradio app …")
102
  app.launch(debug=True)