|
|
import streamlit as st |
|
|
from dotenv import load_dotenv, set_key |
|
|
from generate_script import generate_script |
|
|
import os |
|
|
import json |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
|
|
|
api_key = st.session_state.get("OPENAI_API_KEY", None) |
|
|
serper_api_key = st.secrets.get("SERPER_API_KEY") |
|
|
|
|
|
|
|
|
if not api_key: |
|
|
st.error("API Key not provided! Please enter it in the main app.") |
|
|
else: |
|
|
os.environ["OPENAI_API_KEY"] = api_key |
|
|
|
|
|
|
|
|
history_file = "script_history.json" |
|
|
|
|
|
|
|
|
def load_history(): |
|
|
if os.path.exists(history_file): |
|
|
with open(history_file, "r") as file: |
|
|
return json.load(file) |
|
|
return [] |
|
|
|
|
|
|
|
|
def save_history(history): |
|
|
with open(history_file, "w") as file: |
|
|
json.dump(history, file, indent=4) |
|
|
|
|
|
|
|
|
script_history = load_history() |
|
|
|
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
|
|
|
|
|
|
with col1: |
|
|
st.subheader("Input π") |
|
|
prompt = st.text_input("Provide the topic of the video:", placeholder="e.g., How to bake a cake") |
|
|
video_length = st.number_input("Specify length in minutes", min_value=1.0, step=0.5, value=10.0) |
|
|
|
|
|
with st.sidebar: |
|
|
st.title("Options") |
|
|
creativity = st.slider("Set creativity level:", min_value=0.0, max_value=1.0, value=0.5) |
|
|
|
|
|
|
|
|
generate_script_button = st.button("Generate Script") |
|
|
|
|
|
|
|
|
with col2: |
|
|
st.subheader("Output β¨") |
|
|
|
|
|
if generate_script_button: |
|
|
if not prompt: |
|
|
st.error("Please provide a topic for the video.") |
|
|
else: |
|
|
try: |
|
|
|
|
|
with st.spinner("Generating your script..."): |
|
|
title, script, search_data = generate_script(prompt, video_length, creativity, api_key, serper_api_key) |
|
|
|
|
|
|
|
|
st.success("Script generated successfully!") |
|
|
st.subheader(f"Title: {title}") |
|
|
st.write(f"Script: {script}") |
|
|
|
|
|
|
|
|
new_entry = { |
|
|
"title": title, |
|
|
"script": script, |
|
|
"search_data": search_data, |
|
|
} |
|
|
script_history.append(new_entry) |
|
|
save_history(script_history) |
|
|
|
|
|
|
|
|
st.download_button( |
|
|
label="Download Script", |
|
|
data=script, |
|
|
file_name="generated_script.txt", |
|
|
mime="text/plain", |
|
|
) |
|
|
|
|
|
|
|
|
with st.expander("Show search data for the script"): |
|
|
st.write(search_data) |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
st.error("An error occurred. Try again.") |
|
|
with st.expander("Expand to see details"): |
|
|
st.write(f"{e}") |
|
|
|