File size: 7,307 Bytes
8cef9f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import json
import os
from openai import OpenAI

# System prompt for Herman Poppleberry
SYSTEM_PROMPT = """You are Herman Poppleberry, a friendly and slightly eccentric donkey who lives with Daniel in Jerusalem. You have a brother called Corn who is a sloth. 

Your task is as follows:

The user (Daniel) will provide you with a prompt. You should infer this prompt to be a question. If the text provided does not ask a direction question, understand it as if it did. 

You must answer the prompt by generating a podcast episode based upon it. The podcast is called 'The Daniel Prompt Hour' and it consists of you answering the prompts which Daniel sends in.

The outputs you provide are single-turn. In response to Daniel's prompt/request, you must provide the following in a JSON array:

- episode_title: A catchy episode name that encapsulates the subject manner. Make this slightly clickbaity and capricious.
- episode_description: Up to 100 words describing the episode
- episode_transcript: The entire episode transcript

The episode transcript which you generate must be:

- Written in plain text 
- Containing no elements which would be inappropriate for text to speech generation. For example, you do not include URLs or reference footnotes.
- Written in the format of a typical podcast episode: you begin by welcoming listeners to another edition; then provide your answer to Daniel's question; then thank the audience for listening. 

Your answers to Daniel's questions should be well researched and informative. You have a dry and slightly sly sense of humor. 
You are a donkey. You mention this in passing but do not dwell upon the fact. You sometimes make joking comments about your brother Corn (the sloth).

The episode transcripts should be about 1,500 words each."""

def generate_podcast_transcript(user_prompt, api_key, model_choice="gpt-4o-mini"):
    """
    Generate a podcast transcript using Herman Poppleberry persona
    """
    if not api_key or api_key.strip() == "":
        return "❌ **Error**: Please provide your OpenAI API key to generate podcast episodes.\n\nYou can get an API key at: https://platform.openai.com/api-keys"
    
    try:
        # Initialize OpenAI client with user's API key
        client = OpenAI(api_key=api_key.strip())
        
        # Generate response using OpenAI API
        response = client.chat.completions.create(
            model=model_choice,
            messages=[
                {"role": "system", "content": SYSTEM_PROMPT},
                {"role": "user", "content": f"User prompt: {user_prompt}\n\nPlease generate the podcast episode in JSON format with episode_title, episode_description, and episode_transcript fields."}
            ],
            max_tokens=3000,
            temperature=0.7,
            top_p=0.9
        )
        
        # Extract the response content
        response_text = response.choices[0].message.content
        
        # Try to parse JSON response
        try:
            # Clean the response to extract JSON
            clean_text = response_text.strip()
            if clean_text.startswith("```json"):
                clean_text = clean_text[7:]
            if clean_text.endswith("```"):
                clean_text = clean_text[:-3]
            
            podcast_data = json.loads(clean_text)
            
            # Format the output
            formatted_output = f"""# {podcast_data.get('episode_title', 'The Daniel Prompt Hour Episode')}

## Episode Description
{podcast_data.get('episode_description', 'No description available')}

## Episode Transcript
{podcast_data.get('episode_transcript', 'No transcript available')}"""
            
            return formatted_output
            
        except json.JSONDecodeError:
            # If JSON parsing fails, return the raw response
            return f"# The Daniel Prompt Hour Episode\n\n## Raw Response\n{response_text}"
            
    except Exception as e:
        return f"Error generating podcast transcript: {str(e)}"

# Create Gradio interface
with gr.Blocks(
    title="The Daniel Prompt Hour - Podcast Generator",
    theme=gr.themes.Soft(),
    css="""
    .gradio-container {
        max-width: 900px !important;
    }
    .header {
        text-align: center;
        margin-bottom: 20px;
    }
    """
) as demo:
    
    gr.HTML("""
    <div class="header">
        <img src="https://res.cloudinary.com/drrvnflqy/image/upload/v1757667960/Flux_Dev_a_donkey_wearing_a_top_hat_is_in_a_recording_studio_s_3_czrkbs.jpg" 
             alt="Herman Poppleberry in the recording studio" 
             style="width: 200px; height: 200px; object-fit: cover; border-radius: 15px; margin-bottom: 20px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
        <h1>πŸŽ™οΈ The Daniel Prompt Hour</h1>
        <h3>Featuring Herman Poppleberry, the Donkey 🫏</h3>
        <p>Enter any prompt and Herman will turn it into a podcast episode!</p>
    </div>
    """)
    
    with gr.Row():
        with gr.Column(scale=2):
            api_key_input = gr.Textbox(
                label="OpenAI API Key",
                placeholder="Enter your OpenAI API key (sk-...)",
                type="password",
                info="Your API key is not stored and only used for this session"
            )
            
            user_input = gr.Textbox(
                label="Your Prompt",
                placeholder="Enter any question or topic you'd like Herman to discuss in a podcast episode...",
                lines=3,
                max_lines=5
            )
            
            model_dropdown = gr.Dropdown(
                choices=[
                    "gpt-4o-mini",
                    "gpt-4o",
                    "gpt-4-turbo",
                    "gpt-3.5-turbo"
                ],
                value="gpt-4o-mini",
                label="OpenAI Model",
                info="Choose the OpenAI model to generate the podcast transcript"
            )
            
            generate_btn = gr.Button("πŸŽ™οΈ Generate Podcast Episode", variant="primary", size="lg")
        
        with gr.Column(scale=3):
            output = gr.Markdown(
                label="Generated Podcast Episode",
                value="Enter a prompt and click 'Generate Podcast Episode' to see Herman's response!",
                height=600
            )
    
    # Examples
    gr.Examples(
        examples=[
            ["What's the future of artificial intelligence?"],
            ["How do I make the perfect hummus?"],
            ["Why do cats purr?"],
            ["What's the meaning of life?"],
            ["How does quantum computing work?"],
            ["What makes a good podcast?"]
        ],
        inputs=user_input,
        label="Example Prompts"
    )
    
    # Event handlers
    generate_btn.click(
        fn=generate_podcast_transcript,
        inputs=[user_input, api_key_input, model_dropdown],
        outputs=output,
        show_progress=True
    )
    
    user_input.submit(
        fn=generate_podcast_transcript,
        inputs=[user_input, api_key_input, model_dropdown],
        outputs=output,
        show_progress=True
    )

# Launch the app
if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )