File size: 1,557 Bytes
37c6d1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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}")