sxandie commited on
Commit
d86058b
·
1 Parent(s): 89fd328

Commit local modifications: switch fallback ASR to transformers and update requirements

Browse files
Files changed (3) hide show
  1. requirements.txt +3 -0
  2. src/gpx_parser.py +2 -10
  3. src/llm.py +29 -36
requirements.txt CHANGED
@@ -7,4 +7,7 @@ gpxpy
7
  folium
8
  plotly
9
  pydantic>=2.0.0,<2.11.0
 
 
 
10
 
 
7
  folium
8
  plotly
9
  pydantic>=2.0.0,<2.11.0
10
+ transformers
11
+ torch
12
+ soundfile
13
 
src/gpx_parser.py CHANGED
@@ -280,16 +280,8 @@ def parse_gpx_file(file_path, cache_dir="./temp", buffer_meters=150.0):
280
 
281
  # Check cache first
282
  file_name = os.path.basename(file_path)
283
- cache_path_temp = os.path.join(cache_dir, f"{file_name}.cache.json")
284
- cache_path_routes = os.path.join(os.path.dirname(os.path.abspath(file_path)), "cache", f"{file_name}.cache.json")
285
-
286
- cache_path = None
287
- if os.path.exists(cache_path_temp):
288
- cache_path = cache_path_temp
289
- elif os.path.exists(cache_path_routes):
290
- cache_path = cache_path_routes
291
-
292
- if cache_path:
293
  try:
294
  with open(cache_path, "r", encoding="utf-8") as f:
295
  print(f"[gpx_parser] Loading cached GPX data from {cache_path}")
 
280
 
281
  # Check cache first
282
  file_name = os.path.basename(file_path)
283
+ cache_path = os.path.join(cache_dir, f"{file_name}.cache.json")
284
+ if os.path.exists(cache_path):
 
 
 
 
 
 
 
 
285
  try:
286
  with open(cache_path, "r", encoding="utf-8") as f:
287
  print(f"[gpx_parser] Loading cached GPX data from {cache_path}")
src/llm.py CHANGED
@@ -103,31 +103,35 @@ def _init_whisper():
103
  print(f"[llm.py] Error loading whisper.cpp ASR model: {e}")
104
  return None
105
 
106
- # --- Hugging Face Inference API ASR fallback ---
107
- _hf_client = None
108
-
109
- def _init_hf_client():
110
- """Lazy initialization of Hugging Face InferenceClient for fallback ASR."""
111
- global _hf_client
112
- if _hf_client is not None:
113
- return _hf_client
114
  try:
115
- from huggingface_hub import InferenceClient
116
- # Automatically detects HF_TOKEN from Hugging Face Space environment
117
- _hf_client = InferenceClient()
118
- print("[llm.py] Hugging Face InferenceClient for ASR initialized successfully!")
119
- return _hf_client
 
 
 
 
120
  except ImportError:
121
- print("[llm.py] huggingface_hub not installed. ASR will use mock fallback.")
122
  return None
123
  except Exception as e:
124
- print(f"[llm.py] Error loading HF InferenceClient: {e}")
125
  return None
126
 
127
  def transcribe_audio(audio_path, prompt=""):
128
  """
129
  Transcribe audio file to text using whisper.cpp (offline, lightweight).
130
- Falls back to Hugging Face Inference API or mock transcription if whisper.cpp is unavailable.
131
  """
132
  if not audio_path or not os.path.exists(audio_path):
133
  print("[llm.py] Audio file not found, using mock ASR.")
@@ -167,7 +171,7 @@ def transcribe_audio(audio_path, prompt=""):
167
  except: pass
168
 
169
  if not transcription:
170
- print("[llm.py] Whisper returned empty transcription, trying HF API fallback.")
171
  else:
172
  print(f"[llm.py] ASR Transcription: \"{transcription}\"")
173
  return transcription
@@ -177,29 +181,18 @@ def transcribe_audio(audio_path, prompt=""):
177
  except: pass
178
  print(f"[llm.py] Error during whisper.cpp transcription: {e}")
179
 
180
- # Fallback to Hugging Face Inference API ASR
181
- hf_client = _init_hf_client()
182
- if hf_client is not None:
183
  try:
184
- print(f"[llm.py] Transcribing audio using Hugging Face Inference API: {audio_path}")
185
- with open(audio_path, "rb") as f:
186
- audio_bytes = f.read()
187
- result = hf_client.automatic_speech_recognition(
188
- audio_bytes,
189
- model="openai/whisper-large-v3-turbo"
190
- )
191
-
192
- # Extract transcription string
193
- if isinstance(result, dict):
194
- transcription = result.get("text", "").strip()
195
- else:
196
- transcription = getattr(result, "text", str(result)).strip()
197
-
198
  if transcription:
199
- print(f"[llm.py] ASR (HF API) Transcription: \"{transcription}\"")
200
  return transcription
201
  except Exception as e:
202
- print(f"[llm.py] Error during Hugging Face Inference API transcription: {e}")
203
 
204
  return _mock_transcribe_audio(prompt)
205
 
 
103
  print(f"[llm.py] Error loading whisper.cpp ASR model: {e}")
104
  return None
105
 
106
+ # --- Transformers ASR fallback ---
107
+ _transformers_asr = None
108
+
109
+ def _init_transformers_asr():
110
+ """Lazy initialization of transformers Whisper pipeline for fallback ASR."""
111
+ global _transformers_asr
112
+ if _transformers_asr is not None:
113
+ return _transformers_asr
114
  try:
115
+ from transformers import pipeline
116
+ print("[llm.py] Loading transformers Whisper-tiny model for fallback ASR...")
117
+ _transformers_asr = pipeline(
118
+ "automatic-speech-recognition",
119
+ model="openai/whisper-tiny",
120
+ device="cpu"
121
+ )
122
+ print("[llm.py] transformers ASR model loaded successfully!")
123
+ return _transformers_asr
124
  except ImportError:
125
+ print("[llm.py] transformers or torch not installed. ASR will use mock fallback.")
126
  return None
127
  except Exception as e:
128
+ print(f"[llm.py] Error loading transformers ASR model: {e}")
129
  return None
130
 
131
  def transcribe_audio(audio_path, prompt=""):
132
  """
133
  Transcribe audio file to text using whisper.cpp (offline, lightweight).
134
+ Falls back to transformers or mock transcription if whisper.cpp is unavailable.
135
  """
136
  if not audio_path or not os.path.exists(audio_path):
137
  print("[llm.py] Audio file not found, using mock ASR.")
 
171
  except: pass
172
 
173
  if not transcription:
174
+ print("[llm.py] Whisper returned empty transcription, trying transformers fallback.")
175
  else:
176
  print(f"[llm.py] ASR Transcription: \"{transcription}\"")
177
  return transcription
 
181
  except: pass
182
  print(f"[llm.py] Error during whisper.cpp transcription: {e}")
183
 
184
+ # Fallback to transformers ASR
185
+ asr_pipe = _init_transformers_asr()
186
+ if asr_pipe is not None:
187
  try:
188
+ print(f"[llm.py] Transcribing audio using transformers: {audio_path}")
189
+ result = asr_pipe(audio_path)
190
+ transcription = result.get("text", "").strip()
 
 
 
 
 
 
 
 
 
 
 
191
  if transcription:
192
+ print(f"[llm.py] ASR (transformers) Transcription: \"{transcription}\"")
193
  return transcription
194
  except Exception as e:
195
+ print(f"[llm.py] Error during transformers transcription: {e}")
196
 
197
  return _mock_transcribe_audio(prompt)
198