deepakdara commited on
Commit
cec3b11
·
verified ·
1 Parent(s): 888dfda

Created app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import io
4
+ import base64
5
+ import requests
6
+ import re
7
+ from scipy.io import wavfile
8
+ from murf import Murf
9
+
10
+ # Global variable to track the number of tries.
11
+ attempt_count = 0
12
+
13
+ # Initialize the Murf client with your initial API key.
14
+ client = Murf(api_key="ap2_214acdca-35de-4dca-8810-7cc7b0068cb9")
15
+
16
+ def extract_voice_id(selection):
17
+ """
18
+ Extracts the voice ID from the dropdown selection.
19
+ Expects a string in the format "Label (voice_id)".
20
+ """
21
+ match = re.search(r'\((.*?)\)', selection)
22
+ if match:
23
+ return match.group(1)
24
+ return selection
25
+
26
+ def generate_voice_murf(text, voice_selection):
27
+ global attempt_count, client
28
+ # Check if the try limit is reached.
29
+ if attempt_count >= 5:
30
+ raise gr.Error("Limit of 5 tries reached. Please update your API key. To fetch your new API key, visit https://murf.ai/api/dashboard.")
31
+ attempt_count += 1
32
+
33
+ # Extract the voice_id from the dropdown selection.
34
+ voice_id = extract_voice_id(voice_selection)
35
+
36
+ try:
37
+ # Limit text to 250 characters.
38
+ text = text[:250]
39
+
40
+ # Call the Murf API to generate speech.
41
+ response = client.text_to_speech.generate(
42
+ text=text,
43
+ voice_id=voice_id,
44
+ format="WAV"
45
+ )
46
+
47
+ # Try to access the audio data.
48
+ if hasattr(response, "encoded_audio") and response.encoded_audio:
49
+ audio_bytes = base64.b64decode(response.encoded_audio)
50
+ elif hasattr(response, "audio_file") and response.audio_file:
51
+ audio_url = response.audio_file
52
+ r = requests.get(audio_url)
53
+ if r.status_code != 200:
54
+ raise Exception("Error downloading audio file.")
55
+ audio_bytes = r.content
56
+ else:
57
+ raise Exception("No audio returned by the API.")
58
+
59
+ # Convert the audio bytes to a NumPy array for Gradio.
60
+ audio_file = io.BytesIO(audio_bytes)
61
+ sample_rate, audio_data = wavfile.read(audio_file)
62
+ return (sample_rate, audio_data)
63
+
64
+ except Exception as e:
65
+ raise gr.Error(str(e))
66
+
67
+ def update_api_key(new_key):
68
+ global client, attempt_count
69
+ if new_key.strip() == "":
70
+ raise gr.Error("Please provide a valid API key.")
71
+ client = Murf(api_key=new_key)
72
+ attempt_count = 0 # Reset try counter
73
+ return "API key updated successfully. You can now try again."
74
+
75
+ # Define sample voice options as friendly strings.
76
+ voice_options = [
77
+ "Natalie (en-US-natalie)",
78
+ "Matt (en-UK-matt)"
79
+ ]
80
+
81
+ with gr.Blocks() as demo:
82
+ gr.Markdown("# Murf TTS Demo")
83
+ gr.Markdown("A demo using the Murf API to generate speech from text. Enter some text (max 250 characters) and choose a voice.")
84
+
85
+ input_text = gr.Textbox(
86
+ label="Input Text (250 characters max)",
87
+ lines=2,
88
+ value="Hello, welcome to the Murf TTS demo!",
89
+ elem_id="input_text"
90
+ )
91
+
92
+ voice_dropdown = gr.Dropdown(
93
+ label="Voice",
94
+ choices=voice_options,
95
+ value=voice_options[0],
96
+ elem_id="voice_dropdown"
97
+ )
98
+
99
+ generate_button = gr.Button("Generate Voice", elem_id="generate_button")
100
+ out_audio = gr.Audio(label="Generated Voice", type="numpy", elem_id="out_audio")
101
+
102
+ generate_button.click(
103
+ fn=generate_voice_murf,
104
+ inputs=[input_text, voice_dropdown],
105
+ outputs=out_audio,
106
+ queue=True
107
+ )
108
+
109
+ # Update API Key section – only needed after a "Limit of 5 tries reached" error.
110
+ gr.Markdown("### Update API Key (Only update after receiving a 'Limit of 5 tries reached' error)")
111
+ gr.Markdown("To fetch your new API key, visit [Murf Dashboard](https://murf.ai/api/dashboard).")
112
+ new_api_key = gr.Textbox(
113
+ label="New API Key",
114
+ placeholder="Enter new API key here...",
115
+ elem_id="new_api_key"
116
+ )
117
+ update_button = gr.Button("Update API Key", elem_id="update_button")
118
+ api_key_status = gr.Textbox(label="Status", interactive=False, elem_id="api_key_status")
119
+
120
+ update_button.click(
121
+ fn=update_api_key,
122
+ inputs=new_api_key,
123
+ outputs=api_key_status,
124
+ queue=True
125
+ )
126
+
127
+ demo.launch()