File size: 4,859 Bytes
8c6fff2
c5ee948
14562e6
 
 
c5ee948
 
 
14562e6
c5ee948
 
 
 
14562e6
 
 
c5ee948
 
 
14562e6
c5ee948
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14562e6
c5ee948
 
8c6fff2
c5ee948
 
f870c02
c5ee948
 
14562e6
 
 
 
 
f870c02
8c6fff2
 
c5ee948
 
8c6fff2
 
 
c5ee948
8c6fff2
 
c5ee948
 
8c6fff2
c5ee948
 
 
 
 
8c6fff2
f870c02
 
 
 
 
 
 
 
 
 
8c6fff2
 
 
14562e6
8c6fff2
 
 
 
14562e6
8c6fff2
 
 
 
14562e6
8c6fff2
 
 
 
14562e6
8c6fff2
 
 
 
f870c02
8c6fff2
c5ee948
8c6fff2
f870c02
8c6fff2
 
f870c02
14562e6
 
f870c02
8c6fff2
 
 
 
 
 
 
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
import gradio as gr
import asyncio
from pathlib import Path
from utils.content_generator import ContentGenerator, ContentRequest
from utils.youtube_utils import get_transcript, extract_video_id

class TranscriptProcessor:
    def __init__(self):
        self.generator = ContentGenerator()

    def _get_youtube_transcript(self, url: str) -> str:
        """Get transcript from YouTube URL."""
        try:
            if video_id := extract_video_id(url):
                return get_transcript(video_id)
            raise Exception("Invalid YouTube URL")
        except Exception as e:
            raise Exception(f"Error fetching YouTube transcript: {str(e)}")

    async def process_transcript(self, input_text: str):
        """Process input and generate all content."""
        try:
            # Get transcript from URL or use direct input
            transcript = (
                self._get_youtube_transcript(input_text)
                if any(x in input_text for x in ["youtube.com", "youtu.be"])
                else input_text
            )

            # Define content generation requests
            requests = [
                ContentRequest("clips", max_tokens=8192),
                ContentRequest("description"),
                ContentRequest("timestamps", temperature=0.4),
                ContentRequest("titles_and_thumbnails", temperature=0.7),
            ]

            # Generate all content concurrently
            results = await asyncio.gather(
                *[self.generator.generate_content(req, transcript) for req in requests]
            )
            return tuple(results)

        except Exception as e:
            return (f"Error processing input: {str(e)}",) * 4

    def update_prompts(self, *values) -> str:
        """Update the current session's prompts."""
        self.generator.current_prompts.update(zip(
            ["clips", "description", "timestamps", "titles_and_thumbnails"],
            values
        ))
        return "Prompts updated for this session!"

def create_interface():
    """Create the Gradio interface."""
    processor = TranscriptProcessor()
    
    with gr.Blocks(title="Podcast Transcript Analyzer") as app:
        with gr.Tab("Generate Content"):
            gr.Markdown("# Podcast Content Generator")
            input_text = gr.Textbox(label="Input", placeholder="YouTube URL or transcript...", lines=10)
            submit_btn = gr.Button("Generate Content")
            outputs = [
                gr.Textbox(label=label, lines=10, interactive=False)
                for label in ["Twitter Clips", "Twitter Description", "Timestamps", "Title & Thumbnail Suggestions"]
            ]
            
            async def process_wrapper(text):
                return await processor.process_transcript(text)
            
            submit_btn.click(fn=process_wrapper, inputs=[input_text], outputs=outputs)

        with gr.Tab("Experiment with Prompts"):
            gr.Markdown("# Experiment with Prompts")
            gr.Markdown(
                """
            Here you can experiment with different prompts during your session. 
            Changes will remain active until you reload the page.
            
            Tip: Copy your preferred prompts somewhere safe if you want to reuse them later!
            """
            )

            prompt_inputs = [
                gr.Textbox(
                    label="Clips Prompt", lines=10, value=processor.generator.current_prompts["clips"]
                ),
                gr.Textbox(
                    label="Description Prompt",
                    lines=10,
                    value=processor.generator.current_prompts["description"],
                ),
                gr.Textbox(
                    label="Timestamps Prompt",
                    lines=10,
                    value=processor.generator.current_prompts["timestamps"],
                ),
                gr.Textbox(
                    label="Titles & Thumbnails Prompt",
                    lines=10,
                    value=processor.generator.current_prompts["titles_and_thumbnails"],
                ),
            ]
            status = gr.Textbox(label="Status", interactive=False)

            # Update prompts when they change
            for prompt in prompt_inputs:
                prompt.change(fn=processor.update_prompts, inputs=prompt_inputs, outputs=[status])

            # Reset button
            reset_btn = gr.Button("Reset to Default Prompts")
            reset_btn.click(
                fn=lambda: (
                    processor.update_prompts(*processor.generator.current_prompts.values()),
                    *processor.generator.current_prompts.values(),
                ),
                outputs=[status] + prompt_inputs,
            )

    return app

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