Spaces:
Sleeping
Sleeping
| import requests | |
| from fastapi import HTTPException | |
| # Murf API Endpoint | |
| MURF_API_URL = "https://api.murf.ai/v1/speech/generate" | |
| def generate_audio_with_murf(text: str, api_key: str, voice_id: str = "en-US-marcus") -> str: | |
| """ | |
| Generates audio using Murf.ai API and returns the audio URL. | |
| """ | |
| payload = { | |
| "voiceId": voice_id, | |
| "text": text, | |
| "style": "General", | |
| "rate": 0, | |
| "pitch": 0, | |
| "sampleRate": 24000, | |
| "format": "MP3", | |
| "channelType": "MONO", | |
| "encodeAsBase64": False | |
| } | |
| headers = { | |
| "Content-Type": "application/json", | |
| "Accept": "application/json", | |
| "api-key": api_key | |
| } | |
| try: | |
| response = requests.post(MURF_API_URL, json=payload, headers=headers) | |
| response.raise_for_status() | |
| data = response.json() | |
| # Check if URL is present in response | |
| if "audioFile" in data: | |
| return data["audioFile"] | |
| elif "encodedAudio" in data: | |
| raise HTTPException(status_code=500, detail="Received encoded audio but expected URL") | |
| else: | |
| raise HTTPException(status_code=500, detail=f"Unexpected response from Murf API: {data}") | |
| except requests.exceptions.RequestException as e: | |
| detail = str(e) | |
| if e.response is not None: | |
| try: | |
| detail = e.response.json() | |
| except: | |
| detail = e.response.text | |
| raise HTTPException(status_code=400, detail=f"Murf API Error: {detail}") | |