Spaces:
Sleeping
Sleeping
File size: 4,162 Bytes
f1745b4 4e88dff f1745b4 60357fa f1745b4 4e88dff 60357fa a6786ea 60357fa 361912b a6786ea f1745b4 3710d26 f1745b4 60357fa 3addf3a e8965d3 60357fa f1745b4 60357fa f1745b4 45dd438 2977b17 45dd438 |
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 |
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)}")
|