File size: 5,976 Bytes
af6d7d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e19b042
af6d7d0
 
 
 
 
 
 
 
8ad3de5
af6d7d0
 
e19b042
 
 
af6d7d0
8ad3de5
af6d7d0
 
8ad3de5
 
af6d7d0
8ad3de5
af6d7d0
 
 
 
 
e19b042
af6d7d0
e19b042
af6d7d0
 
 
 
 
 
 
e19b042
af6d7d0
 
e19b042
af6d7d0
 
 
 
8ad3de5
af6d7d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ad3de5
e19b042
af6d7d0
e19b042
 
af6d7d0
 
 
 
 
 
 
8ad3de5
af6d7d0
e19b042
af6d7d0
 
 
 
8ad3de5
 
af6d7d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e19b042
 
af6d7d0
 
 
 
8ad3de5
 
 
 
 
 
 
 
af6d7d0
 
 
8ad3de5
 
 
af6d7d0
 
 
 
8ad3de5
 
af6d7d0
 
 
8ad3de5
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import json
import asyncio
import edge_tts
from pydub import AudioSegment
import os
import gradio as gr
from gradio_client import Client
import shutil
import uuid
from dotenv import load_dotenv
import re

load_dotenv()

def sanitize_filename(filename):
    """Convert a string to a safe filename by removing special characters and spaces"""
    safe_filename = re.sub(r'[^a-zA-Z0-9_-]', '', filename.replace(' ', '_'))
    return safe_filename.lower()[:50]

async def get_voices():
    """Get all available English voices from edge-tts"""
    voices = await edge_tts.list_voices()
    english_voices = [
        voice for voice in voices 
        if voice["Locale"].startswith(("en-US", "en-GB", "en-AU", "en-CA", "en-IN"))
    ]
    
    formatted_voices = [
        f"{voice['ShortName']} ({voice['Gender']}, {voice['Locale']})"
        for voice in english_voices
    ]
    return formatted_voices

def extract_voice_name(voice_string):
    """Extract the voice short name from the formatted string"""
    return voice_string.split(" (")[0]

async def generate_audio(text, voice, filename):
    communicate = edge_tts.Communicate(text, extract_voice_name(voice))
    await communicate.save(filename)

async def create_podcast_version(data, speaker_name, speaker_voice, title):
    session_id = str(uuid.uuid4())
    temp_dir = f'temp_{session_id}'
    safe_title = sanitize_filename(title)
    
    if not os.path.exists(temp_dir):
        os.makedirs(temp_dir)
    
    try:
        speaker_version = AudioSegment.empty()
        
        for i, entry in enumerate(data['conversation']):
            if 'speakertext' in entry:
                temp_file = f'{temp_dir}/speaker_{i}.mp3'
                await generate_audio(entry['speakertext'], speaker_voice, temp_file)
                audio = AudioSegment.from_file(temp_file)
                speaker_version += audio
                os.remove(temp_file)
        
        speaker_path = f"{safe_title}_{speaker_name.lower()}_only.mp3"
        speaker_version.export(speaker_path, format="mp3")
        
        return speaker_path, temp_dir
    except Exception as e:
        if os.path.exists(temp_dir):
            shutil.rmtree(temp_dir)
        raise e

def generate_podcast(title, channel_name, speaker_name, speaker_voice):
    try:
        if not all([title, channel_name, speaker_name, speaker_voice]):
            raise ValueError("All fields must be filled out")
        
        client = Client(os.getenv('API_URL'))
        result = client.predict(
            message=f"""{os.getenv('API_MESSAGE')} {{
              "title": "{title}",
              "channel": "{channel_name}",
              "speaker": "{speaker_name}",
              "conversation": [
                {{
                  "speakertext": ""
                }}
              ]
            }}
            
            give 36 sentences for the speaker.
            """,
            request=os.getenv('API_REQUEST'),
            param_3=0.5,
            param_4=8100,
            param_5=0.5,
            param_6=0,
            api_name="/chat"
        )

        try:
            podcast_data = json.loads(result)
        except json.JSONDecodeError:
            json_start = result.find('```') + 3
            json_end = result.rfind('```')
            
            if json_start > 2 and json_end > json_start:
                if result[json_start:json_start+4] == 'json':
                    json_start = result.find('\n', json_start) + 1
                json_str = result[json_start:json_end].strip()
                podcast_data = json.loads(json_str)
            else:
                raise ValueError("Could not parse JSON from response")

        speaker_path, temp_dir = asyncio.run(
            create_podcast_version(
                podcast_data, 
                speaker_name,
                speaker_voice,
                title
            )
        )
        
        if os.path.exists(temp_dir):
            shutil.rmtree(temp_dir)
        
        return [speaker_path, podcast_data]
    except Exception as e:
        return [None, f"Error: {str(e)}"]

with gr.Blocks(theme=gr.themes.Soft()) as interface:
    available_voices = asyncio.run(get_voices())
    
    gr.Markdown("# Easy Podcast (Single Speaker)")
    gr.Markdown("Generate a podcast with one speaker. Choose a voice and customize details to create your perfect audio.<br>To use elevelabs voices or cloned voices, or to automate the podcast video creation with avatar contact me at aheedsajid@gmail.com<br>Support me USDT (TRC-20) (TAe7hsSVWtMEYz3G5V1UiUdYPQVqm28bKx)")
    
    with gr.Row():
        with gr.Column():
            title = gr.Textbox(
                label="Podcast Topic",
                placeholder="e.g., The Future of AI",
                show_label=True
            )
            channel_name = gr.Textbox(
                label="Channel Name",
                placeholder="e.g., TechTalks",
                value="WeePakistan",
                show_label=True
            )
        with gr.Column():
            speaker_name = gr.Textbox(
                label="Speaker Name",
                placeholder="e.g., John",
                value="Andrew",
                show_label=True
            )
    
    with gr.Row():
        speaker_voice = gr.Dropdown(
            choices=available_voices,
            value=next((v for v in available_voices if "Andrew" in v), available_voices[0]),
            label="Speaker Voice",
            info="Select voice for the speaker"
        )
    
    generate_btn = gr.Button("Generate Podcast", variant="primary")
    
    with gr.Row():
        speaker_audio = gr.Audio(label="Speaker Audio")
    
    conversation_json = gr.JSON(label="Generated Conversation")
    
    generate_btn.click(
        fn=generate_podcast,
        inputs=[title, channel_name, speaker_name, speaker_voice],
        outputs=[speaker_audio, conversation_json]
    )

if __name__ == "__main__":
    interface.launch()