Spaces:
Sleeping
Sleeping
Upload transcribe.py
Browse files- tools/transcribe.py +47 -0
tools/transcribe.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# tools/transcribe.py
|
| 2 |
+
from langchain_core.tools import tool
|
| 3 |
+
from groq import Groq
|
| 4 |
+
import os
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
@tool
|
| 10 |
+
def transcribe_audio(filename: str) -> str:
|
| 11 |
+
"""
|
| 12 |
+
Transcribe speech from an audio file into text.
|
| 13 |
+
Use this immediately after downloading an .mp3 or .wav file to know what is said inside it.
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
filename (str): The filename of the audio (e.g., 'audio.mp3') located in LLMFiles/.
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
str: The transcribed text.
|
| 20 |
+
"""
|
| 21 |
+
try:
|
| 22 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 23 |
+
if not api_key:
|
| 24 |
+
return "Error: GROQ_API_KEY not found."
|
| 25 |
+
|
| 26 |
+
# Initialize Groq Client
|
| 27 |
+
client = Groq(api_key=api_key)
|
| 28 |
+
|
| 29 |
+
# Construct full path (assuming download_file saves to LLMFiles)
|
| 30 |
+
file_path = os.path.join("LLMFiles", filename)
|
| 31 |
+
|
| 32 |
+
if not os.path.exists(file_path):
|
| 33 |
+
return f"Error: File {file_path} does not exist. Did you download it first?"
|
| 34 |
+
|
| 35 |
+
# Open file and send to Groq Whisper
|
| 36 |
+
with open(file_path, "rb") as file:
|
| 37 |
+
transcription = client.audio.transcriptions.create(
|
| 38 |
+
file=(filename, file.read()),
|
| 39 |
+
model="distil-whisper-large-v3-en", # Fast and accurate English model
|
| 40 |
+
response_format="json",
|
| 41 |
+
temperature=0.0
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
return transcription.text
|
| 45 |
+
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"Error transcribing audio: {str(e)}"
|