DKLPDV15\eB commited on
Commit
cddd102
·
1 Parent(s): 4c1718d

Support Hugging Face ZeroGPU media processing

Browse files
Files changed (2) hide show
  1. requirements.txt +1 -0
  2. src/ui/gradio_app.py +53 -21
requirements.txt CHANGED
@@ -1,5 +1,6 @@
1
  # Hugging Face Space runtime only
2
  gradio
 
3
  pandas
4
  numpy
5
  scikit-learn
 
1
  # Hugging Face Space runtime only
2
  gradio
3
+ spaces
4
  pandas
5
  numpy
6
  scikit-learn
src/ui/gradio_app.py CHANGED
@@ -7,6 +7,17 @@ from typing import Any
7
 
8
  import gradio as gr
9
 
 
 
 
 
 
 
 
 
 
 
 
10
  from src.evaluation.dashboard import CBAODashboard
11
  from src.logging_layer.analytics_logger import AnalyticsLogger
12
  from src.models.speech_to_text import SpeechToText
@@ -75,6 +86,27 @@ def _safe_result(answer: str, modality: str) -> dict[str, Any]:
75
  }
76
 
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  def handle_submit(
79
  text_query: str | None,
80
  audio_file: str | None,
@@ -90,32 +122,32 @@ def handle_submit(
90
  speech_result: dict[str, Any] | None = None
91
  image_result: dict[str, Any] | None = None
92
  debug_flags: dict[str, Any] = {}
93
- if audio_file:
94
  try:
95
- speech_result = SPEECH.transcribe(audio_file)
 
 
 
 
96
  except Exception:
97
- speech_result = {
98
- "success": False,
99
- "transcribed_text": "",
100
- "error": "Speech processing unavailable.",
101
- }
 
 
 
 
 
 
 
 
 
 
102
  if not speech_result.get("success"):
103
  debug_flags["speech_error"] = "Speech processing unavailable."
104
  if image_file:
105
- try:
106
- image_result = (
107
- VISION.analyse(image_file, text)
108
- if CLIP is _DEFAULT_CLIP
109
- else CLIP.predict_image(image_file)
110
- )
111
- except Exception:
112
- image_result = {
113
- "success": False,
114
- "predicted_category": "fallback_unknown",
115
- "predicted_label": "Unknown or unsupported image",
116
- "suggested_record_ids": [],
117
- "error": "Image processing unavailable.",
118
- }
119
  if not image_result.get("success"):
120
  debug_flags["image_error"] = "Image processing unavailable."
121
  try:
 
7
 
8
  import gradio as gr
9
 
10
+ try:
11
+ import spaces
12
+ except ImportError: # Local/test environments do not require ZeroGPU runtime.
13
+ class _LocalSpaces:
14
+ @staticmethod
15
+ def GPU(duration: int = 60):
16
+ del duration
17
+ return lambda function: function
18
+
19
+ spaces = _LocalSpaces()
20
+
21
  from src.evaluation.dashboard import CBAODashboard
22
  from src.logging_layer.analytics_logger import AnalyticsLogger
23
  from src.models.speech_to_text import SpeechToText
 
86
  }
87
 
88
 
89
+ @spaces.GPU(duration=120)
90
+ def _process_gpu_media(
91
+ audio_file: str | None,
92
+ image_file: str | None,
93
+ text: str,
94
+ ) -> tuple[dict[str, Any] | None, dict[str, Any] | None]:
95
+ """Run only voice/image model work inside the ZeroGPU allocation."""
96
+
97
+ speech_result: dict[str, Any] | None = None
98
+ image_result: dict[str, Any] | None = None
99
+ if audio_file:
100
+ speech_result = SPEECH.transcribe(audio_file)
101
+ if image_file:
102
+ image_result = (
103
+ VISION.analyse(image_file, text)
104
+ if CLIP is _DEFAULT_CLIP
105
+ else CLIP.predict_image(image_file)
106
+ )
107
+ return speech_result, image_result
108
+
109
+
110
  def handle_submit(
111
  text_query: str | None,
112
  audio_file: str | None,
 
122
  speech_result: dict[str, Any] | None = None
123
  image_result: dict[str, Any] | None = None
124
  debug_flags: dict[str, Any] = {}
125
+ if audio_file or image_file:
126
  try:
127
+ speech_result, image_result = _process_gpu_media(
128
+ audio_file,
129
+ image_file,
130
+ text,
131
+ )
132
  except Exception:
133
+ if audio_file:
134
+ speech_result = {
135
+ "success": False,
136
+ "transcribed_text": "",
137
+ "error": "Speech processing unavailable.",
138
+ }
139
+ if image_file:
140
+ image_result = {
141
+ "success": False,
142
+ "predicted_category": "fallback_unknown",
143
+ "predicted_label": "Unknown or unsupported image",
144
+ "suggested_record_ids": [],
145
+ "error": "Image processing unavailable.",
146
+ }
147
+ if audio_file:
148
  if not speech_result.get("success"):
149
  debug_flags["speech_error"] = "Speech processing unavailable."
150
  if image_file:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
  if not image_result.get("success"):
152
  debug_flags["image_error"] = "Image processing unavailable."
153
  try: