lodhrangpt commited on
Commit
a3f9613
·
verified ·
1 Parent(s): 7951a51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -47
app.py CHANGED
@@ -1,55 +1,65 @@
1
  import gradio as gr
2
- import requests
3
- from gtts import gTTS # Import gTTS for Text-to-Speech
4
  import tempfile
 
5
 
6
- # Function to convert text to speech and transcribe
7
- def text_to_speech_transcribe(text):
8
- # Convert text to speech
9
- tts = gTTS(text, lang='en')
10
- with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_audio:
11
- audio_path = tmp_audio.name
12
- tts.save(audio_path)
13
-
14
- # Read audio file in binary mode
15
- with open(audio_path, "rb") as audio_file:
16
- audio_data = audio_file.read()
17
-
18
- # Groq API endpoint for audio transcription
19
- groq_api_endpoint = "https://api.groq.com/openai/v1/audio/transcriptions"
20
 
21
- # Replace 'YOUR_GROQ_API_KEY' with your actual Groq API key
22
- headers = {
23
- "Authorization": "Bearer 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", # Replace with your Groq API key
24
- }
25
-
26
- # Prepare the files and data for the request
27
- files = {
28
- 'file': ('audio.wav', audio_data, 'audio/wav'),
29
- }
30
- data = {
31
- 'model': 'whisper-large-v3-turbo', # Specify the model to use
32
- 'response_format': 'json', # Desired response format
33
- 'language': 'en', # Language of the audio
34
- }
 
35
 
36
- # Send audio to Groq API
37
- response = requests.post(groq_api_endpoint, headers=headers, files=files, data=data)
 
 
38
 
39
- # Parse response
40
- if response.status_code == 200:
41
- result = response.json()
42
- return result.get("text", "No transcription available.")
43
- else:
44
- return f"Error: {response.status_code}, {response.text}"
45
 
46
- # Gradio interface
47
- iface = gr.Interface(
48
- fn=text_to_speech_transcribe,
49
- inputs="text", # Input text to be converted to speech
50
- outputs="text",
51
- title="Text to Speech and Transcription",
52
- description="Enter text to convert it to audio, then transcribe it with the Groq API."
53
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- iface.launch()
 
 
 
 
1
  import gradio as gr
2
+ import edge_tts
3
+ import asyncio
4
  import tempfile
5
+ import os
6
 
7
+ # Get all available voices
8
+ async def get_voices():
9
+ voices = await edge_tts.list_voices()
10
+ return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Text-to-speech function
13
+ async def text_to_speech(text, voice, rate, pitch):
14
+ if not text.strip():
15
+ return None, gr.Warning("Please enter text to convert.")
16
+ if not voice:
17
+ return None, gr.Warning("Please select a voice.")
18
+
19
+ voice_short_name = voice.split(" - ")[0]
20
+ rate_str = f"{rate:+d}%"
21
+ pitch_str = f"{pitch:+d}Hz"
22
+ communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
23
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
24
+ tmp_path = tmp_file.name
25
+ await communicate.save(tmp_path)
26
+ return tmp_path, None
27
 
28
+ # Gradio interface function
29
+ def tts_interface(text, voice, rate, pitch):
30
+ audio, warning = asyncio.run(text_to_speech(text, voice, rate, pitch))
31
+ return audio, warning
32
 
33
+ # Create Gradio application
34
+ import gradio as gr
 
 
 
 
35
 
36
+ async def create_demo():
37
+ voices = await get_voices()
38
+
39
+ description = """
40
+
41
+
42
+ demo = gr.Interface(
43
+ fn=tts_interface,
44
+ inputs=[
45
+ gr.Textbox(label="Input Text", lines=5),
46
+ gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value=""),
47
+ gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1),
48
+ gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
49
+ ],
50
+ outputs=[
51
+ gr.Audio(label="Generated Audio", type="filepath"),
52
+ gr.Markdown(label="Warning", visible=False)
53
+ ],
54
+ title="Edge TTS Text-to-Speech",
55
+ description=description,
56
+ article="Experience the power of Edge TTS for text-to-speech conversion, and explore our advanced Text-to-Video Converter for even more creative possibilities!",
57
+ analytics_enabled=False,
58
+ allow_flagging="manual"
59
+ )
60
+ return demo
61
 
62
+ # Run the application
63
+ if __name__ == "__main__":
64
+ demo = asyncio.run(create_demo())
65
+ demo.launch()