import streamlit as st from openai import OpenAI from prompts import SYSTEM_MESSAGE, USER_MESSAGE import json import os # Set Streamlit layout to wide mode st.set_page_config(layout="wide") st.title("🎬 AI-Powered Content Planner - Clip Creator") st.markdown("Paste a transcript on the left and view the generated content plan on the right.") # Sidebar for OpenAI API Key OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") DEFAULT_MODEL = "gpt-4o-2024-08-06" DEFAULT_GOAL = "Extract multiple self-contained clips by identifying natural narrative peaks, emotional highlights, and shareable moments (relatable struggles, surprising insights, or friendly debates) in their original sequence, optimizing for standalone engagement potential." # Sidebar: Model Selection st.sidebar.subheader("⚙️ Settings") MODEL = st.sidebar.selectbox( "Choose OpenAI Model:", [DEFAULT_MODEL, "gpt-4o-mini", "o3-mini"], index=0 # Default selection ) # Sidebar: Goal Customization st.sidebar.subheader("🎯 Customize Prompt") GOAL = st.sidebar.text_area("Specify specific prompt to extract clips (optional):", value=DEFAULT_GOAL, height=100) GOAL = GOAL.strip() if GOAL.strip() else DEFAULT_GOAL if not OPENAI_API_KEY: st.warning("⚠️ Please enter your OpenAI API key.") st.stop() client = OpenAI(api_key=OPENAI_API_KEY) # Layout: Left (Input) | Right (Output) col1, col2 = st.columns([1, 1]) with col1: st.subheader("📝 Paste Your Transcript") transcript = st.text_area("Enter the transcript here:", height=400) with col2: st.subheader("📋 Generated Content Plan") generated_plan_container = st.container() generate_button = st.button("Generate Plan") if generate_button: if not transcript.strip(): st.error("❌ Please enter a transcript.") else: with st.spinner("⏳ Generating content plan... Please wait."): try: # Define prompts system_prompt = SYSTEM_MESSAGE.format(prompt_goal=GOAL) user_prompt = USER_MESSAGE.format(source_content=transcript) messages = [ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}, ] # Create placeholder for dynamic streaming generated_plan_container.empty() openai_args = { "model": MODEL, "messages": messages, "response_format": {"type": "json_object"}, } if MODEL == 'o3-mini': openai_args['reasoning_effort'] = "low" else: openai_args["max_tokens"] = 5000 openai_args["temperature"] = 0.45 # Stream OpenAI API Response response = client.chat.completions.create(**openai_args) # Parse the response generated_response = response.choices[0].message.content.strip() content_plan = json.loads(generated_response) # Extract key (assuming there is only one key in the JSON response) plan_key = list(content_plan.keys())[0] clip_plans = content_plan.get(plan_key, []) # Display final output if clip_plans: with generated_plan_container.container(): for i, clip in enumerate(clip_plans): st.markdown(f"### 🎬 Clip {i + 1}") st.write(f"**Title:** {clip.get('Title', 'N/A')}") st.write(f"**Focus Prompt:** {clip.get('Focus Prompt', 'N/A')}") st.write(f"**Duration:** {clip.get('Duration Target', 'N/A')} seconds") st.markdown("---") else: st.error("⚠️ No clips were generated. Try again.") except json.JSONDecodeError: st.error("⚠️ Failed to parse OpenAI response. Try again.") except Exception as e: st.error(f"❌ Error: {str(e)}")