Yudum commited on
Commit
369756b
Β·
verified Β·
1 Parent(s): ef5138a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -43
app.py CHANGED
@@ -327,67 +327,43 @@ class AnalyzeImageTool(Tool):
327
  return response
328
  except Exception as e:
329
  return f"Vision error: {e}"
330
- # ─────────────────────────────────────────────────────────────
331
- # Tool – Audio transcription (MP3/WAV β†’ text via Gemini)
332
- # ─────────────────────────────────────────────────────────────
333
  class TranscribeAudioTool(Tool):
334
  name = "transcribe_audio"
335
  description = (
336
- "Download an audio file (MP3/WAV/M4A) and transcribe its speech to text. "
337
- "Use for questions that reference attached audio recordings or voice memos."
338
  )
339
  inputs = {
340
  "source": {
341
  "type": "string",
342
- "description": "URL or local file path of the audio file",
343
  },
344
  }
345
  output_type = "string"
346
 
 
 
 
 
 
 
 
347
  def forward(self, source: str) -> str:
348
  try:
349
  if source.startswith("http"):
350
  r = requests.get(source, timeout=30)
351
  r.raise_for_status()
352
- audio_bytes = r.content
353
- ext = source.split("?")[0].rsplit(".", 1)[-1].lower() or "mp3"
354
  else:
355
  with open(source, "rb") as f:
356
- audio_bytes = f.read()
357
- ext = source.rsplit(".", 1)[-1].lower()
358
- except Exception as e:
359
- return f"Could not load audio: {e}"
360
-
361
- b64 = base64.b64encode(audio_bytes).decode()
362
- api_key = os.getenv("GEMINI_API_KEY") or os.getenv("API_KEY", "")
363
- payload = {
364
- "model": MODEL_NAME,
365
- "max_tokens": 2048,
366
- "messages": [{
367
- "role": "user",
368
- "content": [
369
- {"type": "input_audio",
370
- "input_audio": {"data": b64, "format": ext}},
371
- {"type": "text",
372
- "text": (
373
- "Please transcribe this audio recording completely and accurately. "
374
- "Include every word spoken. Return only the transcription, nothing else."
375
- )},
376
- ],
377
- }],
378
- }
379
- try:
380
- resp = requests.post(
381
- f"{API_BASE}chat/completions",
382
- headers={"Authorization": f"Bearer {api_key}",
383
- "Content-Type": "application/json"},
384
- json=payload, timeout=90,
385
- )
386
- if resp.status_code == 200:
387
- return resp.json()["choices"][0]["message"]["content"].strip()
388
- return f"Audio API error: HTTP {resp.status_code} {resp.text[:300]}"
389
  except Exception as e:
390
- return f"Could not transcribe audio: {e}"
 
391
 
392
 
393
  # ─────────────────────────────────────────────────────────────
@@ -533,7 +509,7 @@ class BasicAgent:
533
  YouTubeTranscriptTool(),
534
  SafeVisitWebpageTool(),
535
  AnalyzeImageTool(),
536
- # TranscribeAudioTool(),
537
  ReadExcelFileTool(),
538
  ReadTextFileTool(),
539
  WikipediaTablesTool(),
 
327
  return response
328
  except Exception as e:
329
  return f"Vision error: {e}"
330
+
 
 
331
  class TranscribeAudioTool(Tool):
332
  name = "transcribe_audio"
333
  description = (
334
+ "Convert speech from audio files into text. "
335
+ "Use for podcasts, interviews, voice memos, or any recorded speech content."
336
  )
337
  inputs = {
338
  "source": {
339
  "type": "string",
340
+ "description": "Audio file URL or local path (MP3/WAV/M4A/FLAC/OGG)",
341
  },
342
  }
343
  output_type = "string"
344
 
345
+ def __init__(self):
346
+ from huggingface_hub import InferenceClient
347
+ self.client = InferenceClient(
348
+ model="openai/whisper-small",
349
+ token=os.getenv("HF_TOKEN")
350
+ )
351
+
352
  def forward(self, source: str) -> str:
353
  try:
354
  if source.startswith("http"):
355
  r = requests.get(source, timeout=30)
356
  r.raise_for_status()
357
+ audio_data = r.content
 
358
  else:
359
  with open(source, "rb") as f:
360
+ audio_data = f.read()
361
+
362
+ response = self.client.automatic_speech_recognition(audio_data)
363
+ return response.get("text", "No speech detected")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
364
  except Exception as e:
365
+ return f"Transcription error: {e}"
366
+
367
 
368
 
369
  # ─────────────────────────────────────────────────────────────
 
509
  YouTubeTranscriptTool(),
510
  SafeVisitWebpageTool(),
511
  AnalyzeImageTool(),
512
+ TranscribeAudioTool(),
513
  ReadExcelFileTool(),
514
  ReadTextFileTool(),
515
  WikipediaTablesTool(),