File size: 9,058 Bytes
201bb3e
 
 
f0a88ca
 
 
 
 
 
096ae89
f0a88ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201bb3e
 
 
 
 
 
 
58c6bad
201bb3e
 
 
 
f0a88ca
201bb3e
 
58c6bad
201bb3e
f0a88ca
 
201bb3e
 
58c6bad
201bb3e
f0a88ca
 
 
201bb3e
 
f0a88ca
 
096ae89
201bb3e
096ae89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f0a88ca
096ae89
 
f0a88ca
 
096ae89
f0a88ca
096ae89
f0a88ca
096ae89
201bb3e
 
 
f0a88ca
 
201bb3e
 
f0a88ca
 
 
 
201bb3e
 
096ae89
f0a88ca
096ae89
f0a88ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201bb3e
f0a88ca
 
 
 
 
096ae89
201bb3e
096ae89
201bb3e
f0a88ca
201bb3e
 
096ae89
f0a88ca
096ae89
 
 
 
 
 
 
 
 
 
 
 
 
 
201bb3e
 
 
 
 
58c6bad
f0a88ca
 
201bb3e
 
 
 
f0a88ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201bb3e
f0a88ca
 
 
 
 
 
 
 
201bb3e
 
 
 
 
f0a88ca
201bb3e
 
 
 
 
 
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import gradio as gr
from .processor import process_document

SYNTHESIS_MODES = {
    "narration": {
        "description": "Simple document narration with clear voice and natural pacing",
        "styles": ["Technical", "Narrative", "Instructional", "Descriptive"],
        "default_temp": 0.7,
        "default_chunks": 300,
        "system_prompt": """Convert this content into clear narration."""
    },
    "podcast": {
        "description": "Conversational style with engaging tone and dynamic pacing",
        "styles": ["Casual", "Interview", "Educational", "Commentary"],
        "default_temp": 0.8,
        "default_chunks": 400,
        "system_prompt": """Transform this content into engaging podcast-style speech."""
    },
    "presentation": {
        "description": "Professional presentation style with clear structure",
        "styles": ["Business", "Academic", "Sales", "Training"],
        "default_temp": 0.6,
        "default_chunks": 250,
        "system_prompt": """Convert this content into a presentation format."""
    },
    "storytelling": {
        "description": "Narrative style with emotional engagement",
        "styles": ["Dynamic", "Dramatic", "Calm", "Energetic"],
        "default_temp": 0.9,
        "default_chunks": 500,
        "system_prompt": """Transform this content into an engaging story."""
    }
}

def create_interface():
    with gr.Blocks(theme=gr.themes.Base()) as demo:
        gr.HTML(
            """
            <div style="margin-bottom: 1rem;">
                <img src="https://raw.githubusercontent.com/pixeltable/pixeltable/main/docs/source/data/pixeltable-logo-large.png" 
                     alt="Pixeltable" style="max-width: 150px;" />
                <h1>📄 Document to Audio Synthesis 🎧</h1>
            </div>
            """
        )

        # Overview Row
        with gr.Row():
            with gr.Column():
                with gr.Accordion("🎯 What does it do?", open=True):
                    gr.Markdown("""
                        - 📄 Document processing  - 🧠 Content transformation
                        - 🎧 Audio synthesis  - ⚙️ Multiple output styles
                    """)
            with gr.Column():
                with gr.Accordion("⚡ How does it work?", open=True):
                    gr.Markdown("""
                        1. 📑 **Processing:** Token-based segmentation
                        2. 🔍 **Analysis:** LLM optimization & scripts
                        3. 🎵 **Synthesis:** Multiple voice options
                    """)

        synthesis_mode = gr.State(SYNTHESIS_MODES["narration"])

        # Main Settings Row
        with gr.Row():
            # Core Settings Column
            with gr.Column():
                with gr.Accordion("🔑 Core Settings", open=True):
                    with gr.Row():
                        api_key = gr.Textbox(
                            label="OpenAI API Key",
                            placeholder="sk-...",
                            type="password",
                            scale=2
                        )
                        file_input = gr.File(
                            label="PDF Document",
                            file_types=[".pdf"],
                            scale=1
                        )

            # Mode Selection Column
            with gr.Column():
                with gr.Accordion("🎭 Output Mode", open=True):
                    mode_select = gr.Radio(
                        choices=list(SYNTHESIS_MODES.keys()),
                        value="narration",
                        label="Select Mode",
                        info="Choose output style"
                    )
                    mode_description = gr.Markdown(
                        SYNTHESIS_MODES["narration"]["description"]
                    )

        # Voice and Processing Settings Row
        with gr.Row():
            # Voice Settings Column
            with gr.Column():
                with gr.Accordion("🎛️ Voice & Style", open=True):
                    voice_select = gr.Radio(
                        choices=["alloy", "echo", "fable", "onyx", "nova", "shimmer"],
                        value="onyx",
                        label="🎙️ Voice",
                        interactive=True
                    )
                    style_select = gr.Radio(
                        choices=SYNTHESIS_MODES["narration"]["styles"],
                        value=SYNTHESIS_MODES["narration"]["styles"][0],
                        label="💫 Style",
                        interactive=True
                    )

            # Processing Settings Column
            with gr.Column():
                with gr.Accordion("⚙️ Processing Parameters", open=True):
                    with gr.Row():
                        chunk_size = gr.Slider(
                            minimum=100, maximum=1000,
                            value=SYNTHESIS_MODES["narration"]["default_chunks"],
                            step=50,
                            label="📏 Chunk Size"
                        )
                        temperature = gr.Slider(
                            minimum=0, maximum=1,
                            value=SYNTHESIS_MODES["narration"]["default_temp"],
                            step=0.1,
                            label="🌡️ Temperature"
                        )
                        max_tokens = gr.Slider(
                            minimum=100, maximum=1000,
                            value=300,
                            step=50,
                            label="📊 Tokens"
                        )

        # Process Button Row
        with gr.Row():
            process_btn = gr.Button("🚀 Generate Audio", variant="primary", scale=2)
            status_output = gr.Textbox(label="📋 Status", scale=1)

        # Output Section
        with gr.Tabs():
            with gr.TabItem("📝 Content"):
                output_table = gr.Dataframe(
                    headers=["🔍 Segment", "📄 Content", "🎭 Script"],
                    wrap=True
                )
            with gr.TabItem("🎧 Audio"):
                with gr.Row():
                    with gr.Column(scale=2):
                        audio_output = gr.Audio(
                            label="🔊 Synthesized Audio", 
                            type="filepath",
                            show_download_button=True
                        )
                    with gr.Column(scale=1):
                        with gr.Accordion("📚 Quick Tips", open=True):
                            gr.Markdown("""
                                - 🎯 Lower temperature = more consistent
                                - 📏 Smaller chunks = more precise
                                - 🎙️ Try different voices for best fit
                                - 💫 Match style to content type
                            """)

        gr.HTML(
            """
            <div style="text-align: center; margin-top: 1rem; padding-top: 1rem; border-top: 1px solid #ccc;">
                <p style="margin: 0; color: #666; font-size: 0.8em;">
                    🚀 Powered by <a href="https://github.com/pixeltable/pixeltable" target="_blank" style="color: #F25022; text-decoration: none;">Pixeltable</a>
                    | 📚 <a href="https://docs.pixeltable.io" target="_blank" style="color: #666;">Docs</a>
                    | 🤗 <a href="https://huggingface.co/spaces/Pixeltable/document-to-audio-synthesis" target="_blank" style="color: #666;">HF Space</a>
                </p>
            </div>
            """
        )

        def update_mode(mode_name):
            mode = SYNTHESIS_MODES[mode_name]
            return (
                gr.update(choices=mode["styles"], value=mode["styles"][0]),
                gr.update(value=mode["default_chunks"]),
                gr.update(value=mode["default_temp"]),
                mode["description"]
            )

        mode_select.change(
            update_mode,
            inputs=[mode_select],
            outputs=[style_select, chunk_size, temperature, mode_description]
        )

        def update_interface(pdf_file, api_key, mode_name, voice, style, chunk_size, temperature, max_tokens):
            mode = SYNTHESIS_MODES[mode_name]
            return process_document(
                pdf_file=pdf_file,
                api_key=api_key,
                voice_choice=voice,
                style_choice=style,
                chunk_size=chunk_size,
                temperature=temperature,
                max_tokens=max_tokens,
                system_prompt=mode["system_prompt"]
            )
        
        process_btn.click(
            update_interface,
            inputs=[
                file_input, api_key, mode_select, voice_select, style_select,
                chunk_size, temperature, max_tokens
            ],
            outputs=[output_table, audio_output, status_output]
        )
    
    return demo