Spaces:
Sleeping
Sleeping
Create transcribe.py
Browse files- transcribe.py +31 -0
transcribe.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import APIRouter, File, UploadFile, HTTPException
|
| 3 |
+
from groq import Groq
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables from .env file
|
| 7 |
+
load_dotenv()
|
| 8 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 9 |
+
if not GROQ_API_KEY:
|
| 10 |
+
raise Exception("GROQ_API_KEY is not set in the environment.")
|
| 11 |
+
|
| 12 |
+
router = APIRouter()
|
| 13 |
+
|
| 14 |
+
@router.post("/transcribe")
|
| 15 |
+
async def transcribe_audio(file: UploadFile = File(...)):
|
| 16 |
+
"""
|
| 17 |
+
Transcribe an uploaded audio file using the Groq client with the Whisper model.
|
| 18 |
+
Returns the transcribed text.
|
| 19 |
+
"""
|
| 20 |
+
try:
|
| 21 |
+
# Initialize the Groq client with the API key from the environment.
|
| 22 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 23 |
+
file_bytes = await file.read()
|
| 24 |
+
transcription = client.audio.transcriptions.create(
|
| 25 |
+
file=(file.filename, file_bytes),
|
| 26 |
+
model="whisper-large-v3",
|
| 27 |
+
response_format="verbose_json",
|
| 28 |
+
)
|
| 29 |
+
return {"transcription": transcription.text}
|
| 30 |
+
except Exception as e:
|
| 31 |
+
raise HTTPException(status_code=500, detail=str(e))
|