Spaces:
Sleeping
Sleeping
Delete audio_transcriber.py
Browse files- audio_transcriber.py +0 -54
audio_transcriber.py
DELETED
@@ -1,54 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import requests
|
3 |
-
from smolagents import Tool
|
4 |
-
|
5 |
-
class AudioTranscriptionTool(Tool):
|
6 |
-
name = "audio_transcriber"
|
7 |
-
description = "Transcribe a given audio file in .mp3 or .wav format using Whisper via Hugging Face API."
|
8 |
-
inputs = {
|
9 |
-
"file_path": {
|
10 |
-
"type": "string",
|
11 |
-
"description": "Path to the audio file (.mp3 or .wav)"
|
12 |
-
}
|
13 |
-
}
|
14 |
-
output_type = "string"
|
15 |
-
|
16 |
-
def __init__(self):
|
17 |
-
super().__init__()
|
18 |
-
api_token = os.getenv("HF_API_TOKEN")
|
19 |
-
if not api_token:
|
20 |
-
raise EnvironmentError("HF_API_TOKEN not found in environment variables.")
|
21 |
-
self.api_url = "https://api-inference.huggingface.com/models/openai/whisper-large"
|
22 |
-
self.headers = {
|
23 |
-
"Authorization": f"Bearer {api_token}"
|
24 |
-
}
|
25 |
-
|
26 |
-
def forward(self, file_path: str) -> str:
|
27 |
-
if not file_path.lower().endswith((".mp3", ".wav")):
|
28 |
-
return "Error: File must be .mp3 or .wav format."
|
29 |
-
|
30 |
-
try:
|
31 |
-
with open(file_path, "rb") as audio_file:
|
32 |
-
audio_bytes = audio_file.read()
|
33 |
-
|
34 |
-
response = requests.post(
|
35 |
-
self.api_url,
|
36 |
-
headers=self.headers,
|
37 |
-
data=audio_bytes,
|
38 |
-
timeout=60
|
39 |
-
)
|
40 |
-
|
41 |
-
if response.status_code == 200:
|
42 |
-
result = response.json()
|
43 |
-
transcription = result.get("text")
|
44 |
-
if transcription:
|
45 |
-
return transcription.strip()
|
46 |
-
else:
|
47 |
-
return "Error: No transcription found in API response."
|
48 |
-
else:
|
49 |
-
return f"Error transcribing audio: {response.status_code} - {response.text}"
|
50 |
-
|
51 |
-
except Exception as e:
|
52 |
-
return f"Error transcribing audio: {e}"
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|