Spaces:
Sleeping
Sleeping
| import os | |
| import tempfile | |
| import requests | |
| import json | |
| import uuid | |
| from typing import Tuple, Optional, Dict, Any | |
| def transcribe_audio(audio_file, api_key, model_id="scribe_v1"): | |
| if not api_key: | |
| return {"error": "Please provide an API key"} | |
| url = "https://api.elevenlabs.io/v1/speech-to-text" | |
| headers = { | |
| "xi-api-key": api_key | |
| } | |
| files = { | |
| "file": open(audio_file, "rb"), | |
| "model_id": (None, model_id) | |
| } | |
| try: | |
| response = requests.post(url, headers=headers, files=files) | |
| response.raise_for_status() | |
| result = response.json() | |
| return result | |
| except requests.exceptions.RequestException as e: | |
| return {"error": f"API request failed: {str(e)}"} | |
| except json.JSONDecodeError: | |
| return {"error": "Failed to parse API response"} | |
| finally: | |
| files["file"].close() | |
| def save_transcription(transcription): | |
| if "error" in transcription: | |
| return None, transcription["error"] | |
| transcript_filename = f"transcription_{uuid.uuid4().hex[:8]}.txt" | |
| try: | |
| with open(transcript_filename, "w", encoding="utf-8") as f: | |
| f.write(transcription.get('text', 'No text found')) | |
| return transcript_filename, "Transcription saved as text file" | |
| except Exception as e: | |
| return None, f"Error saving transcription: {str(e)}" | |
| def process_audio_file(audio_file, elevenlabs_api_key, model_id="scribe_v1") -> Tuple[str, str, str]: | |
| if not elevenlabs_api_key: | |
| return None, "ElevenLabs API key is required for transcription", None | |
| transcription_result = transcribe_audio(audio_file, elevenlabs_api_key, model_id) | |
| if "error" in transcription_result: | |
| return None, transcription_result["error"], None | |
| transcript_text = transcription_result.get('text', '') | |
| transcript_path = tempfile.mktemp(suffix='.txt') | |
| with open(transcript_path, 'w', encoding='utf-8') as transcript_file: | |
| transcript_file.write(transcript_text) | |
| return transcript_path, f"Transcription completed successfully. Length: {len(transcript_text)} characters.", transcript_text |