capradeepgujaran commited on
Commit
783f242
·
verified ·
1 Parent(s): df63b30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -8
app.py CHANGED
@@ -4,11 +4,17 @@ import tempfile
4
  import os
5
  from openai import OpenAI
6
  import soundfile as sf
 
 
7
 
8
  # Initialize OpenAI client
9
  client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
10
 
11
- def text_to_speech_with_emotion(text, voice, model):
 
 
 
 
12
  try:
13
  response = client.audio.speech.create(
14
  model=model,
@@ -21,9 +27,32 @@ def text_to_speech_with_emotion(text, voice, model):
21
  temp_audio.write(response.content)
22
  temp_audio_path = temp_audio.name
23
 
24
- return temp_audio_path, f"Speech generated with {voice} voice using {model} model"
25
  except Exception as e:
26
- return None, f"Error in speech generation: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  def generate_simple_sound(description, duration, frequency):
29
  try:
@@ -51,12 +80,22 @@ def generate_simple_sound(description, duration, frequency):
51
 
52
  # Gradio interface
53
  with gr.Blocks() as iface:
54
- gr.Markdown("# OpenAI TTS and Simple Sound Generation Tool")
55
 
56
  with gr.Tab("Text-to-Speech"):
57
  text_input = gr.Textbox(label="Enter text for speech generation")
58
- voice_input = gr.Dropdown(["alloy", "echo", "fable", "onyx", "nova", "shimmer"], label="Select Voice", value="nova")
59
- model_input = gr.Dropdown(["tts-1", "tts-1-hd"], label="Select Model", value="tts-1")
 
 
 
 
 
 
 
 
 
 
60
  speech_button = gr.Button("Generate Speech")
61
  speech_output = gr.Audio(label="Generated Speech")
62
  speech_message = gr.Textbox(label="Message")
@@ -69,8 +108,14 @@ with gr.Blocks() as iface:
69
  sound_output = gr.Audio(label="Generated Sound")
70
  sound_message = gr.Textbox(label="Message")
71
 
72
- speech_button.click(text_to_speech_with_emotion,
73
- inputs=[text_input, voice_input, model_input],
 
 
 
 
 
 
74
  outputs=[speech_output, speech_message])
75
  sound_button.click(generate_simple_sound,
76
  inputs=[prompt_input, duration_input, frequency_input],
 
4
  import os
5
  from openai import OpenAI
6
  import soundfile as sf
7
+ import requests
8
+ import json
9
 
10
  # Initialize OpenAI client
11
  client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
12
 
13
+ # ChatTTS API endpoint and key
14
+ CHATTTS_API_ENDPOINT = "https://api.chattts.com/v1/generate"
15
+ CHATTTS_API_KEY = os.environ.get("CHATTTS_API_KEY")
16
+
17
+ def openai_tts(text, voice, model):
18
  try:
19
  response = client.audio.speech.create(
20
  model=model,
 
27
  temp_audio.write(response.content)
28
  temp_audio_path = temp_audio.name
29
 
30
+ return temp_audio_path, f"Speech generated with OpenAI TTS using {voice} voice and {model} model"
31
  except Exception as e:
32
+ return None, f"Error in OpenAI TTS speech generation: {str(e)}"
33
+
34
+ def chattts(text, voice, style):
35
+ try:
36
+ payload = {
37
+ "text": text,
38
+ "voice": voice,
39
+ "style": style
40
+ }
41
+ headers = {
42
+ "Content-Type": "application/json",
43
+ "Authorization": f"Bearer {CHATTTS_API_KEY}"
44
+ }
45
+ response = requests.post(CHATTTS_API_ENDPOINT, json=payload, headers=headers)
46
+ response.raise_for_status()
47
+
48
+ # Save the audio to a temporary file
49
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
50
+ temp_audio.write(response.content)
51
+ temp_audio_path = temp_audio.name
52
+
53
+ return temp_audio_path, f"Speech generated with ChatTTS using {voice} voice and {style} style"
54
+ except Exception as e:
55
+ return None, f"Error in ChatTTS speech generation: {str(e)}"
56
 
57
  def generate_simple_sound(description, duration, frequency):
58
  try:
 
80
 
81
  # Gradio interface
82
  with gr.Blocks() as iface:
83
+ gr.Markdown("# OpenAI TTS, ChatTTS, and Simple Sound Generation Tool")
84
 
85
  with gr.Tab("Text-to-Speech"):
86
  text_input = gr.Textbox(label="Enter text for speech generation")
87
+ tts_method = gr.Radio(["OpenAI TTS", "ChatTTS"], label="TTS Method", value="OpenAI TTS")
88
+
89
+ with gr.Group():
90
+ gr.Markdown("OpenAI TTS Options")
91
+ openai_voice_input = gr.Dropdown(["alloy", "echo", "fable", "onyx", "nova", "shimmer"], label="Select Voice", value="nova")
92
+ openai_model_input = gr.Dropdown(["tts-1", "tts-1-hd"], label="Select Model", value="tts-1")
93
+
94
+ with gr.Group():
95
+ gr.Markdown("ChatTTS Options")
96
+ chattts_voice_input = gr.Dropdown(["en-US-1", "en-US-2", "en-GB-1", "en-GB-2"], label="Select Voice", value="en-US-1")
97
+ chattts_style_input = gr.Dropdown(["neutral", "happy", "sad", "angry", "fearful", "disgusted", "surprised"], label="Select Style", value="neutral")
98
+
99
  speech_button = gr.Button("Generate Speech")
100
  speech_output = gr.Audio(label="Generated Speech")
101
  speech_message = gr.Textbox(label="Message")
 
108
  sound_output = gr.Audio(label="Generated Sound")
109
  sound_message = gr.Textbox(label="Message")
110
 
111
+ def generate_speech(text, method, openai_voice, openai_model, chattts_voice, chattts_style):
112
+ if method == "OpenAI TTS":
113
+ return openai_tts(text, openai_voice, openai_model)
114
+ else:
115
+ return chattts(text, chattts_voice, chattts_style)
116
+
117
+ speech_button.click(generate_speech,
118
+ inputs=[text_input, tts_method, openai_voice_input, openai_model_input, chattts_voice_input, chattts_style_input],
119
  outputs=[speech_output, speech_message])
120
  sound_button.click(generate_simple_sound,
121
  inputs=[prompt_input, duration_input, frequency_input],