Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -203,6 +203,23 @@ def transcription(file):
|
|
| 203 |
print(f"Exception: {e}")
|
| 204 |
return None
|
| 205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
|
| 208 |
ALLOWED_EXTENSIONS = {".mp3", ".wav", ".wma"}
|
|
@@ -236,7 +253,7 @@ def inference(input_prompt: Prompt, question: Prompt):
|
|
| 236 |
return custom_qa_automation(text=input_prompt.text, custom_question=question.text)
|
| 237 |
|
| 238 |
|
| 239 |
-
@app.post("/api/transcribe-
|
| 240 |
async def transcribe_audio_route(file: UploadFile = File(...)):
|
| 241 |
# Check if the file extension is allowed
|
| 242 |
file_extension = os.path.splitext(file.filename)[1].lower()
|
|
@@ -248,3 +265,16 @@ async def transcribe_audio_route(file: UploadFile = File(...)):
|
|
| 248 |
return {"transcription": transcription_result}
|
| 249 |
else:
|
| 250 |
return {"error": "Transcription failed"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
print(f"Exception: {e}")
|
| 204 |
return None
|
| 205 |
|
| 206 |
+
def transcription_content(file):
|
| 207 |
+
try:
|
| 208 |
+
deepgram = DeepgramClient(DEEPGRAM_API_KEY)
|
| 209 |
+
if isinstance(file, str): # File from folder
|
| 210 |
+
with open(file, "rb") as f:
|
| 211 |
+
buffer_data = f.read()
|
| 212 |
+
else: # Uploaded file
|
| 213 |
+
buffer_data = file.file.read()
|
| 214 |
+
|
| 215 |
+
payload = {"buffer": buffer_data}
|
| 216 |
+
options = PrerecordedOptions(model="nova-2", smart_format=True, diarize=True)
|
| 217 |
+
response = deepgram.listen.prerecorded.v("1").transcribe_file(payload, options)
|
| 218 |
+
return response..results.channels[0].alternatives[0].transcript
|
| 219 |
+
except Exception as e:
|
| 220 |
+
print(f"Exception: {e}")
|
| 221 |
+
return None
|
| 222 |
+
|
| 223 |
|
| 224 |
|
| 225 |
ALLOWED_EXTENSIONS = {".mp3", ".wav", ".wma"}
|
|
|
|
| 253 |
return custom_qa_automation(text=input_prompt.text, custom_question=question.text)
|
| 254 |
|
| 255 |
|
| 256 |
+
@app.post("/api/transcribe-display", summary="Transcribe audio file", tags=["Transcription"])
|
| 257 |
async def transcribe_audio_route(file: UploadFile = File(...)):
|
| 258 |
# Check if the file extension is allowed
|
| 259 |
file_extension = os.path.splitext(file.filename)[1].lower()
|
|
|
|
| 265 |
return {"transcription": transcription_result}
|
| 266 |
else:
|
| 267 |
return {"error": "Transcription failed"}
|
| 268 |
+
|
| 269 |
+
@app.post("/api/transcribe-content", summary="Transcribe audio file", tags=["Transcription"])
|
| 270 |
+
async def transcribe_audio_route(file: UploadFile = File(...)):
|
| 271 |
+
# Check if the file extension is allowed
|
| 272 |
+
file_extension = os.path.splitext(file.filename)[1].lower()
|
| 273 |
+
if file_extension not in ALLOWED_EXTENSIONS:
|
| 274 |
+
raise HTTPException(status_code=400, detail=f"File format not supported. Allowed formats: {', '.join(ALLOWED_EXTENSIONS)}")
|
| 275 |
+
|
| 276 |
+
transcription_result = transcription_content(file)
|
| 277 |
+
if transcription_result:
|
| 278 |
+
return {"transcription": transcription_result}
|
| 279 |
+
else:
|
| 280 |
+
return {"error": "Transcription failed"}
|