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("""
Enter any prompt and Herman will turn it into a podcast episode!